jalvesaq / colorout

Colorize R output in terminal emulators
288 stars 21 forks source link

is it possible for colorout not to color numbers in string? #37

Closed ssh352 closed 7 months ago

ssh352 commented 9 months ago
Screenshot 2024-01-05 at 10 34 44 PM

in the commit-hash and host for example. Thanks!

joshuaulrich commented 9 months ago

@jalvesaq would it work if the code required numbers to be either the first character or preceded by a whitespace character?

jalvesaq commented 9 months ago

Each new condition added to the code makes it slower. But we can try the branch https://github.com/jalvesaq/colorout/tree/num_begin The result is not perfect: "64" in "x86_64" will still be highlighted as a number.

joshuaulrich commented 9 months ago

Here's a patch for what I came up with. It requires a whitespace character before a digit in order to format the output as a number.

diff --git a/src/colorout.c b/src/colorout.c
index eae9be5..90b80d8 100644
--- a/src/colorout.c
+++ b/src/colorout.c
@@ -76,8 +76,17 @@ static int isword(const char * b, int i, int len)
     return !is_letter_preceeding && !is_letter_following;
 }

+static int iswhitespace(const char b)
+{
+    /* space or horizontal tab, new line, vertical tab, form feed, carriage return */
+    return b == 32 || (b >= 9 && b <= 13);
+}
+
 static int isnumber(const char * b, int i, int len)
 {
+    if(i > 0 && !iswhitespace(b[i-1]))
+        return 0;
+
     int l = len;
     if(l > (i + 5))
         l = i + 5;

Here's the result:

image

jalvesaq commented 9 months ago

Much simpler! I will wait a few days for other people's comments before making the code changes. Since the condition you are proposing is much simpler, I think it will be OK to put it directly at the beginning of isnumber().

jalvesaq commented 9 months ago

@joshuaulrich, I update the num_begin branch to your code.

joshuaulrich commented 8 months ago

I think it will be OK to put it directly at the beginning of isnumber()

I agree. I created the function before I implemented it and knew what the conditions would be. It makes sense to avoid the function call and include the conditions directly, especially since iswhitespace() would only be called once.

jalvesaq commented 8 months ago

Actually, I tried both ways. I didn't see any difference in speed (possibly, the compiler optimization eliminates the function call), and the code becomes easier to understand with the iswhitespace() function.