added docs and examples
sophuwu sophie@skisiel.com
Sat, 22 Mar 2025 03:20:44 +0100
3 files changed,
104 insertions(+),
6 deletions(-)
A
example/output.html
@@ -0,0 +1,21 @@
+<html><body style="background:#262833;color:#aaa;"> +<pre>testing as: sophie@sophuwu.com +in: /home/sophie/.local/share/applications + +Fmt: "%i %l %e %u @ %h %p $ " +<span style="background-color:#FF00AF"><font color="#AFD7AF">▄</font></span><span style="background-color:#AFD7D7"><font color="#D7875F">▄</font></span><font color="#FF0000"> </font><font color="#FF3300">0</font><font color="#FF6600"> </font><font color="#FF9900">:</font><font color="#FFCC00">O</font><font color="#FFFF00"> </font><font color="#CCFF00">s</font><font color="#99FF00">o</font><font color="#66FF00">p</font><font color="#33FF00">h</font><font color="#00FF00">i</font><font color="#00FF33">e</font><font color="#00FF66"> </font><font color="#00FF99">@</font><font color="#00FFCC"> </font><font color="#00FFFF">s</font><font color="#00CCFF">o</font><font color="#0099FF">p</font><font color="#0066FF">h</font><font color="#0033FF">u</font><font color="#0000FF">w</font><font color="#3300FF">u</font><font color="#6600FF">.</font><font color="#9900FF">c</font><font color="#CC00FF">o</font><font color="#FF00FF">m</font><font color="#FF00CC"> </font><font color="#FF0099">~</font><font color="#FF0066">/</font><font color="#FF0033">.</font><font color="#FF0000">l</font><font color="#FF3300">o</font><font color="#FF6600">c</font><font color="#FF9900">a</font><font color="#FFCC00">l</font><font color="#FFFF00">/</font><font color="#CCFF00">s</font><font color="#99FF00">h</font><font color="#66FF00">a</font><font color="#33FF00">r</font><font color="#00FF00">e</font><font color="#00FF33">/</font><font color="#00FF66">a</font><font color="#00FF99">p</font><font color="#00FFCC">p</font><font color="#00FFFF">l</font><font color="#00CCFF">i</font><font color="#0099FF">c</font><font color="#0066FF">a</font><font color="#0033FF">t</font><font color="#0000FF">i</font><font color="#3300FF">o</font><font color="#6600FF">n</font><font color="#9900FF">s</font><font color="#CC00FF"> </font><font color="#FF00FF">$</font><font color="#FF00CC"> </font> + +Fmt: "%e %l %u @ %h %2p $ " +<font color="#FF3300">:</font><font color="#FF6900">3</font><font color="#FF9F00"> </font><font color="#FFD500">1</font><font color="#F3FF00"> </font><font color="#BDFF00">s</font><font color="#87FF00">o</font><font color="#51FF00">p</font><font color="#1BFF00">h</font><font color="#00FF1B">i</font><font color="#00FF51">e</font><font color="#00FF87"> </font><font color="#00FFBD">@</font><font color="#00FFF3"> </font><font color="#00D5FF">s</font><font color="#009FFF">o</font><font color="#0069FF">p</font><font color="#0033FF">h</font><font color="#0300FF">u</font><font color="#3900FF">w</font><font color="#6F00FF">u</font><font color="#A500FF">.</font><font color="#DB00FF">c</font><font color="#FF00ED">o</font><font color="#FF00B7">m</font><font color="#FF0081"> </font><font color="#FF004B">s</font><font color="#FF0015">h</font><font color="#FF2100">a</font><font color="#FF5700">r</font><font color="#FF8D00">e</font><font color="#FFC300"> </font><font color="#FFF900">a</font><font color="#CFFF00">p</font><font color="#99FF00">p</font><font color="#63FF00">l</font><font color="#2DFF00">i</font><font color="#00FF09">c</font><font color="#00FF3F">a</font><font color="#00FF75">t</font><font color="#00FFAB">i</font><font color="#00FFE1">o</font><font color="#00E7FF">n</font><font color="#00B1FF">s</font><font color="#007BFF"> </font><font color="#0045FF">$</font><font color="#000FFF"> </font> + +Fmt: "%l %u %2p $ " +<font color="#FF6600">2</font><font color="#FF9E00"> </font><font color="#FFD600">s</font><font color="#F0FF00">o</font><font color="#B8FF00">p</font><font color="#80FF00">h</font><font color="#48FF00">i</font><font color="#10FF00">e</font><font color="#00FF28"> </font><font color="#00FF60">s</font><font color="#00FF98">h</font><font color="#00FFD0">a</font><font color="#00F6FF">r</font><font color="#00BEFF">e</font><font color="#0086FF"> </font><font color="#004EFF">a</font><font color="#0016FF">p</font><font color="#2200FF">p</font><font color="#5A00FF">l</font><font color="#9200FF">i</font><font color="#CA00FF">c</font><font color="#FF00FC">a</font><font color="#FF00C4">t</font><font color="#FF008C">i</font><font color="#FF0054">o</font><font color="#FF001C">n</font><font color="#FF1C00">s</font><font color="#FF5400"> </font><font color="#FF8C00">$</font><font color="#FFC400"> </font> + +Fmt: "%l %u %2p $ " +<font color="#FF9900">3</font><font color="#FFD400"> </font><font color="#EFFF00">s</font><font color="#B4FF00">o</font><font color="#79FF00">p</font><font color="#3EFF00">h</font><font color="#03FF00">i</font><font color="#00FF38">e</font><font color="#00FF73"> </font><font color="#00FFAE">s</font><font color="#00FFE9">h</font><font color="#00DAFF">a</font><font color="#009FFF">r</font><font color="#0064FF">e</font><font color="#0029FF"> </font><font color="#1200FF">a</font><font color="#4D00FF">p</font><font color="#8800FF">p</font><font color="#C300FF">l</font><font color="#FE00FF">i</font><font color="#FF00C5">c</font><font color="#FF008A">a</font><font color="#FF004F">t</font><font color="#FF0014">i</font><font color="#FF2700">o</font><font color="#FF6200">n</font><font color="#FF9D00">s</font><font color="#FFD800"> </font><font color="#EBFF00">$</font><font color="#B0FF00"> </font>cd /usr/local/share/man + +Fmt: "%l %u %p $ " +<font color="#FFCC00">4</font><font color="#F6FF00"> </font><font color="#BAFF00">s</font><font color="#7EFF00">o</font><font color="#42FF00">p</font><font color="#06FF00">h</font><font color="#00FF36">i</font><font color="#00FF72">e</font><font color="#00FFAE"> </font><font color="#00FFEA">/</font><font color="#00D8FF">u</font><font color="#009CFF">s</font><font color="#0060FF">r</font><font color="#0024FF">/</font><font color="#1800FF">l</font><font color="#5400FF">o</font><font color="#9000FF">c</font><font color="#CC00FF">a</font><font color="#FF00F6">l</font><font color="#FF00BA">/</font><font color="#FF007E">s</font><font color="#FF0042">h</font><font color="#FF0006">a</font><font color="#FF3600">r</font><font color="#FF7200">e</font><font color="#FFAE00">/</font><font color="#FFEA00">m</font><font color="#D8FF00">a</font><font color="#9CFF00">n</font><font color="#60FF00"> </font><font color="#24FF00">$</font><font color="#00FF18"> </font> +</pre> +</body> +</html>
A
example/test.go
@@ -0,0 +1,54 @@
+package main + +import ( + "fmt" + "git.sophuwu.com/rbprompt" + "os" +) + +func Err(err *error) func() { + if err == nil { + fmt.Println("null pointer given to error handler") + os.Exit(1) + } + return func() { + if *err != nil { + fmt.Println(*err) + os.Exit(1) + } + } +} + +func main() { + var err error + chk := Err(&err) + var ps rbprompt.PS + ps.NoColor = false + ps.LineNo = 0 + ps.User = "sophie" + ps.HostName = "sophuwu.com" + ps.HostIP = [4]byte{217, 160, 162, 187} + ps.Home = "/home/sophie" + ps.PWD = "/home/sophie/.local/share/applications" + + fmt.Printf("testing as: %s@%s\nin: %s\n", ps.User, ps.HostName, ps.PWD) + + for _, v := range []string{ + "%i %l %e %u @ %h %p $ ", + "%e %l %u @ %h %2p $ ", + "%l %u %2p $ ", + } { + ps.Fmt = v + err = ps.Next() + chk() + fmt.Printf("\nFmt: \"%s\"\n%s\n", ps.Fmt, ps.String()) + } + err = ps.Next() + chk() + ps.PWD = "/usr/local/share/man" + fmt.Printf("\nFmt: \"%s\"\n%scd %s\n", ps.Fmt, ps.String(), ps.PWD) + ps.Fmt = "%l %u %p $ " + err = ps.Next() + chk() + fmt.Printf("\nFmt: \"%s\"\n%s\n", ps.Fmt, ps.String()) +}
M
rbprompt.go
→
rbprompt.go
@@ -95,19 +95,41 @@ out += "\033[0m"
return } +// PS is the main struct for the prompt. It contains all the information needed to generate the prompt. +// After setting User, HostName, Home, PWD, and Fmt, call Next() to generate the prompt. +// Then call String() to get the prompt string and print it. type PS struct { - LineNo uint + // LineNo is the line number, defaults to 0, then increments by 1 each time Next() is called. + LineNo uint + // if NoColor is true, the prompt will be printed without any colors. NoColor bool + // Current is the current prompt string, calculated by the last call to Next(). + // Same as String(). Current string - Fmt, - User, - Home, - HostName, + // Fmt is the format string for the prompt. It can contain the following: + // %u = username + // %h = hostname + // %i = ip address as colours + // %e = random emote + // %l = line number + // %<n>p = bottom <n> directories in PWD + // %p = entire PWD, short for %0p + Fmt string + // Set User to the username of the current user, required if Fmt contains "%u". + User string + // Set Home to the home directory of the current user, required if Fmt contains "%p" or %<n>p. + Home string + // Set HostName to the hostname of the current machine, required if Fmt contains "%h". + HostName string + // Set PWD to the current working directory, required if Fmt contains "%p" or %<n>p. PWD string - HostIP [4]byte + // Set HostIP to the IP address of the current machine, required if Fmt contains "%i". + HostIP [4]byte + // Rainbow is the rainbow struct used to generate the rainbow effect. Rainbow Rainbow } +// String returns the prompt calculated by the last call to Next. func (p *PS) String() string { return p.Current }@@ -219,6 +241,7 @@ *i++
return nil } +// Next generates the next prompt string based on the format string and the current state of the PS struct. func (p *PS) Next() error { p.Rainbow.next() r := p.Rainbow.copy()