OSDN Git Service

Print name of file at the bottom of the screen.
authorRob Landley <rob@landley.net>
Wed, 13 May 2015 06:34:01 +0000 (01:34 -0500)
committerRob Landley <rob@landley.net>
Wed, 13 May 2015 06:34:01 +0000 (01:34 -0500)
And yes, I tested $PWD/私はガラスを食べられま す。それは私を傷つけません。
as a name and made it work. If you throw newlines or ascii escapes in the
name it'll use the fancy printing logic for chars, otherwise it does the
full utf8 fontmetrics deal.

toys/other/hexedit.c

index 9e9ca11..404095e 100644 (file)
@@ -4,7 +4,7 @@
  *
  * No standard
 
-USE_HEXEDIT(NEWTOY(hexedit, "<1>1r", TOYFLAG_USR|TOYFLAG_BIN))
+USE_HEXEDIT(NEWTOY(hexedit, "<1>1r", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE))
 
 config HEXEDIT
   bool "hexedit"
@@ -46,6 +46,7 @@ static void fix_terminal(void)
   esc("?25h");
   esc("0m");
   jump(0, 999);
+  esc("K");
 }
 
 static void sigttyreset(int i)
@@ -73,6 +74,40 @@ static void draw_char(char broiled)
   } else printf("%c", broiled);
 }
 
+static void draw_tail(void)
+{
+  int i = 0, width = 0, w, len;
+  char *start = *toys.optargs, *end;
+
+  jump(0, TT.height);
+  esc("K");
+
+  // First time, make sure we fit in 71 chars (advancing start as necessary).
+  // Second time, print from start to end, escaping nonprintable chars.
+  for (i=0; i<2; i++) {
+    for (end = start; *end;) {
+      wchar_t wc;
+
+      len = mbrtowc(&wc, end, 99, 0);
+      if (len<0 || wc<32 || (w = wcwidth(wc))<0) {
+        len = w = 1;
+        if (i) draw_char(*end);
+      } else if (i) fwrite(end, len, 1, stdout);
+      end += len;
+
+      if (!i) {
+        width += w;
+        while (width > 71) {
+          len = mbrtowc(&wc, start, 99, 0);
+          if (len<0 || wc<32 || (w = wcwidth(wc))<0) len = w = 1;
+          width -= w;
+          start += len;
+        }
+      }
+    }
+  }
+}
+
 static void draw_line(long long yy)
 {
   int x, xx = 16;
@@ -99,6 +134,7 @@ static void draw_page(void)
     if (y) printf("\r\n");
     draw_line(y);
   }
+  draw_tail();
 }
 
 // side: 0 = editing left, 1 = editing right, 2 = clear, 3 = read only
@@ -171,7 +207,7 @@ void hexedit_main(void)
 
     // Display cursor
     highlight(x, y, ro ? 3 : side);
-    fflush(0);
+    xprintf("");
 
     // Wait for next key
     key = scan_key(toybuf, keys, 1);
@@ -204,8 +240,7 @@ void hexedit_main(void)
         if (TT.base) {
           TT.base--;
           esc("1T");
-          jump(0, TT.height);
-          esc("K");
+          draw_tail();
           jump(0, 0);
           draw_line(0);
         }
@@ -218,6 +253,7 @@ down:
         esc("1S");
         jump(0, TT.height-1);
         draw_line(TT.height-1);
+        draw_tail();
       }
       if (++y>=TT.height) y--;
     } else if (key==KEY_RIGHT) {