I spotted the more implementation in pending. Looking at it, it's missing quite 
a lot of stuff, 
Such as the ability to go back in a file. It's built in a way where nothing is 
accumulated, Which 
means that support for that would require a half-rewrite.

What does POSIX specify as far as options? Looking at the man page, quite a 
bit. None of which I've
ever seen used before.

Looking at the other keybindings GNU more provides which I can implement, 
There's "=" (prints current
line number) ":f" (print filename and line), as well as being able to use the 
down arrow to go down 
(with the added side effect of any escape key doing so too, not the end of the 
world, especially
since we can't scroll up) That are Implemented them in the attached patch.

There is also a testing problem. vi.c doesn't do TEST_HOST because it needs a 
-s option
to pass in scripts to test with. less and more are _worse_ since they don't 
change anything.
Manually testing often introduces regressions, so I dunno what the solution is 
here

This patch improves things for now, but if we are planning on doing a future 
less implementation. 
We could probably merge more into that and make them share a common base either 
as 2 functions in
the same file, or just make more a OLDTOY pointing to less like how [ points to 
[[.

(The rest of this email is about less, not more)

Seeing if this is possible, what does GNU less implement:

$ man less
[...]
       less [-[+]aABcCdeEfFgGiIJKLmMnNqQrRsSuUVwWX~]
            [-b space] [-h lines] [-j line] [-k keyfile]
            [-{oO} logfile] [-p pattern] [-P prompt] [-t tag]
            [-T tagsfile] [-x tab,...] [-y lines] [-[z] lines]
            [-# shift] [+[+]cmd] [--] [filename]...

Oh. Busybox has a less implementation, what does it implement:

        -E      Quit once the end of a file is reached
        -F      Quit if entire file fits on first screen
        -I      Ignore case in all searches
        -M,-m   Display status line with line numbers and percentage through 
the file
        -N      Prefix line number to each line
        -S      Truncate long lines
        -R      Remove color escape codes in input
        -~      Suppress ~s displayed past EOF

That's more reasonable. Although I've used less -R before and that is the exact 
opposite of
what it is for. Plus no keybinding list and "h" isn't showing them... Which 
means I'd
have to look at and study the GPLv2 source code, whee! Using this "when in 
doubt; look at what busybox does"
approach to more gives me what looks like the exact same implementation that is 
in toybox, internal help text and all.

less seems moderately easy (Read lines into list, show lines, scroll down and 
read more lines,
scroll up and go back in list.) Especially since we already have vi.c which 
does almost all the stuff
less does at it's core, there's a lot of potential code-sharing (To which file 
though, lib/tty.c?).
But I have to ask the question "If it's so easy, why isn't it in toybox yet?" 
Is it just because
other TODO items taking up time, or is it because it's harder to implement than 
it seems.

-   Oliver Webb <aquahobby...@proton.me>
From 5954a76ec579b3ca573abf5e138430f3483098c5 Mon Sep 17 00:00:00 2001
From: Oliver Webb <aquahobby...@proton.me>
Date: Tue, 19 Mar 2024 23:36:36 -0500
Subject: [PATCH] more.c: More stuff (:f, :n, =), down arrow goes down

---
 toys/pending/more.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/toys/pending/more.c b/toys/pending/more.c
index dbd429b4..ab2c26de 100644
--- a/toys/pending/more.c
+++ b/toys/pending/more.c
@@ -29,7 +29,7 @@ static void signal_handler(int sig)
   tcsetattr(TT.cin_fd, TCSANOW, &TT.inf);
 
   // If we were actually signalled, move to a new line and re-raise the signal.
-  if (sig != 0) {
+  if (sig) {
     xputc('\n');
     signal(sig, SIG_DFL);
     raise(sig);
@@ -42,7 +42,7 @@ static void show_file_header(const char *name)
   printf("::::::::::::::\n%s\n::::::::::::::\n", name);
 }
 
-static int prompt(FILE *cin, const char* fmt, ...)
+static int prompt(FILE *cin, const char *fmt, ...)
 {
   int input_key;
   va_list ap;
@@ -57,11 +57,12 @@ static int prompt(FILE *cin, const char* fmt, ...)
     fflush(NULL);
     input_key = tolower(getc(cin));
     printf("\33[0m\33[1K\r"); // Reset all attributes, erase to start of line.
-    if (strchr(" \nrq", input_key)) {
+    if (strchr("\e \nrq=:", input_key)) {
       fflush(NULL);
       return input_key;
     }
-    printf("\33[7m(Enter:Next line Space:Next page Q:Quit R:Show the rest)");
+    printf("\33[7m(Enter:Next line Space:Next page Q:Quit R:Show rest "
+        ":f:File line :n:next =:line)");
   }
 }
 
@@ -112,6 +113,7 @@ void more_main()
 
   do {
     char *filename = *toys.optargs;
+    size_t lineno = 0;
 
     st.st_size = show_prompt = col = row = 0;
     if (!filename) fp = stdin;
@@ -128,7 +130,7 @@ void more_main()
 
     if (toys.optc > 1) {
       show_file_header(filename);
-      row += 3;
+      row+=3;
     }
 
     while ((ch = getc(fp)) != EOF) {
@@ -139,11 +141,19 @@ void more_main()
               (long long)st.st_size);
         else
           input_key = prompt(cin, "--More--");
-        if (input_key == 'q') goto stop;
 
-        col = row = show_prompt = 0;
-        terminal_size(&cols, &rows);
-        rows--;
+        if (input_key == 'q') goto stop;
+        else if (input_key == ':') switch (getc(cin)) {
+            case 'n': goto next_file;
+            case 'f':
+              prompt(cin, "\"%s\" line %lu", *toys.optargs ? : "stdin", lineno);
+              break;
+        } else if (input_key == '=') prompt(cin, "%lu", lineno);
+        else {
+          col = row = show_prompt = 0;
+          terminal_size(&cols, &rows);
+          rows--;
+        }
       }
 
       putchar(ch);
@@ -152,6 +162,7 @@ void more_main()
       if (col == cols) putchar(ch = '\n');
       if (ch == '\n') {
         col = 0;
+        lineno++;
         if (++row >= rows || input_key == '\n') show_prompt = 1;
       }
     }
-- 
2.44.0

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to