textutil

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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 Capitalize

func Capitalize(s string) string

Capitalize upper-cases the first letter of s.

func Clip

func Clip(s string, n int) string

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

func Counted(n int, one, many string) string

Counted writes n with its noun: one when n is 1, else many, as in "1 file", "0 files", and "3 files".

func EndSentence

func EndSentence(msg string) string

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

func HasText(s string) bool

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

func Join[S ~string](elems []S, sep string) string

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 Sentence

func Sentence(msg string) string

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

func Series(names []string) string

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

func ShellQuote(s string) string

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

func ShellSafe(s string) bool

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

func ShellWord(s string) string

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

func TerminalLine(raw string) string

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

func Truncate(s string, n int) string

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

func WithReason(lead, reason string) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL