~taiite/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[PATCH senpai] Add colors.prompt option to set prompt color

Details
Message ID
<20210527132535.27028-1-yyp@disroot.org>
DKIM signature
pass
Download raw message
Patch: +54 -2
---
 app.go    | 10 ++++++++--
 config.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/app.go b/app.go
index 3e544df..f1ef5e0 100644
--- a/app.go
+++ b/app.go
@@ -70,7 +70,10 @@ func NewApp(cfg Config) (app *App, err error) {
	if err != nil {
		return
	}
	app.win.SetPrompt(ui.PlainString(">"))
	app.win.SetPrompt(ui.Styled(">",
		tcell.
			StyleDefault.
			Foreground(app.cfg.Colors.Prompt.TcellColor())))

	app.initWindow()

@@ -796,7 +799,10 @@ func (app *App) updatePrompt() {
	command := app.win.InputIsCommand()
	var prompt ui.StyledString
	if buffer == Home || command {
		prompt = ui.PlainString(">")
		prompt = ui.Styled(">",
			tcell.
				StyleDefault.
				Foreground(app.cfg.Colors.Prompt.TcellColor()))
	} else {
		prompt = identString(app.s.Nick())
	}
diff --git a/config.go b/config.go
index 1d8d79a..6703944 100644
--- a/config.go
+++ b/config.go
@@ -4,10 +4,52 @@ import (
	"errors"
	"fmt"
	"io/ioutil"
	"strings"
	"strconv"

	"github.com/gdamore/tcell/v2"

	"gopkg.in/yaml.v2"
)

type Color tcell.Color

func (c *Color) UnmarshalText(data []byte) error {
	s := string(data)

	if strings.HasPrefix(s, "#") {
		hex, err := strconv.ParseInt(s[1:], 16, 32)
		if err != nil {
			return err
		}

		*c = Color(tcell.NewHexColor(int32(hex)))
		return nil
	}

	code, err := strconv.Atoi(s)
	if err != nil {
		return err
	}

	if code == -1 {
		*c = Color(tcell.ColorDefault)
		return nil
	}

	if code < 0 || code > 255 {
		return fmt.Errorf("color code must be between 0-255. If you meant to use true colors, use #aabbcc notation")
	}
	
	*c = Color(tcell.PaletteColor(code))

	return nil
}

func (c *Color) TcellColor() tcell.Color {
	return tcell.Color(*c)
}

type Config struct {
	Addr     string
	Nick     string
@@ -24,6 +66,10 @@ type Config struct {
	NickColWidth int    `yaml:"nick-column-width"`
	ChanColWidth int    `yaml:"chan-column-width"`

	Colors struct {
		Prompt Color
	}

	Debug bool
}

-- 
2.31.1
Details
Message ID
<20210528095206.560d609f@vroom.localdomain>
In-Reply-To
<20210527132535.27028-1-yyp@disroot.org> (view parent)
DKIM signature
pass
Download raw message
Thank you for the patch.

Can you add an item for this option in senpai.5.scd?

Also some minor nits

On Thu, 27 May 2021 16:25:35 +0300, Alexey Yerin wrote:
> ---
>  app.go    | 10 ++++++++--
>  config.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 54 insertions(+), 2 deletions(-)
> 
> diff --git a/app.go b/app.go
> index 3e544df..f1ef5e0 100644
> --- a/app.go
> +++ b/app.go
> @@ -70,7 +70,10 @@ func NewApp(cfg Config) (app *App, err error) {
>  	if err != nil {
>  		return
>  	}
> -	app.win.SetPrompt(ui.PlainString(">"))  
> +	app.win.SetPrompt(ui.Styled(">",
> +		tcell.
> +			StyleDefault.
> +			Foreground(app.cfg.Colors.Prompt.TcellColor())))
>  
>  	app.initWindow()
>  
> @@ -796,7 +799,10 @@ func (app *App) updatePrompt() {
>  	command := app.win.InputIsCommand()
>  	var prompt ui.StyledString
>  	if buffer == Home || command {
> -		prompt = ui.PlainString(">")
> +		prompt = ui.Styled(">",
> +			tcell.
> +				StyleDefault.
> +				Foreground(app.cfg.Colors.Prompt.TcellColor()))
>  	} else {
>  		prompt = identString(app.s.Nick())
>  	}
> diff --git a/config.go b/config.go
> index 1d8d79a..6703944 100644
> --- a/config.go
> +++ b/config.go
> @@ -4,10 +4,52 @@ import (
>  	"errors"
>  	"fmt"
>  	"io/ioutil"
> +	"strings"
> +	"strconv"
> +
> +	"github.com/gdamore/tcell/v2"
>  
>  	"gopkg.in/yaml.v2"
>  )
>  
> +type Color tcell.Color
> +
> +func (c *Color) UnmarshalText(data []byte) error {
> +	s := string(data)
> +
> +	if strings.HasPrefix(s, "#") {
> +		hex, err := strconv.ParseInt(s[1:], 16, 32)
> +		if err != nil {
> +			return err
> +		}
> +
> +		*c = Color(tcell.NewHexColor(int32(hex)))
> +		return nil
> +	}
> +
> +	code, err := strconv.Atoi(s)
> +	if err != nil {
> +		return err
> +	}
> +
> +	if code == -1 {
> +		*c = Color(tcell.ColorDefault)
> +		return nil
> +	}
> +
> +	if code < 0 || code > 255 {
> +		return fmt.Errorf("color code must be between 0-255. If you meant to use true colors, use #aabbcc notation")
> +	}
> +	
> +	*c = Color(tcell.PaletteColor(code))
> +
> +	return nil
> +}
> +
> +func (c *Color) TcellColor() tcell.Color {
> +	return tcell.Color(*c)
> +}

Can you inline this function? It's just a cast.

> +
>  type Config struct {
>  	Addr     string
>  	Nick     string
> @@ -24,6 +66,10 @@ type Config struct {
>  	NickColWidth int    `yaml:"nick-column-width"`
>  	ChanColWidth int    `yaml:"chan-column-width"`
>  
> +	Colors struct {
> +		Prompt Color
> +	}
> +
>  	Debug bool
>  }
>  
Reply to thread Export thread (mbox)