tokiwa-software / fuzion

The Fuzion Language Implementation
https://fuzion-lang.dev
GNU General Public License v3.0
46 stars 11 forks source link

Use underline instead of 'Dächle'? (in terminals that support it) #3297

Closed michaellilltokiwa closed 2 months ago

michaellilltokiwa commented 3 months ago

This is how it would look like: 2024-07-02_10-43

The patch:

diff --git a/src/dev/flang/util/SourcePosition.java b/src/dev/flang/util/SourcePosition.java
index 5b0ca87d1..1453e20f6 100644
--- a/src/dev/flang/util/SourcePosition.java
+++ b/src/dev/flang/util/SourcePosition.java
@@ -188,19 +188,30 @@ public class SourcePosition extends ANY implements Comparable<SourcePosition>, H
         while (_sourceFile.numLines() >= l+1 && _sourceFile.lineStartPos(l+1) <= p)
           {
             l = l + 1;
+            var endPos = byteEndPos() > _sourceFile.lineEndPos(l) ? _sourceFile.lineEndPos(l) : byteEndPos();
+            var str = _sourceFile.asString(p, endPos);
+            var noneWS = firstNoneWS(str);
             sb.append(Terminal.BLUE)
-              .append(_sourceFile.line(l));
+              .append(_sourceFile.asString(_sourceFile.lineStartPos(l), p))
+              .append(str.subSequence(0, noneWS))
+              .append(Terminal.CURLY_UNDERLINE)
+              .append(str.subSequence(noneWS, str.length()))
+              .append(Terminal.CURLY_UNDERLINE_OFF)
+              .append(_sourceFile.asString(endPos, _sourceFile.lineEndPos(l)));
             if (sb.length() != 0 && sb.charAt(sb.length()-1) != '\n')
               { // add LF in case this is the last line of a file that does not end in a line break
                 sb.append("\n");
               }
-            sb.append(Terminal.YELLOW);
-            for (int j=0; l == line() && j < column()-1; j++)
+            if (!Terminal.ENABLED)
               {
-                sb.append('-');
+                sb.append(Terminal.YELLOW);
+                for (int j=0; l == line() && j < column()-1; j++)
+                {
+                  sb.append('-');
+                }
               }
           }
-        if (p < _sourceFile.lineEndPos(l) || p == _bytePos || p == byteEndPos()-1) // suppress '^' at LFs except for LF at end position
+        if (!Terminal.ENABLED && (p < _sourceFile.lineEndPos(l) || p == _bytePos || p == byteEndPos()-1)) // suppress '^' at LFs except for LF at end position
           {
             sb.append('^');
             if (p+1 < byteEndPos() && p+1 == _sourceFile.lineEndPos(l))
@@ -214,6 +225,19 @@ public class SourcePosition extends ANY implements Comparable<SourcePosition>, H
   }

+  private int firstNoneWS(String str)
+  {
+    for (int i = 0; i < str.length(); i++)
+      {
+        if (!Character.isWhitespace(str.charAt(i)))
+          {
+            return i;
+          }
+      }
+    return str.length()-1;
+  }
+
+
   /**
    * @return the line of this source position,
    * starting at 1, return 0 for empty file.
diff --git a/src/dev/flang/util/Terminal.java b/src/dev/flang/util/Terminal.java
index 1b27f6026..7dd3a4850 100644
--- a/src/dev/flang/util/Terminal.java
+++ b/src/dev/flang/util/Terminal.java
@@ -59,7 +59,8 @@ public class Terminal extends ANY
    */
   public static final boolean ENABLED =
     !"true".equals(System.getProperty("FUZION_DISABLE_ANSI_ESCAPES")) &&
-    !"true".equals(System.getenv     ("FUZION_DISABLE_ANSI_ESCAPES"));
+    !"true".equals(System.getenv     ("FUZION_DISABLE_ANSI_ESCAPES")) &&
+    System.getenv().get("TERM") != null;

   public static final String RESET                     = ENABLED ? "\033[0m" : "";
   public static final String BOLD                      = ENABLED ? "\033[1m" : "";
@@ -67,7 +68,10 @@ public class Terminal extends ANY
   public static final String ITALICS                   = ENABLED ? "\033[3m" : "";
   public static final String ITALICS_OFF               = ENABLED ? "\033[23m" : "";
   public static final String UNDERLINE                 = ENABLED ? "\033[4m" : "";   // also called underscore
+  public static final String DOUBLE_UNDERLINE          = ENABLED ? "\033[21m" : "";
+  public static final String CURLY_UNDERLINE           = ENABLED ? "\033[4:3m" : "";
   public static final String UNDERLINE_OFF             = ENABLED ? "\033[24m" : "";
+  public static final String CURLY_UNDERLINE_OFF       = ENABLED ? "\033[4:0m" : "";
   public static final String BLINK                     = ENABLED ? "\033[5m" : "";
   public static final String BLINK_OFF                 = ENABLED ? "\033[25m" : "";
   public static final String INVERSE                   = ENABLED ? "\033[7m" : "";
@@ -85,6 +89,7 @@ public class Terminal extends ANY
   public static final String CYAN                      = ENABLED ? "\033[36m" : "";
   public static final String WHITE                     = ENABLED ? "\033[37m" : "";
   public static final String REGULAR_COLOR             = ENABLED ? "\033[39m" : "";
+  public static final String COLOR_OFF                 = ENABLED ? "\033[59m" : "";

   public static final String PLAIN_BLACK               = ENABLED ? "\033[0;30m" : "";
   public static final String PLAIN_RED                 = ENABLED ? "\033[0;31m" : "";

@tokiwa-software/developers your opinions?

maxteufel commented 3 months ago
$ printf '\e[4:3m\e[58;5;166m\e[38;5;79m%s\e[59m\e[4:0m\n' hello

should print green-ish text with an orange-ish underline

edit: intended as an example that different colors are possible in text and underline, not necessarily a suggestion for these colors

maxteufel commented 3 months ago

to add my opinion on this: i very much prefer the underline to the ^^^^, but the underline should have a different color than the code, see my comment above