Compare commits

..

17 Commits

Author SHA1 Message Date
Aadhavan Srinivasan 43e145a6ec Throw error instead of panicking if regex doesn't compile
Aadhavan Srinivasan c887f2a0cc Don't panic; throw error instead
Aadhavan Srinivasan 9f6dcac314 Formatting change
Aadhavan Srinivasan 421e9b074e Merge pull request 'Use kleingrep's regex engine instead of stdlib' () from useMyEngine into master
Reviewed-on: 
Aadhavan Srinivasan 720a01140c Updated some regexes for markdown coloring
Aadhavan Srinivasan 9ced9ab5cb Added underline 'color'
Aadhavan Srinivasan 8ae28cb359 Don't define any custom colors
Aadhavan Srinivasan d1a9f3f726 Updated coloring for Golang
Aadhavan Srinivasan a1309af696 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.
Aadhavan Srinivasan 06fab2292c Enable RE_SINGLE_LINE flag when compiling regex, to allow newline to be treated like any other character
Aadhavan Srinivasan 19be04fd66 Enable multiline mode when compiling regex, because I read the whole file at once, rather than lline-by-line
Aadhavan Srinivasan 40858a673a Use my regex engine instead of the stdlib one
Aadhavan Srinivasan 23e9c5d58d Added SGR fields to the RGB struct for bold and italic;
allow RGB values to be -1 (default color);
allow underscore in color names
Aadhavan Srinivasan fd256bc7c7 Read the file from the embedded FS instead of the real FS
Aadhavan Srinivasan 565205fb5e Added some extended colors along with the config files
Aadhavan Srinivasan 33a3f7c937 Added a Markdown config file
Aadhavan Srinivasan 9fce9cf2a1 Construct config path using user's home directory, instead of trying to guess what the home directory is

@ -18,12 +18,17 @@ type color struct {
colorObj *colorData.Color colorObj *colorData.Color
} }
// A RGB represents a Red, Blue, Green trio of values. Each value is represented as // A RGB represents a Red, Blue, Green trio of values, along with SGR parameters.
// an int. // Each value is represented as an int. For info on SGR parameters, see:
// https://en.wikipedia.org/wiki/ANSI_escape_code#Select_Graphic_Rendition_parameters
// If 'red', 'green' and 'blue' are all -1, then the default terminal color is used.
// If some (but not all) of them are -1, an error is thrown.
type RGB struct { type RGB struct {
sgr1 int
red int red int
blue int blue int
green int green int
sgr2 int
} }
// The following is a list of all possible colors, stored in a map. // The following is a list of all possible colors, stored in a map.
@ -33,13 +38,13 @@ var possibleColors map[string]color = map[string]color{
"GREEN": {"GREEN", colorData.New(colorData.FgGreen)}, "GREEN": {"GREEN", colorData.New(colorData.FgGreen)},
"YELLOW": {"YELLOW", colorData.New(colorData.FgYellow)}, "YELLOW": {"YELLOW", colorData.New(colorData.FgYellow)},
"BLUE": {"BLUE", colorData.New(colorData.FgBlue)}, "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)}, "CYAN": {"CYAN", colorData.New(colorData.FgCyan)},
"WHITE": {"WHITE", colorData.New(colorData.FgWhite)}, "WHITE": {"WHITE", colorData.New(colorData.FgWhite)},
"GRAY": {"GRAY", colorData.New(colorData.FgWhite, colorData.Faint)}, "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. // 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)}, // "ORANGE": {"ORANGE", colorData.New(38, 2, 255, 153, 28)},
"DARKBLUE": {"DARKBLUE", colorData.New(38, 2, 0, 112, 255)}, // "DARKBLUE": {"DARKBLUE", colorData.New(38, 2, 0, 112, 255)},
"NONE": {"NONE", colorData.New()}, "NONE": {"NONE", colorData.New()},
} }
@ -76,63 +81,83 @@ func newColorMust(colorString string) color {
// characters. // characters.
func isValidColorName(colorName string) bool { func isValidColorName(colorName string) bool {
for _, ch := range colorName { for _, ch := range colorName {
if ch > 'Z' || ch < 'A' { if (ch > 'Z' || ch < 'A') && (ch != '_') {
return false return false
} }
} }
return true return true
} }
// stringToRGB takes a string representing an RGB trio. It constructs and RGB type and // stringToRGB takes a string representing an RGB five-tuple. It constructs and RGB type and
// returns it. Any errors encountered are returned. If an error is returned, it is safe to // returns it. Any errors encountered are returned. If an error is returned, it is safe to
// assume that the string doesn't represent an RGB trio. // assume that the string doesn't represent an RGB five-tuple.
func stringToRGB(rgbString string) (*RGB, error) { func stringToRGB(rgbString string) (*RGB, error) {
values := strings.Split(rgbString, " ") values := strings.Split(rgbString, " ")
// There must be three space-separated strings. // There must be three space-separated strings.
if len(values) != 3 { if len(values) != 5 {
// TODO: Instead of ignoring these errors and returning a generic error (as I do in the // TODO: Instead of ignoring these errors and returning a generic error (as I do in the
// callee), wrap the error returned from this function, inside the error returned by the callee. // callee), wrap the error returned from this function, inside the error returned by the callee.
return nil, fmt.Errorf("Error parsing RGB trio.") return nil, fmt.Errorf("Error parsing RGB five-tuple.")
} }
// If any of the strings doesn't represent an integer (or is out of bounds), return an error. // If any of the strings doesn't represent an integer (or is out of bounds), return an error.
// WARNING: LAZY CODE INCOMING // WARNING: LAZY CODE INCOMING
var toReturn RGB var toReturn RGB
var err error var err error
toReturn.red, err = strconv.Atoi(values[0]) toReturn.sgr1, err = strconv.Atoi(values[0])
if err != nil {
return nil, fmt.Errorf("Error parsing SGR1 integer: Invalid value.")
}
if toReturn.sgr1 < 0 || toReturn.sgr1 > 107 { // Maximum value for SGR values
return nil, fmt.Errorf("Error parsing SGR1 integer: Out-of-bounds.")
}
toReturn.red, err = strconv.Atoi(values[1])
if err != nil { if err != nil {
return nil, fmt.Errorf("Error parsing RED integer: Invalid value.") return nil, fmt.Errorf("Error parsing RED integer: Invalid value.")
} }
if toReturn.red < 0 || toReturn.red > 255 { if toReturn.red < -1 || toReturn.red > 255 {
return nil, fmt.Errorf("Error parsing RED integer: Out-of-bounds.") return nil, fmt.Errorf("Error parsing RED integer: Out-of-bounds.")
} }
toReturn.blue, err = strconv.Atoi(values[1]) toReturn.blue, err = strconv.Atoi(values[2])
if err != nil { if err != nil {
return nil, fmt.Errorf("Error parsing BLUE integer: Invalid value.") return nil, fmt.Errorf("Error parsing BLUE integer: Invalid value.")
} }
if toReturn.blue < 0 || toReturn.blue > 255 { if toReturn.blue < -1 || toReturn.blue > 255 {
return nil, fmt.Errorf("Error parsing BLUE integer: Out-of-bounds.") return nil, fmt.Errorf("Error parsing BLUE integer: Out-of-bounds.")
} }
toReturn.green, err = strconv.Atoi(values[2]) toReturn.green, err = strconv.Atoi(values[3])
if err != nil { if err != nil {
return nil, fmt.Errorf("Error parsing GREEN integer: Invalid value.") return nil, fmt.Errorf("Error parsing GREEN integer: Invalid value.")
} }
if toReturn.green < 0 || toReturn.green > 255 { if toReturn.green < -1 || toReturn.green > 255 {
return nil, fmt.Errorf("Error parsing GREEN integer: Out-of-bounds.") return nil, fmt.Errorf("Error parsing GREEN integer: Out-of-bounds.")
} }
toReturn.sgr2, err = strconv.Atoi(values[4])
if err != nil {
return nil, fmt.Errorf("Error parsing SGR2 integer: Invalid value.")
}
if toReturn.sgr2 < 0 || toReturn.sgr2 > 107 {
return nil, fmt.Errorf("Error parsing SGR2 integer: Out-of-bounds.")
}
if !(toReturn.red > 0 && toReturn.blue > 0 && toReturn.green > 0) &&
!(toReturn.red == -1 && toReturn.green == -1 && toReturn.blue == -1) {
return nil, fmt.Errorf("Error parsing color: All values must be positive or -1 for default terminal color.")
}
return &toReturn, nil return &toReturn, nil
} }
// loadColorsFromFile loads the colors defined in the given config file, and adds them to // loadColorsFromFile loads the colors defined in the given config file, and adds them to
// the possibleColors map. This allows the user to define custom colors at run-time. // the possibleColors map. This allows the user to define custom colors at run-time.
// The colors config file has the following syntax: // The colors config file has the following syntax:
// COLOR: <RED> <GREEN> <BLUE> // COLOR: <SGR1> <RED> <GREEN> <BLUE> <SGR2>
// //
// Note that the color must be capitalized (and not contain spaces), and the R, G and B // Note that the color must be capitalized (and not contain spaces), and the R, G and B
// values must be from 0 to 255. // values must be from -1 to 255 (-1 refers to the default terminal color, and all three values
// must be -1 for this to work).
func loadColorsFromFile(filepath string) error { func loadColorsFromFile(filepath string) error {
data, err := os.ReadFile(filepath) data, err := os.ReadFile(filepath)
if err != nil { if err != nil {
panic(err) return err
} }
// Read color config file into a MapSlice // Read color config file into a MapSlice
tempMapSlice := yaml.MapSlice{} tempMapSlice := yaml.MapSlice{}
@ -151,8 +176,28 @@ func loadColorsFromFile(filepath string) error {
// If we haven't returned an error yet, the color must be valid. // If we haven't returned an error yet, the color must be valid.
// Add it to the map. colorData.New() expects values of type colorData.Attribute, // Add it to the map. colorData.New() expects values of type colorData.Attribute,
// so we must cast our RGB values accordingly. // so we must cast our RGB values accordingly.
possibleColors[item.Key.(string)] = color{item.Key.(string), colorData.New(38, 2, colorData.Attribute(rgb.red), colorData.Attribute(rgb.blue), colorData.Attribute(rgb.green))} // First, check if one of the color values is -1. If it is, they must all be negative (based
// on the check in 'stringToRGB()'). If this is the case, don't put the color values.
if rgb.red == -1 {
possibleColors[item.Key.(string)] = color{
item.Key.(string),
colorData.New(
colorData.Attribute(rgb.sgr2),
),
}
} else {
possibleColors[item.Key.(string)] = color{
item.Key.(string),
colorData.New(
colorData.Attribute(rgb.sgr1),
2,
colorData.Attribute(rgb.red),
colorData.Attribute(rgb.blue),
colorData.Attribute(rgb.green),
colorData.Attribute(rgb.sgr2),
),
}
}
} }
return nil return nil

@ -3,14 +3,16 @@ package main
import ( import (
"embed" "embed"
"errors" "errors"
"gitea.twomorecents.org/Rockingcool/ccat/stack" "fmt"
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"strings" "strings"
"gitea.twomorecents.org/Rockingcool/ccat/stack"
"gitea.twomorecents.org/Rockingcool/kleingrep/regex"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -49,7 +51,7 @@ func generateDefaultConfigs(configOutputPath string) error {
relPath, _ := filepath.Rel("config", path) relPath, _ := filepath.Rel("config", path)
dstPath := filepath.Join(configOutputPath, relPath) // Destination path dstPath := filepath.Join(configOutputPath, relPath) // Destination path
data, err := os.ReadFile(path) data, err := storedConfigs.ReadFile(path)
if err != nil { if err != nil {
return err return err
} }
@ -87,13 +89,16 @@ func loadConfig(configFilename string) (stack.Stack[regColor], error) {
// returned. // 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 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 { for _, item := range tempMapSlice {
re := regexp.MustCompile(item.Key.(string)) re, err := regex.Compile(item.Key.(string), regex.RE_MULTILINE, regex.RE_SINGLE_LINE)
if err != nil {
return *stack.NewStack[regColor](0), fmt.Errorf("%v: '%s'", err, item.Key.(string))
}
clr, err := newColor(item.Value.(string)) clr, err := newColor(item.Value.(string))
if err != nil { if err != nil {
return *stack.NewStack[regColor](0), err return *stack.NewStack[regColor](0), err
} }
// If we got past the errors, then the color _must_ be valid. // 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 return *regColorStack, nil

@ -14,7 +14,11 @@
'\<(.*?)\>': BLUE '\<(.*?)\>': BLUE
# Assignments and comparisons # Assignments and comparisons
# TODO: Add less than, greater than, not equal to, and struct pointer member access # 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 # Keywords
'\b(if|else|while|do|for|return)\b': CYAN '\b(if|else|while|do|for|return)\b': CYAN
'(\n|^)(#ifdef|#ifndef|#define|#include)\b': CYAN '(\n|^)(#ifdef|#ifndef|#define|#include)\b': CYAN

@ -0,0 +1,8 @@
# The first and last fields are SGR values, used to control things
# like bold and italic text. If you're unsure what to put, use '38'
# for the first one and '22' for the last one, for normal text.
# The last three values are R G B values.
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

@ -1,7 +1,7 @@
# Priority decreases going downward ie. If two regexes match the same piece of # 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. # text, the one defined earlier will take precedence over the one defined later.
# Comments # Comments
'//.*': GRAY '//[^\n]*': GRAY
'/\*[^*]*\*+(?:[^/*][^*]*\*+)*/': GRAY '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/': GRAY
# Numbers and special values # Numbers and special values
'\b\-?[0-9]*\b': MAGENTA '\b\-?[0-9]*\b': MAGENTA
@ -14,15 +14,13 @@
"'(.)'": BLUE "'(.)'": BLUE
"'\\\\(.)'": BLUE # The escape backslash needs to be escaped as well "'\\\\(.)'": BLUE # The escape backslash needs to be escaped as well
# Assignments and comparisons # Assignments and comparisons
'(?:\s|\b)(=|==|!=|<=|>=)(\s|\b)' : CYAN '(?:\s|\b)(=|==|!=|\<=|\>=)(\s|\b)' : CYAN
'(&&)|(\|\|)': CYAN '(&&)|(\|\|)': CYAN
# Keywords # Keywords
'\b(if|else|for|range|go|func|return|break|continue)\b': CYAN '\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 # Built-in Functions
'\b(panic|len)\b': DARKBLUE '\b(panic|len)\b': GREEN
# Functions from packages (package name and function name separated by dot)
'\b(\w*\.\w*)\b': DARKBLUE
# Data Types # Data Types
'\b(bool|byte|rune|string|interface|map|chan)\b': YELLOW '\b(bool|byte|rune|string|interface|map|chan)\b': YELLOW
'\b(u?int)(8|16|32|64)?\b': YELLOW '\b(u?int)(8|16|32|64)?\b': YELLOW

@ -0,0 +1,19 @@
# 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
'^#{1,6}.*?$': MAGENTA
# Link text
'\[.*?\](?=\(.*?\))': UNDERLINE_WHITE
# Link URL
'https?://\w+\.\w+.*?(?=\))': RED
# Code blocks
'```(.|\n)+?```': YELLOW
# Bold text
'\b__[^_]+?__\b': BOLD_WHITE
# Italic text
'\b_[^_]+?_\b': ITALIC_WHITE

@ -1,11 +1,14 @@
module gitea.twomorecents.org/Rockingcool/ccat module gitea.twomorecents.org/Rockingcool/ccat
go 1.22.5 go 1.23.1
toolchain go1.24.2
require ( 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-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // 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 gopkg.in/yaml.v2 v2.4.0 // indirect
) )

@ -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 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= 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 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= 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 h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= 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= 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.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= 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.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/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 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"math" "math"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
) )
@ -78,11 +77,11 @@ func main() {
flag.Parse() flag.Parse()
// Check if config exists. If it doesn't, generate the config files. // Check if config exists. If it doesn't, generate the config files.
currentUser, err := user.Current() // Get current user, to determine config path userHomeDir, err := os.UserHomeDir() // Get current user's home directory, to construct config path
if err != nil { if err != nil {
panic(err) printErrAndExit("Unable to retrieve user home directory")
} }
configPath := filepath.Join("/home/" + currentUser.Username + "/.config/ccat/") configPath := filepath.Join(userHomeDir + "/.config/ccat/")
if _, err := os.Stat(configPath); os.IsNotExist(err) { if _, err := os.Stat(configPath); os.IsNotExist(err) {
generateDefaultConfigs(configPath) generateDefaultConfigs(configPath)
} }
@ -108,7 +107,7 @@ func main() {
// the program. // the program.
finfo, err := os.Stat(fileName) finfo, err := os.Stat(fileName)
if err != nil { if err != nil {
panic(err) printErrAndExit("Unable to read file")
} }
if finfo.Size() == 0 { if finfo.Size() == 0 {
os.Exit(0) os.Exit(0)
@ -158,13 +157,13 @@ func main() {
clr := regclr.clr clr := regclr.clr
// Returns an int double-slice, where each slice contains the start and end indices // 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'. // 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 { if matches == nil {
continue continue
} }
// For each match, apply the corresponding color to all characters in the match. // For each match, apply the corresponding color to all characters in the match.
for _, match := range matches { for _, match := range matches {
units = applyColor(units, match[0], match[1], clr) units = applyColor(units, match[0].StartIdx, match[0].EndIdx, clr)
} }
} }

@ -1,10 +1,10 @@
package main package main
import "regexp" import "gitea.twomorecents.org/Rockingcool/kleingrep/regex"
// A regColor is a regex-color pair. The config file is read // A regColor is a regex-color pair. The config file is read
// into a stack of this data type. // into a stack of this data type.
type regColor struct { type regColor struct {
re *regexp.Regexp re *regex.Reg
clr color clr color
} }

Loading…
Cancel
Save