Compare commits
10 Commits
2a007ba56c
...
66b227fb63
Author | SHA1 | Date | |
---|---|---|---|
66b227fb63 | |||
373c3c3385 | |||
624ea575cc | |||
ffae02b62b | |||
81b976bcc7 | |||
0b67cb317e | |||
098f7fe01b | |||
76fc61a19d | |||
94d1d99af2 | |||
7feac0b5f7 |
7
color.go
7
color.go
@@ -26,8 +26,9 @@ var possibleColors map[string]color = map[string]color{
|
|||||||
"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)},
|
||||||
"NONE": {"NONE", colorData.New()},
|
"DARKBLUE": {"DARKBLUE", colorData.New(38, 2, 0, 112, 255)},
|
||||||
|
"NONE": {"NONE", colorData.New()},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply the given color 'clr' to all units in 'units', within the indices
|
// Apply the given color 'clr' to all units in 'units', within the indices
|
||||||
@@ -52,7 +53,7 @@ func newColor(colorString string) (color, error) {
|
|||||||
// newColorMust is similar to newColor, but prints an error and exits if the given color isn't valid.
|
// newColorMust is similar to newColor, but prints an error and exits if the given color isn't valid.
|
||||||
func newColorMust(colorString string) color {
|
func newColorMust(colorString string) color {
|
||||||
if clr, err := newColor(colorString); err != nil {
|
if clr, err := newColor(colorString); err != nil {
|
||||||
printAndExit(err.Error())
|
printErrAndExit(err.Error())
|
||||||
panic(err) // NEVER REACHED
|
panic(err) // NEVER REACHED
|
||||||
} else {
|
} else {
|
||||||
return clr
|
return clr
|
||||||
|
63
config.go
63
config.go
@@ -2,13 +2,76 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"ccat/stack"
|
"ccat/stack"
|
||||||
|
"embed"
|
||||||
|
"errors"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed config
|
||||||
|
var storedConfigs embed.FS // Embed the folder containing config files
|
||||||
|
|
||||||
|
func runningOnWindows() bool {
|
||||||
|
return runtime.GOOS == "windows"
|
||||||
|
}
|
||||||
|
|
||||||
|
// generateDefaultConfigs is used to generate a folder of default config files
|
||||||
|
// for common languages. These default config files are embedded into the program, and will
|
||||||
|
// be outputted into a directory.
|
||||||
|
//
|
||||||
|
// If there is an error encountered, the error is returned.
|
||||||
|
func generateDefaultConfigs() error {
|
||||||
|
|
||||||
|
var configOutputPath string // Location of config files, depends on OS
|
||||||
|
if runningOnWindows() {
|
||||||
|
configOutputPath = "%APPDATA%\\ccat"
|
||||||
|
} else {
|
||||||
|
currentUser, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
configOutputPath = filepath.Join("/home/" + currentUser.Username + "/.config/ccat/")
|
||||||
|
}
|
||||||
|
err := os.MkdirAll(configOutputPath, 0755)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsExist(err) {
|
||||||
|
return errors.New("Directory already exists.")
|
||||||
|
} else {
|
||||||
|
return errors.New("Unable to create directory.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy each folder from the embedded filesystem, into the destination path
|
||||||
|
err = fs.WalkDir(storedConfigs, "config", func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if d.IsDir() { // Skip directories
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
relPath, _ := filepath.Rel("config", path)
|
||||||
|
dstPath := filepath.Join(configOutputPath, relPath) // Destination path
|
||||||
|
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.WriteFile(dstPath, data, 0644); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// loadConfig takes in the filename of a config file. It reads the file,
|
// loadConfig takes in the filename of a config file. It reads the file,
|
||||||
// and returns a stack of RegColors, with the item at the bottom being the one that
|
// and returns a stack of RegColors, with the item at the bottom being the one that
|
||||||
// was read first. This ensures that, _when accessing the RegColors in the stack, the last
|
// was read first. This ensures that, _when accessing the RegColors in the stack, the last
|
||||||
|
30
config/go.conf
Normal file
30
config/go.conf
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# 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
|
||||||
|
'/\*[^*]*\*+(?:[^/*][^*]*\*+)*/': GRAY
|
||||||
|
# Numbers and special values
|
||||||
|
'\b\-?[0-9]*\b': MAGENTA
|
||||||
|
'\b(true|false)\b': MAGENTA
|
||||||
|
'\b(nil)\b': MAGENTA
|
||||||
|
# Strings in double quotes and backticks
|
||||||
|
'"(.*?)"': BLUE
|
||||||
|
'`(.*?)`': BLUE
|
||||||
|
# Bytes / Runes
|
||||||
|
"'(.)'": BLUE
|
||||||
|
"'\\\\(.)'": BLUE # The escape backslash needs to be escaped as well
|
||||||
|
# Assignments and comparisons
|
||||||
|
'(?:\s|\b)(=|==|!=|<=|>=)(\s|\b)' : CYAN
|
||||||
|
'(&&)|(\|\|)': CYAN
|
||||||
|
# Keywords
|
||||||
|
'\b(if|else|for|range|go|func|return|break|continue)\b': CYAN
|
||||||
|
'\b(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
|
||||||
|
# Data Types
|
||||||
|
'\b(bool|byte|rune|string|interface|map|chan)\b': YELLOW
|
||||||
|
'\b(u?int)(8|16|32|64)?\b': YELLOW
|
||||||
|
'\b(float)(32|64)\b': YELLOW
|
||||||
|
'\b(complex)(64|128)\b': YELLOW
|
@@ -5,7 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func printAndExit(errorStr string) {
|
func printErrAndExit(errorStr string) {
|
||||||
fmt.Printf("ERROR: %s\n", errorStr)
|
fmt.Printf("ERROR: %s\n", errorStr)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
96
main.go
96
main.go
@@ -1,9 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +19,7 @@ func fileExists(filename string) bool {
|
|||||||
} else if errors.Is(err, os.ErrNotExist) {
|
} else if errors.Is(err, os.ErrNotExist) {
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
printAndExit(err.Error())
|
printErrAndExit(err.Error())
|
||||||
return false // NEVER REACHED
|
return false // NEVER REACHED
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,7 +28,7 @@ func fileExists(filename string) bool {
|
|||||||
// the file doesn't exist.
|
// the file doesn't exist.
|
||||||
func mustExist(filename string) {
|
func mustExist(filename string) {
|
||||||
if fileExists(filename) != true {
|
if fileExists(filename) != true {
|
||||||
printAndExit(os.ErrNotExist.Error())
|
printErrAndExit(os.ErrNotExist.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,12 +36,12 @@ func mustExist(filename string) {
|
|||||||
// It returns two values: the first is true if the config file exists.
|
// It returns two values: the first is true if the config file exists.
|
||||||
// If it does, the second value is the config filename.
|
// If it does, the second value is the config filename.
|
||||||
// If it doesn't, the second value is blank and can be ignored.
|
// If it doesn't, the second value is blank and can be ignored.
|
||||||
func getConfig(extension string) (bool, string) {
|
func getConfig(configPath, extension string) (bool, string) {
|
||||||
if extension == "" {
|
if extension == "" {
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
// Assuming the file has an extension
|
// Assuming the file has an extension
|
||||||
fileName := "config/" + extension[1:] + ".conf"
|
fileName := filepath.Join(configPath, extension[1:]+".conf")
|
||||||
if exists := fileExists(fileName); exists == false {
|
if exists := fileExists(fileName); exists == false {
|
||||||
return false, ""
|
return false, ""
|
||||||
} else {
|
} else {
|
||||||
@@ -52,41 +56,87 @@ func printFile(fileName string) {
|
|||||||
mustExist(fileName)
|
mustExist(fileName)
|
||||||
data, err := os.ReadFile(fileName)
|
data, err := os.ReadFile(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printAndExit(err.Error())
|
printErrAndExit(err.Error())
|
||||||
}
|
}
|
||||||
fmt.Print(string(data))
|
fmt.Print(string(data))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// computeLineNumDigits computes the number of digits in the number of lines
|
||||||
|
// in the given byte array.
|
||||||
|
func computeLineNumDigits(data []byte) int {
|
||||||
|
numLines := bytes.Count(data, []byte{'\n'}) + 1
|
||||||
|
return int(math.Round(math.Log10(float64(numLines))))
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
disableColorFlag := flag.Bool("d", false, "Disable color")
|
||||||
|
lineNumberFlag := flag.Bool("n", false, "Print line numbers")
|
||||||
|
// Used only if lineNumberFlag is true
|
||||||
|
var lineNumDigits int
|
||||||
|
var lineNum int
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
// Check if config exists. If it doesn't, generate the config files.
|
||||||
|
var configPath string // Location of config files, depends on OS
|
||||||
|
if runningOnWindows() {
|
||||||
|
configPath = "%APPDATA%\\ccat"
|
||||||
|
} else {
|
||||||
|
currentUser, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
configPath = filepath.Join("/home/" + currentUser.Username + "/.config/ccat/")
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||||
|
generateDefaultConfigs()
|
||||||
|
}
|
||||||
|
|
||||||
// Check if user has provided a file name
|
// Check if user has provided a file name
|
||||||
if len(os.Args) != 2 {
|
if len(flag.Args()) < 1 {
|
||||||
printAndExit("Invalid number of arguments")
|
printErrAndExit("No File specified")
|
||||||
}
|
}
|
||||||
fileName := os.Args[1]
|
fileName := flag.Args()[0]
|
||||||
|
|
||||||
|
// Check if file exists.
|
||||||
mustExist(fileName)
|
mustExist(fileName)
|
||||||
|
|
||||||
extension := filepath.Ext(fileName)
|
extension := filepath.Ext(fileName)
|
||||||
configExists, configFilename := getConfig(extension)
|
configExists, configFilename := getConfig(configPath, extension)
|
||||||
// If the given file has no corresponding config, print it out
|
// If the given file has no corresponding config, print the file out and exit.
|
||||||
// and exit.
|
|
||||||
if configExists == false {
|
if configExists == false {
|
||||||
printFile(fileName)
|
printFile(fileName)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// To save computing time, determine here if the file is empty. If it is, exit
|
||||||
|
// the program.
|
||||||
|
finfo, err := os.Stat(fileName)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if finfo.Size() == 0 {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assuming the file is not empty...
|
||||||
// If the given file has a config, load the config into a stack of regColors.
|
// If the given file has a config, load the config into a stack of regColors.
|
||||||
regColorStack, err := loadConfig(configFilename)
|
regColorStack, err := loadConfig(configFilename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printAndExit(err.Error())
|
printErrAndExit(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load the input file into a colorunit slice (units) and a byte slice (data)
|
// Load the input file into a colorunit slice (units) and a byte slice (data)
|
||||||
units, data, err := loadInputFile(fileName)
|
units, data, err := loadInputFile(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
printAndExit(err.Error())
|
printErrAndExit(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the '-n' flag is set, compute the number of digits in the number of lines
|
||||||
|
// in the file, to determine the padding for the line numbers.
|
||||||
|
if *lineNumberFlag {
|
||||||
|
lineNumDigits = computeLineNumDigits(data)
|
||||||
|
}
|
||||||
// For each regular expression in the stack, apply it to the byte slice. Find
|
// For each regular expression in the stack, apply it to the byte slice. Find
|
||||||
// the first and last index of all matches of the regex. Then apply the corresponding color
|
// the first and last index of all matches of the regex. Then apply the corresponding color
|
||||||
// to every character within these indices.
|
// to every character within these indices.
|
||||||
@@ -94,7 +144,9 @@ func main() {
|
|||||||
// The infinite for loop exists, because I couldn't figure out a way to pop an element from
|
// The infinite for loop exists, because I couldn't figure out a way to pop an element from
|
||||||
// the stack inside the 'for' statement. The loop exits when the 'pop' call returns 'false',
|
// the stack inside the 'for' statement. The loop exits when the 'pop' call returns 'false',
|
||||||
// indicating that the stack is empty.
|
// indicating that the stack is empty.
|
||||||
for {
|
//
|
||||||
|
// The loop is also only run if the 'disable color' flag is not set.
|
||||||
|
for *disableColorFlag == false {
|
||||||
regclr, ok := regColorStack.Pop()
|
regclr, ok := regColorStack.Pop()
|
||||||
// regColorStack.Pop() returns false when there are no more elements to pop
|
// regColorStack.Pop() returns false when there are no more elements to pop
|
||||||
if ok != true {
|
if ok != true {
|
||||||
@@ -115,7 +167,21 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// After all possible regexes have been matched, print out the contents of 'units'.
|
// After all possible regexes have been matched, print out the contents of 'units'.
|
||||||
for _, unit := range units {
|
|
||||||
|
// If the line number flag is set, initialize the lineNum variable and print the first line number
|
||||||
|
// with the appropriate padding.
|
||||||
|
if *lineNumberFlag {
|
||||||
|
lineNum = 1
|
||||||
|
fmt.Printf(" %*d ", lineNumDigits, lineNum)
|
||||||
|
lineNum++
|
||||||
|
}
|
||||||
|
for idx, unit := range units {
|
||||||
unit.print()
|
unit.print()
|
||||||
|
// If the flag is set and we encounter a newline (and the newline isn't a trailing newline),
|
||||||
|
// then print the next line number.
|
||||||
|
if *lineNumberFlag && unit.ch == '\n' && idx != len(units)-1 {
|
||||||
|
fmt.Printf(" %*d ", lineNumDigits, lineNum)
|
||||||
|
lineNum++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user