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.
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), }
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.
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.
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 (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
}
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))
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.