From 40858a673abeb377c818ce4113d17dc73dad1267 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 21 Apr 2025 23:02:20 -0400 Subject: [PATCH 1/8] Use my regex engine instead of the stdlib one --- config.go | 6 +++--- config/go.conf | 2 +- go.mod | 9 ++++++--- go.sum | 8 ++++++++ main.go | 4 ++-- regcolor.go | 4 ++-- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/config.go b/config.go index 01ae107..dc581d9 100644 --- a/config.go +++ b/config.go @@ -6,11 +6,11 @@ import ( "io/fs" "os" "path/filepath" - "regexp" "runtime" "strings" "gitea.twomorecents.org/Rockingcool/ccat/stack" + "gitea.twomorecents.org/Rockingcool/kleingrep/regex" "gopkg.in/yaml.v2" ) @@ -88,13 +88,13 @@ func loadConfig(configFilename string) (stack.Stack[regColor], error) { // returned. regColorStack := stack.NewStack[regColor](len(strings.Split(string(configFile), "\n"))) // The stack will have the same size as the number of lines in the file for _, item := range tempMapSlice { - re := regexp.MustCompile(item.Key.(string)) + re := regex.MustCompile(item.Key.(string)) clr, err := newColor(item.Value.(string)) if err != nil { return *stack.NewStack[regColor](0), err } // If we got past the errors, then the color _must_ be valid. - regColorStack.Push(regColor{re, clr}) + regColorStack.Push(regColor{&re, clr}) } return *regColorStack, nil diff --git a/config/go.conf b/config/go.conf index 3c02ad2..35544c2 100644 --- a/config/go.conf +++ b/config/go.conf @@ -14,7 +14,7 @@ "'(.)'": BLUE "'\\\\(.)'": BLUE # The escape backslash needs to be escaped as well # Assignments and comparisons -'(?:\s|\b)(=|==|!=|<=|>=)(\s|\b)' : CYAN +'(?:\s|\b)(=|==|!=|\<=|\>=)(\s|\b)' : CYAN '(&&)|(\|\|)': CYAN # Keywords '\b(if|else|for|range|go|func|return|break|continue)\b': CYAN diff --git a/go.mod b/go.mod index f25a77b..f45eefb 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,14 @@ module gitea.twomorecents.org/Rockingcool/ccat -go 1.22.5 +go 1.23.1 + +toolchain go1.24.2 require ( - github.com/fatih/color v1.17.0 // indirect + gitea.twomorecents.org/Rockingcool/kleingrep v0.7.0 // indirect + github.com/fatih/color v1.18.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - golang.org/x/sys v0.18.0 // indirect + golang.org/x/sys v0.25.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 7aaf4bf..f6c85e5 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,13 @@ +gitea.twomorecents.org/Rockingcool/kleingrep v0.6.1 h1:eeryIhh2lDMXsu3D1i95IgW2SPte1DaJxlfmiQjrpsE= +gitea.twomorecents.org/Rockingcool/kleingrep v0.6.1/go.mod h1:8bcYe2hyjNIDM9J2xnyH5veMCAMzVJQR3c0OkatcEPg= +gitea.twomorecents.org/Rockingcool/kleingrep v0.7.0 h1:owDJjgulFmg9DmgKBtwZMxdf19wM9VbGchMXq7ZlhIM= +gitea.twomorecents.org/Rockingcool/kleingrep v0.7.0/go.mod h1:8bcYe2hyjNIDM9J2xnyH5veMCAMzVJQR3c0OkatcEPg= github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= +github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= +github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU= github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o= github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -13,6 +19,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go index e51fb46..5d5cad9 100644 --- a/main.go +++ b/main.go @@ -157,13 +157,13 @@ func main() { clr := regclr.clr // Returns an int double-slice, where each slice contains the start and end indices // of the match. In this case, I am finding all the matches of 're' in 'data'. - matches := re.FindAllSubmatchIndex(data, -1) + matches := re.FindAllSubmatch(string(data)) if matches == nil { continue } // For each match, apply the corresponding color to all characters in the match. for _, match := range matches { - units = applyColor(units, match[0], match[1], clr) + units = applyColor(units, match[0].StartIdx, match[0].EndIdx, clr) } } diff --git a/regcolor.go b/regcolor.go index e938cdf..3f1adc1 100644 --- a/regcolor.go +++ b/regcolor.go @@ -1,10 +1,10 @@ package main -import "regexp" +import "gitea.twomorecents.org/Rockingcool/kleingrep/regex" // A regColor is a regex-color pair. The config file is read // into a stack of this data type. type regColor struct { - re *regexp.Regexp + re *regex.Reg clr color } From 19be04fd6673ad62fb0bbb75160b68247aef48a6 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 22 Apr 2025 15:38:17 -0400 Subject: [PATCH 2/8] Enable multiline mode when compiling regex, because I read the whole file at once, rather than lline-by-line --- config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.go b/config.go index dc581d9..d27c6ea 100644 --- a/config.go +++ b/config.go @@ -88,7 +88,7 @@ func loadConfig(configFilename string) (stack.Stack[regColor], error) { // returned. regColorStack := stack.NewStack[regColor](len(strings.Split(string(configFile), "\n"))) // The stack will have the same size as the number of lines in the file for _, item := range tempMapSlice { - re := regex.MustCompile(item.Key.(string)) + re := regex.MustCompile(item.Key.(string), regex.RE_MULTILINE) clr, err := newColor(item.Value.(string)) if err != nil { return *stack.NewStack[regColor](0), err From 06fab2292c740059316205818d8cf4d000fd1ac0 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 22 Apr 2025 15:55:30 -0400 Subject: [PATCH 3/8] Enable RE_SINGLE_LINE flag when compiling regex, to allow newline to be treated like any other character --- config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.go b/config.go index d27c6ea..19bf956 100644 --- a/config.go +++ b/config.go @@ -88,7 +88,7 @@ func loadConfig(configFilename string) (stack.Stack[regColor], error) { // returned. regColorStack := stack.NewStack[regColor](len(strings.Split(string(configFile), "\n"))) // The stack will have the same size as the number of lines in the file for _, item := range tempMapSlice { - re := regex.MustCompile(item.Key.(string), regex.RE_MULTILINE) + re := regex.MustCompile(item.Key.(string), regex.RE_MULTILINE, regex.RE_SINGLE_LINE) clr, err := newColor(item.Value.(string)) if err != nil { return *stack.NewStack[regColor](0), err From a1309af6969969d420c2f010edafed83d7fadc09 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Wed, 23 Apr 2025 16:25:46 -0400 Subject: [PATCH 4/8] Updated some regexes One of the expressions used .* to mean 'any character except newline', but since I've enabled 'RE_SINGLE_LINE' mode, a dot matches a newline as well. --- config/c.conf | 6 +++++- config/go.conf | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/config/c.conf b/config/c.conf index eba8746..449b241 100644 --- a/config/c.conf +++ b/config/c.conf @@ -14,7 +14,11 @@ '\<(.*?)\>': BLUE # Assignments and comparisons # TODO: Add less than, greater than, not equal to, and struct pointer member access -'(?:\s|\b)(=|==|!=|<=|>=|\->)(\s|\b)' : CYAN +'(?:\s|\b)(=|==|!=|\<=|\>=|\->)(\s|\b)' : CYAN + +# Built-in boolean values, modifiers +'\b(static|const|true|false)\b': DARKBLUE + # Keywords '\b(if|else|while|do|for|return)\b': CYAN '(\n|^)(#ifdef|#ifndef|#define|#include)\b': CYAN diff --git a/config/go.conf b/config/go.conf index 35544c2..47230c2 100644 --- a/config/go.conf +++ b/config/go.conf @@ -1,7 +1,7 @@ # Priority decreases going downward ie. If two regexes match the same piece of # text, the one defined earlier will take precedence over the one defined later. # Comments -'//.*': GRAY +'//[^\n]*': GRAY '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/': GRAY # Numbers and special values '\b\-?[0-9]*\b': MAGENTA From d1a9f3f7269ebe5bd60cc7897b2f587e497b7bf0 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Thu, 24 Apr 2025 08:46:50 -0400 Subject: [PATCH 5/8] Updated coloring for Golang --- config/go.conf | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/config/go.conf b/config/go.conf index 47230c2..2f0b8fa 100644 --- a/config/go.conf +++ b/config/go.conf @@ -18,11 +18,9 @@ '(&&)|(\|\|)': CYAN # Keywords '\b(if|else|for|range|go|func|return|break|continue)\b': CYAN -'\b(import|var|const|type|struct)\b': CYAN +'\b(package|import|var|const|type|struct)\b': CYAN # Built-in Functions -'\b(panic|len)\b': DARKBLUE -# Functions from packages (package name and function name separated by dot) -'\b(\w*\.\w*)\b': DARKBLUE +'\b(panic|len)\b': GREEN # Data Types '\b(bool|byte|rune|string|interface|map|chan)\b': YELLOW '\b(u?int)(8|16|32|64)?\b': YELLOW From 8ae28cb3599e83ac2b5574fcf84fa869b92ac082 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Thu, 24 Apr 2025 08:47:20 -0400 Subject: [PATCH 6/8] Don't define any custom colors --- color.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/color.go b/color.go index 06fbb9c..ee198f5 100644 --- a/color.go +++ b/color.go @@ -38,13 +38,13 @@ var possibleColors map[string]color = map[string]color{ "GREEN": {"GREEN", colorData.New(colorData.FgGreen)}, "YELLOW": {"YELLOW", colorData.New(colorData.FgYellow)}, "BLUE": {"BLUE", colorData.New(colorData.FgBlue)}, - "MAGENTA": {"MAGENTA", colorData.New(38, 2, 254, 141, 255)}, + "MAGENTA": {"MAGENTA", colorData.New(colorData.FgMagenta)}, "CYAN": {"CYAN", colorData.New(colorData.FgCyan)}, "WHITE": {"WHITE", colorData.New(colorData.FgWhite)}, "GRAY": {"GRAY", colorData.New(colorData.FgWhite, colorData.Faint)}, // Last three numbers are RGB. Reference https://en.wikipedia.org/wiki/ANSI_escape_code for what the first two numbers mean. - "ORANGE": {"ORANGE", colorData.New(38, 2, 255, 153, 28)}, - "DARKBLUE": {"DARKBLUE", colorData.New(38, 2, 0, 112, 255)}, +// "ORANGE": {"ORANGE", colorData.New(38, 2, 255, 153, 28)}, +// "DARKBLUE": {"DARKBLUE", colorData.New(38, 2, 0, 112, 255)}, "NONE": {"NONE", colorData.New()}, } From 9ced9ab5cbd37aa910f6c55ee57fb28d5a528b0a Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Thu, 24 Apr 2025 08:58:24 -0400 Subject: [PATCH 7/8] Added underline 'color' --- config/ccat.colors | 1 + 1 file changed, 1 insertion(+) diff --git a/config/ccat.colors b/config/ccat.colors index 0319c4f..dec2854 100644 --- a/config/ccat.colors +++ b/config/ccat.colors @@ -5,3 +5,4 @@ PINK: 38 244 211 244 22 BOLD_WHITE: 38 -1 -1 -1 1 ITALIC_WHITE: 38 -1 -1 -1 3 +UNDERLINE_WHITE: 38 -1 -1 -1 4 From 720a01140c0f93088b7d25172b3173e76b820bab Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Thu, 24 Apr 2025 08:58:46 -0400 Subject: [PATCH 8/8] Updated some regexes for markdown coloring --- config/md.conf | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/config/md.conf b/config/md.conf index cbf4d32..7319a2c 100644 --- a/config/md.conf +++ b/config/md.conf @@ -1,7 +1,13 @@ # Priority decreases going downward ie. If two regexes match the same piece of # text, the one defined earlier will take precedence over the one defined later. # Headings -'##?#?#?#?#?.*': MAGENTA +'^#{1,6}.*?$': MAGENTA + +# Link text +'\[.*?\](?=\(.*?\))': UNDERLINE_WHITE + +# Link URL +'https?://\w+\.\w+.*?(?=\))': RED # Code blocks '```(.|\n)+?```': YELLOW