Documentation
¶
Overview ¶
Package textutil holds the small text helpers the services share for what they show: cutting text to a length, turning an error message into a sentence, counting a noun and listing names as a sentence does, handing the UI an empty list instead of a nil one, and quoting a word for a POSIX shell, always (ShellQuote) or only when it needs it (ShellWord).
Index ¶
- Constants
- func Capitalize(s string) string
- func Clip(s string, n int) string
- func Counted(n int, one, many string) string
- func EndSentence(msg string) string
- func HasText(s string) bool
- func Join[S ~string](elems []S, sep string) string
- func NonNil[T any](list []T) []T
- func NonNilMap[K comparable, V any](m map[K]V) map[K]V
- func OneLine(s string) string
- func Sentence(msg string) string
- func Series(names []string) string
- func ShellQuote(s string) string
- func ShellSafe(s string) bool
- func ShellWord(s string) string
- func TerminalLine(raw string) string
- func Truncate(s string, n int) string
- func WithReason(lead, reason string) string
- type LineSplitter
Constants ¶
const MaxLine = 64 << 10
MaxLine is how much of one line of a tool's output a LineSplitter keeps by default.
Variables ¶
This section is empty.
Functions ¶
func Clip ¶
Clip cuts s to at most n runes, without an ellipsis, and trims the space around what is left when it was cut. An n below 1 leaves s as it is.
func Counted ¶
Counted writes n with its noun: one when n is 1, else many, as in "1 file", "0 files", and "3 files".
func EndSentence ¶
EndSentence ends a message as a sentence without changing its words: trimmed, and with a period unless it already ends in one, a question mark, or an exclamation mark. It suits a line quoted as it was written. An empty message stays empty.
func HasText ¶
HasText reports text that says something: anything left once the white space the window's String.prototype.trim drops is gone from both ends, so the window and the services that post the user's text always agree. That is unicode.IsSpace's set but U+0085, which trim keeps, plus U+FEFF, which trim drops.
func Join ¶
Join joins string-typed values with sep, as strings.Join joins strings, for listing the choices of a named string type in a message.
func NonNil ¶
func NonNil[T any](list []T) []T
NonNil returns list, or an empty list for nil, so JSON carries [] rather than null and the UI always gets an array.
func NonNilMap ¶
func NonNilMap[K comparable, V any](m map[K]V) map[K]V
NonNilMap returns m, or an empty map for nil, so JSON carries {} rather than null and the UI always gets an object.
func OneLine ¶ added in v0.4.0
OneLine turns s into a single line of plain text: control characters (including newlines and tabs) become spaces, bidirectional-text controls are removed, and runs of whitespace collapse to one space, trimmed at both ends. It suits text someone else wrote, such as an item's title, shown or matched as one line.
func Sentence ¶
Sentence turns a message such as an error's into a sentence: trimmed, its first letter upper-cased, and ended as EndSentence ends it. An empty message stays empty.
func Series ¶
Series joins names as a sentence lists them, with a comma before the last one's "and" once there are three: "a", "a and b", "a, b, and c", and "" for none.
func ShellQuote ¶
ShellQuote quotes s as one word for a POSIX shell, whatever s holds: in single quotes, inside which nothing is special, each single quote in s closing them, written as \', and opening them again. The empty string comes out as a pair of quotes. It quotes every word of a script the app writes, the ssh command it hands git, and the program of a command line a shell runs unless that is a path that starts with a slash (ShellWord).
func ShellSafe ¶
ShellSafe reports whether every POSIX shell reads s, unquoted, as exactly one word that holds s: s isn't empty, holds only ASCII letters and digits, =, and -_./:,+@%, and doesn't start with =, which zsh reads as a command's path. Every other character, spaces, quotes, $, `, \, globs, ~, #, control characters, and anything outside ASCII among them, needs quotes.
func ShellWord ¶
ShellWord quotes s as one word for a POSIX shell only when it needs quotes: as it is when ShellSafe passes it, else as ShellQuote quotes it. Every POSIX shell reads what it returns as the one word s, so it suits a command shown to the user, or copied, where a plain word reads better, and a folder or an argument of a command line a shell runs. A shell reads a command's first word as more than itself, though, when it is a reserved word, an alias, or, as in A=b, sets a variable, so a command line a shell runs quotes its program with ShellQuote unless it is a path that starts with a slash. A command only shown (scriptrun.DisplayCommand) passes its program here too: a run's is always such a path.
func TerminalLine ¶
TerminalLine returns one line of a tool's output as a terminal would show it: from its last bare \r, which overwrites the line, without escapes or other control characters, valid UTF-8, and no trailing space.
func Truncate ¶
Truncate cuts s to at most n runes, ending it with an ellipsis (which counts toward n) when it was cut. Space before the ellipsis is dropped. An n below 1 leaves s as it is.
func WithReason ¶
WithReason ends lead with ": reason", the reason ended as a sentence, or, without one, ends lead as a sentence: "Couldn't check whether v1.4.3 reached GitHub: origin hung up."
Types ¶
type LineSplitter ¶
type LineSplitter struct {
// Max is how much of a line is kept; 0 is MaxLine.
Max int
// contains filtered or unexported fields
}
LineSplitter cuts a tool's output into lines as it arrives, split anywhere: each whole line, ended by \n, goes to the callback as TerminalLine shows it, and a line longer than Max keeps its first Max bytes, its rest dropped up to its newline. Flush hands over a last line that has no newline. The zero value is ready, keeping MaxLine. It isn't safe for concurrent use: the reader that holds it guards it, as it guards what the callback changes.
func (*LineSplitter) Flush ¶
func (s *LineSplitter) Flush(line func(string))
Flush hands line the last line, when the output ended without a newline.
func (*LineSplitter) Write ¶
func (s *LineSplitter) Write(p []byte, line func(string))
Write hands line every line p ends, and keeps the rest for the next Write.