git.sophuwu.com > rbprompt
added hex colour option to fmt
sophuwu sophie@skisiel.com
Sat, 22 Mar 2025 04:35:44 +0100
commit

274f3a74c73b6df745f7e3395890ffc0272e0348

parent

e517b08b854a762774cbc3cc3659d73b9e479522

3 files changed, 44 insertions(+), 8 deletions(-)

jump to
M example/output.htmlexample/output.html

@@ -3,19 +3,22 @@ <pre>testing as: sophie@sophuwu.com

in: /home/sophie/.local/share/applications Fmt: &quot;%i %l %e %u @ %h %p $ &quot; -<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> +<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="#FF2B00">0</font><font color="#FF5600"> </font><font color="#FF8100">;</font><font color="#FFAC00">3</font><font color="#FFD700"> </font><font color="#FCFF00">s</font><font color="#D1FF00">o</font><font color="#A6FF00">p</font><font color="#7BFF00">h</font><font color="#50FF00">i</font><font color="#25FF00">e</font><font color="#00FF06"> </font><font color="#00FF31">@</font><font color="#00FF5C"> </font><font color="#00FF87">s</font><font color="#00FFB2">o</font><font color="#00FFDD">p</font><font color="#00F6FF">h</font><font color="#00CBFF">u</font><font color="#00A0FF">w</font><font color="#0075FF">u</font><font color="#004AFF">.</font><font color="#001FFF">c</font><font color="#0C00FF">o</font><font color="#3700FF">m</font><font color="#6200FF"> </font><font color="#8D00FF">~</font><font color="#B800FF">/</font><font color="#E300FF">.</font><font color="#FF00F0">l</font><font color="#FF00C5">o</font><font color="#FF009A">c</font><font color="#FF006F">a</font><font color="#FF0044">l</font><font color="#FF0019">/</font><font color="#FF1200">s</font><font color="#FF3D00">h</font><font color="#FF6800">a</font><font color="#FF9300">r</font><font color="#FFBE00">e</font><font color="#FFE900">/</font><font color="#EAFF00">a</font><font color="#BFFF00">p</font><font color="#94FF00">p</font><font color="#69FF00">l</font><font color="#3EFF00">i</font><font color="#13FF00">c</font><font color="#00FF18">a</font><font color="#00FF43">t</font><font color="#00FF6E">i</font><font color="#00FF99">o</font><font color="#00FFC4">n</font><font color="#00FFEF">s</font><font color="#00E4FF"> </font><font color="#00B9FF">$</font><font color="#008EFF"> </font> Fmt: &quot;%e %l %u @ %h %2p $ &quot; -<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> +<font color="#FF2B00">;</font><font color="#FF5900">3</font><font color="#FF8700"> </font><font color="#FFB500">1</font><font color="#FFE300"> </font><font color="#EDFF00">s</font><font color="#BFFF00">o</font><font color="#91FF00">p</font><font color="#63FF00">h</font><font color="#35FF00">i</font><font color="#07FF00">e</font><font color="#00FF27"> </font><font color="#00FF55">@</font><font color="#00FF83"> </font><font color="#00FFB1">s</font><font color="#00FFDF">o</font><font color="#00F1FF">p</font><font color="#00C3FF">h</font><font color="#0095FF">u</font><font color="#0067FF">w</font><font color="#0039FF">u</font><font color="#000BFF">.</font><font color="#2300FF">c</font><font color="#5100FF">o</font><font color="#7F00FF">m</font><font color="#AD00FF"> </font><font color="#DB00FF">s</font><font color="#FF00F5">h</font><font color="#FF00C7">a</font><font color="#FF0099">r</font><font color="#FF006B">e</font><font color="#FF003D"> </font><font color="#FF000F">a</font><font color="#FF1F00">p</font><font color="#FF4D00">p</font><font color="#FF7B00">l</font><font color="#FFA900">i</font><font color="#FFD700">c</font><font color="#F9FF00">a</font><font color="#CBFF00">t</font><font color="#9DFF00">i</font><font color="#6FFF00">o</font><font color="#41FF00">n</font><font color="#13FF00">s</font><font color="#00FF1B"> </font><font color="#00FF49">$</font><font color="#00FF77"> </font> + +Fmt: &quot;%e %l %u#ff4500 @ %h %2p#ff00ff $ &quot; +<font color="#FF5600">=</font><font color="#FF8600">]</font><font color="#FFB600"> </font><font color="#FFE600">2</font><font color="#E8FF00"> </font><font color="#FF4500">sophie</font><font color="#B8FF00"> </font><font color="#88FF00">@</font><font color="#58FF00"> </font><font color="#28FF00">s</font><font color="#00FF08">o</font><font color="#00FF38">p</font><font color="#00FF68">h</font><font color="#00FF98">u</font><font color="#00FFC8">w</font><font color="#00FFF8">u</font><font color="#00D6FF">.</font><font color="#00A6FF">c</font><font color="#0076FF">o</font><font color="#0046FF">m</font><font color="#0016FF"> </font><font color="#FF00FF">share applications</font><font color="#1A00FF"> </font><font color="#4A00FF">$</font><font color="#7A00FF"> </font> Fmt: &quot;%l %u %2p $ &quot; -<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> +<font color="#FF8100">3</font><font color="#FFB400"> </font><font color="#FFE700">s</font><font color="#E4FF00">o</font><font color="#B1FF00">p</font><font color="#7EFF00">h</font><font color="#4BFF00">i</font><font color="#18FF00">e</font><font color="#00FF1B"> </font><font color="#00FF4E">s</font><font color="#00FF81">h</font><font color="#00FFB4">a</font><font color="#00FFE7">r</font><font color="#00E4FF">e</font><font color="#00B1FF"> </font><font color="#007EFF">a</font><font color="#004BFF">p</font><font color="#0018FF">p</font><font color="#1B00FF">l</font><font color="#4E00FF">i</font><font color="#8100FF">c</font><font color="#B400FF">a</font><font color="#E700FF">t</font><font color="#FF00E4">i</font><font color="#FF00B1">o</font><font color="#FF007E">n</font><font color="#FF004B">s</font><font color="#FF0018"> </font><font color="#FF1B00">$</font><font color="#FF4E00"> </font> Fmt: &quot;%l %u %2p $ &quot; -<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 +<font color="#FFAC00">4</font><font color="#FFE000"> </font><font color="#EAFF00">s</font><font color="#B6FF00">o</font><font color="#82FF00">p</font><font color="#4EFF00">h</font><font color="#1AFF00">i</font><font color="#00FF1A">e</font><font color="#00FF4E"> </font><font color="#00FF82">s</font><font color="#00FFB6">h</font><font color="#00FFEA">a</font><font color="#00E0FF">r</font><font color="#00ACFF">e</font><font color="#0078FF"> </font><font color="#0044FF">a</font><font color="#0010FF">p</font><font color="#2400FF">p</font><font color="#5800FF">l</font><font color="#8C00FF">i</font><font color="#C000FF">c</font><font color="#F400FF">a</font><font color="#FF00D6">t</font><font color="#FF00A2">i</font><font color="#FF006E">o</font><font color="#FF003A">n</font><font color="#FF0006">s</font><font color="#FF2E00"> </font><font color="#FF6200">$</font><font color="#FF9600"> </font>cd /usr/local/share/man -Fmt: &quot;%l %u %p $ &quot; -<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> +Fmt: &quot;%l %u %p#ff00ff $ &quot; +<font color="#FFD700">5</font><font color="#F2FF00"> </font><font color="#BDFF00">s</font><font color="#88FF00">o</font><font color="#53FF00">p</font><font color="#1EFF00">h</font><font color="#00FF17">i</font><font color="#00FF4C">e</font><font color="#00FF81"> </font><font color="#FF00FF">/usr/local/share/man</font><font color="#00FFB6"> </font><font color="#00FFEB">$</font><font color="#00DEFF"> </font> </pre> </body> </html>
M example/test.goexample/test.go

