]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Teach the CLI that VT100-escape codes that do things like change font
authordrh <>
Fri, 21 Mar 2025 18:15:13 +0000 (18:15 +0000)
committerdrh <>
Fri, 21 Mar 2025 18:15:13 +0000 (18:15 +0000)
colors have zero-width for the purpose of laying out the columns of a
table.

FossilOrigin-Name: 2d0a8a6c38981552748ff5fc2eeba86590e0f116abac260a7fc9318de0a0dbda

manifest
manifest.uuid
src/shell.c.in
test/vt100-a.sql [new file with mode: 0644]

index aa6df8a09f93b0ee06505a9eb13238aff40e9df8..6b242f7484d4959f8c55f0348101321923f33a58 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Flesh\sout\sthe\snew\sproc-debug\sand\sits\sinfrastructure\sa\sbit.
-D 2025-03-21T16:49:32.629
+C Teach\sthe\sCLI\sthat\sVT100-escape\scodes\sthat\sdo\sthings\slike\schange\sfont\ncolors\shave\szero-width\sfor\sthe\spurpose\sof\slaying\sout\sthe\scolumns\sof\sa\ntable.
+D 2025-03-21T18:15:13.290
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md e108e1e69ae8e8a59e93c455654b8ac9356a11720d3345df2a4743e9590fb20d
@@ -784,7 +784,7 @@ F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c
 F src/resolve.c 20e1fbe8f840ffc0cd835e33f68a802a22e34faa918d7a269f3de242fda02f99
 F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97
 F src/select.c bfe14cdfceba54744b1c6c29099313f5173a0793dfaff0cd484774e9d05dbeab
-F src/shell.c.in 248050551cad788f8bb4b4728e00d8e36a10130d2d101e55cd51cfee03df91ff
+F src/shell.c.in 9d1b46e09c1b933b0c7afaf4ae27030dc356ee19ae4f95ce8bf3647035b9635b
 F src/sqlite.h.in fd70afd92948cf7cc93f687ac960bad1b0b6fbc436752419eff2fd65a1809380
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
 F src/sqlite3ext.h 3f046c04ea3595d6bfda99b781926b17e672fd6d27da2ba6d8d8fc39981dcb54
@@ -1972,6 +1972,7 @@ F test/view.test 3c23d7a068e9e4a0c4e6907498042772adea725f0630c3d9638ffd4e5a08b92
 F test/view2.test db32c8138b5b556f610b35dfddd38c5a58a292f07fda5281eedb0851b2672679
 F test/view3.test ad8a8290ee2b55ff6ce66c9ef1ce3f1e47926273a3814e1c425293e128a95456
 F test/vt02.c 5b44ac67b1a283fedecf2d6e2ceda61e7a157f01d44dcb4490dcb1e87d057060
+F test/vt100-a.sql 631eeab18c5adb531bab79aecf64eee3934b42c75a309ee395c814717a6a7651
 F test/vtab1.test 09a72330d0f31eda2ffaa828b06a6b917fb86250ee72de0301570af725774c07
 F test/vtab2.test 14d4ab26cee13ba6cf5c5601b158e4f57552d3b055cdd9406cf7f711e9c84082
 F test/vtab3.test b45f47d20f225ccc9c28dc915d92740c2dee311e
@@ -2215,8 +2216,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350
 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7
 F tool/warnings.sh 49a486c5069de041aedcbde4de178293e0463ae9918ecad7539eedf0ec77a139
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P f0298c773d3490ad3a5b53d2ceeff1bd90e1a7bb5deeba2d24f681ec1bc10510
-R 3cc891ff71296fb59fcd1fa5eb66bbd2
-U stephan
-Z 05c9795d3fb61aee80553fe96bed0bc9
+P ba7f1ff0d7d1d3fb79fc298d99fd27b65f639fb1691a1a9cdc9c006b8ff41212
+R 8af62bb2e2dc8ccdf26ae7c0bc939029
+U drh
+Z cfacc14c0921c2b298dd0d24d3eebca7
 # Remove this line to create a well-formed Fossil manifest.
