jangko / nim-noise

Nim implementation of linenoise command line editor
MIT License
60 stars 11 forks source link

Using custom colors in the prompt #33

Open midshell-ghoul opened 2 weeks ago

midshell-ghoul commented 2 weeks ago

Using custom colors in the prompt adds dead space at the end of it. This occurs anytime you pass a string with ANSI escape characters in it, and seems to be roughly the length of the escape characters used.

This code:

var noise = Noise.init()

let fgCustomColor = ansiForegroundColorCode(rgb(150, 50, 250))
let prompt = Styler.init(fgCustomColor, "prompt> ")
noise.setPrompt(prompt)

Results in this output: image

I tried another method, by using a Color value directly in the Styler initialization, however it didn't work. I might be reading the source code wrong, but I believe it is supported here (https://github.com/jangko/nim-noise/blob/master/noise/styler.nim#L54). Please let me know if I've misunderstood something.

jangko commented 2 weeks ago

looks like the prompt length calculator not calculating the color correctly. The calculator should ignore the color chars length.

midshell-ghoul commented 2 weeks ago

Is there a proper way to pass Color to the Styler.init() ? I tried:

let fgCustomColor = rgb(150, 50, 250)
let prompt = Styler.init(fgCustomColor, "prompt> ")

but that didn't seem to take despite there being code in the library to handle Color.

jangko commented 2 weeks ago

What kind of terminal you use? Is the example/test.nim works for your terminal?

midshell-ghoul commented 2 weeks ago

I'm using the default kali terminal. The examples in example/test.nim work as they are using ForegroundColor. Anything from std/colors fails (https://nim-lang.org/docs/colors.html)

Edit: zsh version 5.9. It seems nim-noise uses stdout.setForegroundColor c.rgbVal when using Color. This function checks to make sure the terminal supports true color, which mine supposedly does... (https://github.com/nim-lang/Nim/blob/version-2-2/lib/pure/terminal.nim#L764). This might just be a configuration issue on my end.

Edit2: Terminal supports true color just fine image

Final Edit: Okay it turns out I needed to call enableTrueColors(). std/terminal doesn't seem to actually check whether true color is supported, you've got to explicitly tell it.

jangko commented 2 weeks ago

Glad you can solve it