@@ -36,6 +36,7 @@

for _, v := range []string{ "%i %l %e %u @ %h %p $ ", "%e %l %u @ %h %2p $ ", + "%e %l %u#ff4500 @ %h %2p#ff00ff $ ", "%l %u %2p $ ", } { ps.Fmt = v

@@ -47,7 +48,7 @@ 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 $ " + ps.Fmt = "%l %u %p#ff00ff $ " err = ps.Next() chk() fmt.Printf("\nFmt: \"%s\"\n%s\n", ps.Fmt, ps.String())
M rbprompt.gorbprompt.go

@@ -114,6 +114,7 @@ // %e = random emote

// %l = line number // %<n>p = bottom <n> directories in PWD // %p = entire PWD, short for %0p + // adding #xxxxxx (hex colour) after any argument will set the colour of that argument. e.g. %u#ff0000 will set the username to red. Fmt string // Set User to the username of the current user, required if Fmt contains "%u". User string

@@ -186,6 +187,25 @@ return fmt.Errorf("rbprompt: %s: %s", fnName, s)

} } +type hex struct { + rgb [3]byte +} + +func (h *hex) readByte(i int, c byte) bool { + if c >= '0' && c <= '9' { + c = c - '0' + } else if c >= 'a' && c <= 'f' { + c = c - 'a' + 10 + } else { + return false + } + h.rgb[i/2] |= byte(c) << (4 * (1 - i%2)) + return true +} +func (h *hex) escape(s string) string { + return fmt.Sprintf(ESCAPESTR+"38;2;%d;%d;%dm%s"+ESCAPESTR+"0m", h.rgb[0], h.rgb[1], h.rgb[2], s) +} + func (p *PS) doFmt(rbfn *func(string) string, dst *string, f *string, i *int) error { errFn := errGen("PS.doFmt") s := ""

@@ -236,7 +256,19 @@ }

if s == "" { return errFn("invalid format " + (*f)[j:*i]) } - *dst += (*rbfn)(s) + if *i < len(*f)-1 && (*f)[*i+1] == '#' { + *i++ + var rgb hex + for k := 0; *i < len(*f) && k < 6; k++ { + *i++ + if !rgb.readByte(k, (*f)[*i]) { + return errFn("parse error: invalid hex colour " + (*f)[j:*i]) + } + } + *dst += rgb.escape(s) + } else { + *dst += (*rbfn)(s) + } *i++ return nil }