index 8597b0b793c3965d86b5ec635b99b7c8bded7bfb..0b4b998c42bd4215858ed5612805ab1fe4373352 100644 (file)
@@ -1 +1 @@
-ba7f1ff0d7d1d3fb79fc298d99fd27b65f639fb1691a1a9cdc9c006b8ff41212
+2d0a8a6c38981552748ff5fc2eeba86590e0f116abac260a7fc9318de0a0dbda
index 93d73e6ac70c96c13dae57f2dc89cb4e92bab8ab..8272956ebd202465922bfe011b64ecaf0c749a33 100644 (file)
@@ -784,6 +784,23 @@ int cli_wcswidth(const char *z){
 }
 #endif
 
+/*
+** Check to see if z[] is a valid VT100 escape.  If it is, then
+** return the number of bytes in the escape sequence.  Return 0 if
+** z[] is not a VT100 escape.
+**
+** This routine assumes that z[0] is \033 (ESC).
+*/
+static int isVt100(const unsigned char *z){
+  int i;
+  if( z[1]!='[' ) return 0;
+  i = 2;
+  while( z[i]>=0x30 && z[i]<=0x3f ){ i++; }
+  while( z[i]>=0x20 && z[i]<=0x2f ){ i++; }
+  if( z[i]<0x40 || z[i]>0x7e ) return 0;
+  return i+1;
+}
+
 /*
 ** Output string zUtf to stdout as w characters.  If w is negative,
 ** then right-justify the text.  W is the width in UTF-8 characters, not
@@ -799,6 +816,7 @@ static void utf8_width_print(FILE *out, int w, const char *zUtf){
   unsigned char c;
   int i = 0;
   int n = 0;
+  int k;
   int aw = w<0 ? -w : w;
   if( zUtf==0 ) zUtf = "";
   while( (c = a[i])!=0 ){
@@ -811,6 +829,8 @@ static void utf8_width_print(FILE *out, int w, const char *zUtf){
       }
       i += len;
       n += x;
+    }else if( c==0x1b && (k = isVt100(&a[i]))>0 ){
+      i += k;       
     }else if( n>=aw ){
       break;
     }else{
@@ -3998,9 +4018,14 @@ static char *translateForDisplayAndDup(
       i++;
       continue;
     }
-    n++;
-    j += 3;
-    i++;
+    if( c==0x1b && p->eEscMode==SHELL_ESC_OFF && (k = isVt100(&z[i]))>0 ){
+      i += k;
+      j += k;
+    }else{
+      n++;
+      j += 3;
+      i++;
+    }
   }
   if( n>=mxWidth && bWordWrap  ){
     /* Perhaps try to back up to a better place to break the line */
@@ -4066,9 +4091,17 @@ static char *translateForDisplayAndDup(
         zOut[j++] = '^';
         zOut[j++] = 0x40 + c;
         break;
-      case SHELL_ESC_OFF:
-        zOut[j++] = c;
+      case SHELL_ESC_OFF: {
+        int nn;
+        if( c==0x1b && (nn = isVt100(&z[i]))>0 ){
+          memcpy(&zOut[j], &z[i], nn);
+          j += nn;
+          i += nn - 1;
+        }else{
+          zOut[j++] = c;
+        }
         break;
+      }
     }
     i++;
   }
diff --git a/test/vt100-a.sql b/test/vt100-a.sql
new file mode 100644 (file)
index 0000000..a0d3f46
--- /dev/null
@@ -0,0 +1,19 @@
+/*
+** Run this script using the "sqlite3" command-line shell
+** test test formatting of output text that contains
+** vt100 escape sequences.
+*/
+.mode box -escape off
+CREATE TEMP TABLE t1(a,b,c);
+INSERT INTO t1 VALUES
+  ('one','twotwotwo','thirty-three'),
+  (unistr('\u001b[91mRED\u001b[0m'),'fourfour','fifty-five'),
+  ('six','seven','eighty-eight');
+.print With -escape off
+SELECT * FROM t1;
+.mode box -escape ascii
+.print With -escape ascii
+SELECT * FROM t1;
+.mode box -escape symbol
+.print With -escape symbol
+SELECT * FROM t1;