Compare commits
5 Commits
5bb51fb90c
...
v0.0.1
Author | SHA1 | Date | |
---|---|---|---|
122cd5ed04 | |||
3b8bcb4c8a | |||
e7e7a247d8 | |||
eb2a0a9122 | |||
46e3e9da85 |
27
README.md
27
README.md
@@ -5,8 +5,8 @@ ccat is a file printing tool (like 'cat') which uses Regular Expressions to enab
|
|||||||
---
|
---
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
- Support for 11 colors: Red, Blue, Green, Magenta, Cyan, Black, White, Yellow, Gray, Orange and Dark Blue.
|
- 11 colors are defined out-of-the-box: RED, BLUE, GREEN, MAGENTA, CYAN, BLACK, WHITE, YELLOW, GRAY, ORANGE and DARKBLUE.
|
||||||
- Adding more colors involves adding a line of code, then recompiling.
|
- Support for defining custom colors via the `ccat.colors` file.
|
||||||
- Regex-color mappings are stored in configuration files.
|
- Regex-color mappings are stored in configuration files.
|
||||||
- Uses the file extension to determine which configuration file to use.
|
- Uses the file extension to determine which configuration file to use.
|
||||||
- Highly extensible - to add a config file for an specific file type, name the file `<extension>.conf`.
|
- Highly extensible - to add a config file for an specific file type, name the file `<extension>.conf`.
|
||||||
@@ -21,11 +21,23 @@ If you have the `go` command installed, run `make` after cloning the repository.
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
### Supported Languages
|
||||||
|
|
||||||
|
The following languages have config files included by default:
|
||||||
|
|
||||||
|
- C
|
||||||
|
- Go
|
||||||
|
-
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
### Getting Started
|
### Getting Started
|
||||||
The config files are embedded within the binary. They will automatically be installed to the correct location (`%APPDATA/ccat` on Windows, `~/.config/ccat` on UNIX) when the program is first run.
|
The config files are embedded within the binary. They will automatically be installed to the correct location (`%APPDATA/ccat` on Windows, `~/.config/ccat` on UNIX) when the program is first run.
|
||||||
|
|
||||||
As written above, if provided a file with extension `.example`, the program will look for the config file named `example.conf`. If such a file doesn't exist, the file is printed out without any highlighting.
|
As written above, if provided a file with extension `.example`, the program will look for the config file named `example.conf`. If such a file doesn't exist, the file is printed out without any highlighting.
|
||||||
|
|
||||||
|
For example, if you want to create syntax highlighting for Java, create a file named `java.conf` in your config directory. In this file, include regular-expressions for each of the langauges's keywords, and provide a corresponding color. Use the provided `c.conf` and `go.conf` files as a starting point.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### Config Files
|
### Config Files
|
||||||
@@ -38,7 +50,16 @@ Note that the regex must be enclosed in double quotes, and the color must be cap
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
### Custom Colors
|
||||||
|
|
||||||
|
To define a color of your own, create a file named `ccat.colors` in the config directory (mentioned above). The syntax of this file is the following:
|
||||||
|
|
||||||
|
`COLOR: <red> <green> <blue>`
|
||||||
|
|
||||||
|
Note that the color name must be capitalized (and shouldn't contain spaces). The RGB values must each be from 0 to 255.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
### TODO:
|
### TODO:
|
||||||
- Allow user to define colors at runtime by reading RGB values from a config file.
|
|
||||||
- Allow users to provide a config file in the command-line, overriding the extension-based config file.
|
- Allow users to provide a config file in the command-line, overriding the extension-based config file.
|
||||||
- Provide releases.
|
- Provide releases.
|
||||||
|
98
color.go
98
color.go
@@ -2,8 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
colorData "github.com/fatih/color"
|
colorData "github.com/fatih/color"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A color represents a possible color, which text can be printed out in.
|
// A color represents a possible color, which text can be printed out in.
|
||||||
@@ -14,6 +18,14 @@ type color struct {
|
|||||||
colorObj *colorData.Color
|
colorObj *colorData.Color
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A RGB represents a Red, Blue, Green trio of values. Each value is represented as
|
||||||
|
// an int.
|
||||||
|
type RGB struct {
|
||||||
|
red int
|
||||||
|
blue int
|
||||||
|
green 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.
|
||||||
var possibleColors map[string]color = map[string]color{
|
var possibleColors map[string]color = map[string]color{
|
||||||
"BLACK": {"BLACK", colorData.New(colorData.FgBlack)},
|
"BLACK": {"BLACK", colorData.New(colorData.FgBlack)},
|
||||||
@@ -59,3 +71,89 @@ func newColorMust(colorString string) color {
|
|||||||
return clr
|
return clr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isValidColorName returns true if the given string only contains uppercase alphabetic
|
||||||
|
// characters.
|
||||||
|
func isValidColorName(colorName string) bool {
|
||||||
|
for _, ch := range colorName {
|
||||||
|
if ch > 'Z' || ch < 'A' {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// stringToRGB takes a string representing an RGB trio. It constructs and RGB type and
|
||||||
|
// 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.
|
||||||
|
func stringToRGB(rgbString string) (*RGB, error) {
|
||||||
|
values := strings.Split(rgbString, " ")
|
||||||
|
// There must be three space-separated strings.
|
||||||
|
if len(values) != 3 {
|
||||||
|
// 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.
|
||||||
|
return nil, fmt.Errorf("Error parsing RGB trio.")
|
||||||
|
}
|
||||||
|
// If any of the strings doesn't represent an integer (or is out of bounds), return an error.
|
||||||
|
// WARNING: LAZY CODE INCOMING
|
||||||
|
var toReturn RGB
|
||||||
|
var err error
|
||||||
|
toReturn.red, err = strconv.Atoi(values[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error parsing RED integer: Invalid value.")
|
||||||
|
}
|
||||||
|
if toReturn.red < 0 || toReturn.red > 255 {
|
||||||
|
return nil, fmt.Errorf("Error parsing RED integer: Out-of-bounds.")
|
||||||
|
}
|
||||||
|
toReturn.blue, err = strconv.Atoi(values[1])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error parsing BLUE integer: Invalid value.")
|
||||||
|
}
|
||||||
|
if toReturn.blue < 0 || toReturn.blue > 255 {
|
||||||
|
return nil, fmt.Errorf("Error parsing BLUE integer: Out-of-bounds.")
|
||||||
|
}
|
||||||
|
toReturn.green, err = strconv.Atoi(values[2])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error parsing GREEN integer: Invalid value.")
|
||||||
|
}
|
||||||
|
if toReturn.green < 0 || toReturn.green > 255 {
|
||||||
|
return nil, fmt.Errorf("Error parsing GREEN integer: Out-of-bounds.")
|
||||||
|
}
|
||||||
|
return &toReturn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 colors config file has the following syntax:
|
||||||
|
// COLOR: <RED> <GREEN> <BLUE>
|
||||||
|
//
|
||||||
|
// Note that the color must be capitalized (and not contain spaces), and the R, G and B
|
||||||
|
// values must be from 0 to 255.
|
||||||
|
func loadColorsFromFile(filepath string) error {
|
||||||
|
data, err := os.ReadFile(filepath)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
// Read color config file into a MapSlice
|
||||||
|
tempMapSlice := yaml.MapSlice{}
|
||||||
|
if err := yaml.Unmarshal(data, &tempMapSlice); err != nil {
|
||||||
|
return fmt.Errorf("Unable to read color config file: %s", filepath)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range tempMapSlice {
|
||||||
|
if !(isValidColorName(item.Key.(string))) {
|
||||||
|
return fmt.Errorf("Invalid color name: %s", item.Key.(string))
|
||||||
|
}
|
||||||
|
var rgb *RGB
|
||||||
|
if rgb, err = stringToRGB(item.Value.(string)); err != nil {
|
||||||
|
return fmt.Errorf("Invalid RGB trio: %s", item.Value.(string))
|
||||||
|
}
|
||||||
|
// 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,
|
||||||
|
// 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))}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
7
main.go
7
main.go
@@ -120,6 +120,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Assuming the file is not empty...
|
// Assuming the file is not empty...
|
||||||
|
// If a ccat.colors file exists in the config directory, load all the colors in it
|
||||||
|
if fileExists(filepath.Join(configPath, "ccat.colors")) {
|
||||||
|
err := loadColorsFromFile(filepath.Join(configPath, "ccat.colors"))
|
||||||
|
if err != nil {
|
||||||
|
printErrAndExit(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
// 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 {
|
||||||
|
Reference in New Issue
Block a user