commit 70d519a

Alexey Yerin  ·  2021-05-28 10:30:54 +0000 UTC
parent eafb0dd
Add colors.prompt option to set prompt color
3 files changed,  +71, -2
M app.go
M app.go
+10, -2
 1@@ -70,7 +70,11 @@ func NewApp(cfg Config) (app *App, err error) {
 2 	if err != nil {
 3 		return
 4 	}
 5-	app.win.SetPrompt(ui.PlainString(">"))
 6+	app.win.SetPrompt(ui.Styled(">",
 7+		tcell.
 8+			StyleDefault.
 9+			Foreground(tcell.Color(app.cfg.Colors.Prompt))),
10+	)
11 
12 	app.initWindow()
13 
14@@ -787,7 +791,11 @@ func (app *App) updatePrompt() {
15 	command := app.win.InputIsCommand()
16 	var prompt ui.StyledString
17 	if buffer == Home || command {
18-		prompt = ui.PlainString(">")
19+		prompt = ui.Styled(">",
20+			tcell.
21+				StyleDefault.
22+				Foreground(tcell.Color(app.cfg.Colors.Prompt)),
23+		)
24 	} else {
25 		prompt = identString(app.s.Nick())
26 	}
+42, -0
 1@@ -4,10 +4,48 @@ import (
 2 	"errors"
 3 	"fmt"
 4 	"io/ioutil"
 5+	"strings"
 6+	"strconv"
 7+
 8+	"github.com/gdamore/tcell/v2"
 9 
10 	"gopkg.in/yaml.v2"
11 )
12 
13+type Color tcell.Color
14+
15+func (c *Color) UnmarshalText(data []byte) error {
16+	s := string(data)
17+
18+	if strings.HasPrefix(s, "#") {
19+		hex, err := strconv.ParseInt(s[1:], 16, 32)
20+		if err != nil {
21+			return err
22+		}
23+
24+		*c = Color(tcell.NewHexColor(int32(hex)))
25+		return nil
26+	}
27+
28+	code, err := strconv.Atoi(s)
29+	if err != nil {
30+		return err
31+	}
32+
33+	if code == -1 {
34+		*c = Color(tcell.ColorDefault)
35+		return nil
36+	}
37+
38+	if code < 0 || code > 255 {
39+		return fmt.Errorf("color code must be between 0-255. If you meant to use true colors, use #aabbcc notation")
40+	}
41+
42+	*c = Color(tcell.PaletteColor(code))
43+
44+	return nil
45+}
46+
47 type Config struct {
48 	Addr     string
49 	Nick     string
50@@ -24,6 +62,10 @@ type Config struct {
51 	NickColWidth int    `yaml:"nick-column-width"`
52 	ChanColWidth int    `yaml:"chan-column-width"`
53 
54+	Colors struct {
55+		Prompt Color
56+	}
57+
58 	Debug bool
59 }
60 
+19, -0
 1@@ -70,6 +70,25 @@ Some settings are required, the others are optional.
 2 *mouse*
 3 	Enable or disable mouse support.  Defaults to true.
 4 
 5+*colors*
 6+	Settings for colors of different UI elements.
 7+
 8+	Colors are represented as numbers from 0 to 255 for 256 default terminal
 9+	colors respectively. -1 has special meaning of default terminal color. To
10+	use true colors, *#*_rrggbb_ notation is supported.
11+
12+	Colors are set as sub-options of the main *colors* option:
13+
14+```
15+colors:
16+    prompt: 3 # green
17+```
18+
19+[[ *Sub-option*
20+:< *Description*
21+|  prompt
22+:  color for ">"-prompt that appears in command mode
23+
24 *debug*
25 	Dump all sent and received data to the home buffer, useful for debugging.
26 	By default, false.