Documentation
¶
Overview ¶
Package chromaterm colours terminal output by keyword. It is a Go port of ChromaTerm 0.10.7 (https://github.com/hSaria/ChromaTerm) that keeps its config format, default rules, colour handling and output buffering, so an existing ~/.chromaterm.yml works unchanged. No Python is involved.
Highlight text with a Config:
cfg := chromaterm.NewConfig()
cfg.Load(chromaterm.DefaultConfig, true) // true for 24-bit colour
fmt.Println(cfg.Highlight("ping 10.0.0.1 failed"))
Highlight a stream as it arrives, waiting briefly for cut-off lines the way ChromaTerm does, with (&Stream{Config: cfg}).Run(in, out). To colour an interactive program (ssh, a shell), run it with RunInPTY: the program gets its own PTY, keystrokes pass through, and its output is highlighted. Unlike ChromaTerm, output on the alternate screen (vim, less, htop) is left alone.
Rules use ChromaTerm's (Python) regular expression syntax, including lookaround and backreferences, through github.com/dlclark/regexp2.
Example ¶
package main
import (
"fmt"
"strings"
chromaterm "github.com/jurger/chromaterm-go"
)
func main() {
cfg := chromaterm.NewConfig()
cfg.Load(chromaterm.DefaultConfig, true)
out := cfg.Highlight("link to 10.0.0.1 is down")
fmt.Println(strings.ReplaceAll(out, "\x1b", `\e`))
}
Output: link to \e[38;2;0;224;209m10.0.0.1\e[39m is \e[38;2;199;24;0mdown\e[39m
Index ¶
Examples ¶
Constants ¶
const PTYSupported = true
PTYSupported reports whether RunInPTY works on this platform.
Variables ¶
var DefaultConfig string
DefaultConfig is ChromaTerm's default palette and rules.
Functions ¶
func ExitCode ¶
ExitCode turns the error from running a program into its exit code: 0 for success, the program's code when it exited, 1 when it was killed by a signal, and 127 when it could not run at all.
func RunInPTY ¶
RunInPTY runs program in a new PTY and returns its exit code. Input from in is copied raw (in is switched to raw mode if it is a terminal), output is highlighted with cfg on its way to out, and in's window size follows SIGWINCH. An error means the PTY could not be started; run the program without highlighting then.
Types ¶
type Color ¶
type Color struct {
Spec string
// contains filtered or unexported fields
}
Color is a parsed ChromaTerm colour such as "f#ff0000 bold" or "b.status-1".
type Config ¶
type Config struct {
Rules []*Rule
// contains filtered or unexported fields
}
Config highlights data with its rules. It remembers the last colour of each type it emitted, so a highlight that ends in a later chunk restores the colour the program had set instead of the terminal default.
func (*Config) Highlight ¶
Highlight returns data with the rules' colours added. Existing colours are stripped before matching so they don't interfere, then put back.
func (*Config) Load ¶
Load parses a ChromaTerm YAML config (palette and rules). Rules that fail to parse are skipped and reported in the returned errors; a config that cannot be read at all leaves c unchanged.
Example ¶
package main
import (
"fmt"
chromaterm "github.com/jurger/chromaterm-go"
)
func main() {
cfg := chromaterm.NewConfig()
errs := cfg.Load(`
palette:
alert: "#ff0000"
rules:
- description: Failed logins
regex: (?i)failed password for (\w+)
color:
0: f.alert
1: bold
`, true)
fmt.Println(len(cfg.Rules), errs)
}
Output: 1 []
type Rule ¶
type Rule struct {
Description string
Regex string
Exclusive bool
// contains filtered or unexported fields
}
Rule highlights the matches of a regular expression. Colours are keyed by regex group; group 0 colours the whole match.
type Stream ¶
type Stream struct {
Config *Config
// contains filtered or unexported fields
}
Stream highlights everything read from in and writes it to out, until in reaches EOF. It is a port of ChromaTerm's process_input for a program run in a PTY: complete chunks are written at once, and the trailing chunk waits briefly for the rest of its line so a match is not split across reads.