Documentation
¶
Overview ¶
Package theme holds the colors the packages of this module paint with.
A Style is a set of SGR parameters ("1;38;5;141"); a Theme assigns one to each role text plays on screen — a title, a muted note, the selected row, an error. Packages read their colors from the active theme, so a tool that wants its own palette calls Use once at start-up, usually with a copy of Default that changes a few roles. Whether anything is painted at all is decided by term.Colored, not by the theme; Style.For and Theme.For ask it for a given writer.
Theme.PaintDiff paints a unified diff with the theme's diff roles, and DiffScanner tells its lines apart for code that shows only some of them. Both take the diff as it is and take nothing out of it: a diff that does not come from the program itself goes through term.Plain first. Theme.PaintJSON paints encoded JSON with the theme's JSON roles in the same way.
Hashed gives a name a style of its own that stays the same across rows and runs, such as a color per author, picked from NamePalette or a palette of the caller's.
Index ¶
Examples ¶
Constants ¶
const Reset = "\x1b[0m"
Reset ends every SGR attribute.
Variables ¶
var Default = Theme{ Title: Bold, Subject: Bold, Header: Join(Bold, Fg256(252)), Border: Fg256(240), Muted: Fg256(244), Status: Fg256(244), Qualifier: Fg256(250), Value: Fg256(141), Detail: Fg256(117), Selected: Join(Bold, Fg256(141)), SelectedQualifier: Fg256(146), Action: Fg256(73), SelectedAction: Join(Bold, Fg256(79)), Accent: Fg256(214), Flag: Fg256(203), Info: Fg256(117), Success: Fg256(114), Warning: Fg256(221), Error: Fg256(203), Added: Fg256(114), Removed: Fg256(203), DiffHeader: Bold, DiffHunk: Fg256(117), JSONKey: Fg256(116), JSONString: Fg256(120), JSONNumber: Fg256(118), JSONBool: Fg256(156), JSONNull: Fg256(242), }
Default is the theme the packages use unless Use installs another. It sticks to the 256-color palette, which every common terminal supports, and to mid-luminance tones that read on dark and light backgrounds. A diff reads the way git paints one: file headers bold, hunk headers in light blue, removed lines red and added lines green, in the tones of Error and Success. JSON reads in cool tones: keys turquoise, strings and numbers in two greens, true and false in a pale green, and null receding in grey.
var NamePalette = []Style{ Fg256(218), Fg256(116), Fg256(216), Fg256(105), Fg256(120), Fg256(177), Fg256(110), }
NamePalette is the palette Hashed picks from when it is given none: seven distinct mid-luminance hues of the 256-color palette — pink, turquoise, apricot, periwinkle, green, orchid and steel blue — that read on dark and light backgrounds and stay apart from the red and yellow of warnings and errors.
var Plain = Theme{}
Plain paints nothing. Installing it keeps escape sequences out of output even where term.Colored would allow them.
Functions ¶
Types ¶
type DiffKind ¶ added in v0.5.0
type DiffKind int
DiffKind is what a line of a unified diff is, as a DiffScanner tells it.
const ( // DiffText is a line outside the files a diff changes, such as the // commit message above them. It is not painted. DiffText DiffKind = iota // DiffFileHeader is a line that introduces a file: diff --git, index, // mode and rename lines, and the --- and +++ lines naming the old and // the new file. DiffFileHeader // DiffHunkHeader is the line that starts a hunk: @@ -12,7 +12,8 @@. DiffHunkHeader // DiffContext is an unchanged line inside a hunk, or a "\ No newline at // end of file" note. It is not painted. DiffContext // DiffRemoved is a line the change takes away. DiffRemoved // DiffAdded is a line the change adds. DiffAdded )
type DiffScanner ¶ added in v0.5.0
type DiffScanner struct {
// contains filtered or unexported fields
}
DiffScanner tells the lines of a unified diff apart, one at a time. It reads the diff the way patch does: a hunk header says how many old and new lines the hunk holds, and until they are through, a line starting with "-" is a removed line even when it reads "--- something" — only outside a hunk does such a line name a file. A hunk header without line counts, as some tools write them, makes the hunk run until the next header.
A file's header starts at its diff line (diff --git, diff --cc, diff -u) and holds the lines git and diff write there: index, mode, similarity, rename and copy lines, the --- and +++ lines and the note that binary files differ. Any other line ends it, so what follows a file without a hunk, a renamed or a binary one, is text again: the next commit of a git log -p, say.
The combined diff git shows for a merge is read as well. Its hunk headers have one more @ per further parent (@@@ -1,3 -1,3 +1,4 @@@), and its lines start with one marker column per parent: a line with a "-" in any column is removed, one with a "+" and no "-" is added.
A DiffScanner reads its lines as they are and takes them for plain text: a line that starts with an escape sequence is not told apart. Diff text from elsewhere goes through term.Plain before it is classified, see Theme.PaintDiff.
The zero DiffScanner is ready to read a diff from its first line.
Example ¶
A review shows only what a change removes and adds, painted where the output takes color: here Plain stands in for theme.Active().For(os.Stdout) on a pipe.
package main
import (
"fmt"
"strings"
"github.com/mittwald/cli-kit/theme"
)
var diff = "--- a/version.yaml\n" +
"+++ b/version.yaml\n" +
"@@ -1,2 +1,2 @@\n" +
"-version: 8.4.24\n" +
"+version: 8.4.25\n" +
" stable: true\n"
func main() {
th := theme.Plain
var scanner theme.DiffScanner
for line := range strings.SplitSeq(strings.TrimSuffix(diff, "\n"), "\n") {
switch kind := scanner.Classify(line); kind {
case theme.DiffRemoved, theme.DiffAdded:
fmt.Println(" " + th.PaintDiffLine(kind, line))
}
}
}
Output: -version: 8.4.24 +version: 8.4.25
func (*DiffScanner) Classify ¶ added in v0.5.0
func (s *DiffScanner) Classify(line string) DiffKind
Classify returns what line is, given the lines classified before it, and remembers it for the next. line is one line of the diff, without its newline.
type Style ¶
type Style string
Style is a set of SGR parameters, such as "1" (bold) or "38;5;141" (a 256-color foreground). The zero Style paints nothing.
const ( Bold Style = "1" Dim Style = "2" Italic Style = "3" Underline Style = "4" Reverse Style = "7" )
Common attributes.
func Hashed ¶ added in v0.8.0
Hashed returns a style for name that stays the same: the same name gets the same style on every row, in every list and on every run, so a list can be scanned by who or what a row belongs to, such as the author of a commit. It picks from palette by the FNV-1a hash of the name, or from NamePalette when palette is empty.
The name is compared without case and without the white space around it, so "Alice" and " alice" get one style. A blank name gets the zero Style, which paints nothing. Different names may share a style: a palette of seven tells seven names apart at best.
Example ¶
Every row by the same author is painted alike, whatever case the name is written in.
package main
import (
"fmt"
"github.com/mittwald/cli-kit/theme"
)
func main() {
palette := []theme.Style{"31", "32", "33"}
fmt.Println(theme.Hashed("Alice", palette) == theme.Hashed("alice ", palette))
fmt.Printf("%q\n", theme.Hashed("", palette))
}
Output: true ""
func (Style) For ¶ added in v0.4.0
For returns s where w takes color, as term.Colored decides, and the zero Style anywhere else, so text painted with the result stays plain in a pipe, a file or a test's buffer:
fmt.Fprintln(os.Stderr, theme.Active().Muted.For(os.Stderr).PaintLines(note))
func (Style) Paint ¶
Paint wraps text in s and a reset. It returns text unchanged when s is empty or text is empty, so it is safe to call unconditionally.
func (Style) PaintIf ¶
PaintIf paints text only when on is true, usually the result of term.Colored for the writer the text goes to.
func (Style) PaintLines ¶
PaintLines paints every line of text on its own, so a terminal that redraws a single line keeps its color and a newline never carries a color into the next line.
type Theme ¶
type Theme struct {
// Title is a heading, or the question a prompt asks.
Title Style
// Subject is the thing a title is about, drawn inside it.
Subject Style
// Header is a table's header row.
Header Style
// Border is a table's rules and frame.
Border Style
// Muted recedes text: notes, key hints, disabled rows.
Muted Style
// Status is text that only says what a command is doing while it does
// it: the lines of pick.Status and the detail lines of live.Blocks.
Status Style
// Qualifier is secondary text beside a value, quieter than the value but
// louder than Muted.
Qualifier Style
// Value is an answer or a typed value.
Value Style
// Detail is the full value of a shortened item, shown beside a title.
Detail Style
// Selected is the row or item under the cursor.
Selected Style
// SelectedQualifier is Qualifier on the selected row.
SelectedQualifier Style
// Action is what choosing a row does.
Action Style
// SelectedAction is Action on the selected row.
SelectedAction Style
// Accent marks something worth noticing, such as a marker column.
Accent Style
// Flag marks something worth stopping at.
Flag Style
// Info, Success, Warning and Error color messages by severity.
Info Style
Success Style
Warning Style
Error Style
// Added and Removed are what a change adds and takes away, such as the
// added and removed lines of a diff.
Added Style
Removed Style
// DiffHeader is the header of a file in a diff: the diff --git, index,
// --- and +++ lines.
DiffHeader Style
// DiffHunk is the @@ -1,4 +1,5 @@ that starts a hunk of a diff.
DiffHunk Style
// JSONKey is the name of a member of a JSON object, quotes included.
JSONKey Style
// JSONString is a JSON string value, quotes included.
JSONString Style
// JSONNumber is a JSON number.
JSONNumber Style
// JSONBool is a JSON true or false.
JSONBool Style
// JSONNull is a JSON null.
JSONNull Style
}
Theme assigns a style to each role text plays on screen.
func (Theme) For ¶ added in v0.5.0
For returns t where w takes color, as term.Colored decides, and Plain anywhere else, so whatever is painted with the result stays plain in a pipe, a file or a test's buffer:
th := theme.Active().For(os.Stdout)
fmt.Println(th.Success.Paint("passed"), th.Muted.Paint("in 2s"))
func (Theme) PaintDiff ¶ added in v0.5.0
PaintDiff paints every line of text, a unified diff, the way Theme.PaintDiffLine does, telling the lines apart with a DiffScanner. Each line is painted on its own, and line endings are kept. Paint with t.For(w) what is written to w, so it is painted only where w takes color:
fmt.Fprint(os.Stdout, theme.Active().For(os.Stdout).PaintDiff(diff))
The text is painted as it is: nothing is taken out of it. A diff shows what files hold, and a file can hold escape sequences: one that sets the clipboard, or colors that make an added line look removed or hide it. Painting leaves them in place, and term.Sanitize would keep the colors. Diff text that does not come from the program itself goes through term.Plain first, which leaves the text and the line breaks of the diff and nothing else:
fmt.Fprint(os.Stdout, theme.Active().For(os.Stdout).PaintDiff(term.Plain(diff)))
term.Plain reads a carriage return inside a line as a terminal does: the text after it draws over the text before it, the "+" or "-" of the line included. Where no changed line may pass for another, take the carriage returns out before, with strings.ReplaceAll(diff, "\r", "").
Example ¶
package main
import (
"fmt"
"strings"
"github.com/mittwald/cli-kit/theme"
)
var diff = "--- a/version.yaml\n" +
"+++ b/version.yaml\n" +
"@@ -1,2 +1,2 @@\n" +
"-version: 8.4.24\n" +
"+version: 8.4.25\n" +
" stable: true\n"
func main() {
th := theme.Theme{Added: "32", Removed: "31", DiffHeader: theme.Bold, DiffHunk: "36"}
for line := range strings.Lines(th.PaintDiff(diff)) {
fmt.Printf("%q\n", line)
}
}
Output: "\x1b[1m--- a/version.yaml\x1b[0m\n" "\x1b[1m+++ b/version.yaml\x1b[0m\n" "\x1b[36m@@ -1,2 +1,2 @@\x1b[0m\n" "\x1b[31m-version: 8.4.24\x1b[0m\n" "\x1b[32m+version: 8.4.25\x1b[0m\n" " stable: true\n"
func (Theme) PaintDiffLine ¶ added in v0.5.0
PaintDiffLine paints line, a line of a unified diff that a DiffScanner classified as kind: a file header in DiffHeader, the "@@ … @@" of a hunk header in DiffHunk — the function name git writes after it stays plain —, a removed line in Removed and an added one in Added. Text and context lines are returned unchanged.
The line is painted as it is. An escape sequence in it is written out with it, and a reset in it ends the line's color early; see Theme.PaintDiff for diff text from elsewhere.
func (Theme) PaintJSON ¶ added in v0.8.0
PaintJSON paints text, JSON as an encoder wrote it, compact or indented: the names of object members in JSONKey, string values in JSONString, numbers in JSONNumber, true and false in JSONBool and null in JSONNull. Quotes are painted with what they enclose; brackets, braces, commas, colons and white space stay plain. Paint with t.For(w) what is written to w, so it is painted only where w takes color:
data, _ := json.MarshalIndent(v, "", " ") fmt.Println(theme.Active().For(os.Stdout).PaintJSON(string(data)))
PaintJSON does not parse: it reads tokens, and text that is not JSON comes out with its strings, numbers and literals painted as it finds them, and everything else as it is. A string is never painted past the end of its line, so a quote that is never closed does not color the lines after it.
Like Theme.PaintDiff, PaintJSON takes nothing out of the text. JSON written by encoding/json holds no escape sequence, since it escapes every control character in a string. JSON from elsewhere that was not decoded and encoded again goes through term.Plain first.
Example ¶
package main
import (
"fmt"
"github.com/mittwald/cli-kit/theme"
)
func main() {
th := theme.Theme{JSONKey: "36", JSONString: "32", JSONNumber: "33", JSONBool: "35", JSONNull: "2"}
fmt.Printf("%q\n", th.PaintJSON(`{"id":7,"ok":true,"tag":null}`))
}
Output: "{\x1b[36m\"id\"\x1b[0m:\x1b[33m7\x1b[0m,\x1b[36m\"ok\"\x1b[0m:\x1b[35mtrue\x1b[0m,\x1b[36m\"tag\"\x1b[0m:\x1b[2mnull\x1b[0m}"