commit d7601a723dc3adc98cb0b6e51d818a8cc0a87d84
parent f7a0c3428622eed6a34341dcdfc0f3df9f92b1d6
Author: hhvn <dev@hhvn.uk>
Date: Wed, 22 Jun 2022 19:03:00 +0100
Handle ids as arguments
Diffstat:
3 files changed, 41 insertions(+), 9 deletions(-)
diff --git a/zygo.1 b/zygo.1
@@ -87,9 +87,29 @@ Program to use for yanking URIs.
is constantly reading input.
This input can be either an id to a link
(seen to the left of a line) or a command.
-If a command requires an argument,
-or the id is ambiguous,
-the user must press enter.
+
+When an id is inputted with no command,
+the link is followed.
+
+If an id needs to be inputted,
+pressing enter is not required as long as the inputted id can be disambiguated.
+For example, if the largest id is
+.Li 27 ,
+and
+.Li 3
+is inputted, it is clear that the link with the id of 3 should be used.
+However, if
+.Li 2
+is inputted, it could refer to any link with an id between 20 and 29,
+so the user must press enter.
+The logic is as follows:
+.Dl if (id * 10 > largest_id_on_page) follow_the_link;
+
+When a command is entered, it may require an argument.
+If so, the character used for the command is shown,
+and the cursor is displayed next to it.
+The argument can either be text, or it can be an id
+(in which case it will follows the rules of the previous paragraph).
.Sh COMMANDS
.Bl -tag -width " "
.It : Ar uri
diff --git a/zygo.c b/zygo.c
@@ -923,6 +923,18 @@ idgo(size_t id) {
go(list_idget(&page, id), 1, 0);
}
+int
+wantnum(char cmd) {
+ return (!ui.cmd || ui.cmd == BIND_DISPLAY || ui.cmd == BIND_YANK);
+}
+
+int
+acceptkey(char cmd, int key) {
+ if (wantnum(cmd))
+ return isdigit(key);
+ return key >= 32 && key < KEY_CODE_YES;
+}
+
/*
* Main loop
*/
@@ -959,6 +971,7 @@ run(void) {
if (c == 27 /* escape */) {
ui.wantinput = 0;
} else if (c == '\n') {
+submit:
switch (ui.cmd) {
case BIND_URI:
e = uritoelem(ui.arg);
@@ -1019,15 +1032,12 @@ run(void) {
} else if (ui.cmd == BIND_YANK && c == BIND_YANK && !il) {
ui.wantinput = 0;
yank(current);
- } else if (c >= 32 && c < KEY_CODE_YES) {
+ } else if (ui.cmd && acceptkey(ui.cmd, c)) {
ui.input[il++] = c;
ui.input[il] = '\0';
syncinput();
- if (!ui.cmd && atoi(ui.arg) * 10 > page->lastid) {
- idgo(atoi(ui.arg));
- ui.wantinput = 0;
- draw_page();
- }
+ if (wantnum(ui.cmd) && atoi(ui.arg) * 10 > page->lastid)
+ goto submit;
}
draw_bar();
} else {
diff --git a/zygo.h b/zygo.h
@@ -104,6 +104,8 @@ char *prompt(char *prompt, size_t count);
Elem *strtolink(char *str);
void pagescroll(int lines);
void idgo(size_t id);
+int wantnum(char cmd);
+int acceptkey(char cmd, int key);
/* Main loop */
void run(void);