gg/display.go

60 lines
1.2 KiB
Go
Raw Permalink Normal View History

package main
import (
"fmt"
"github.com/logrusorgru/aurora/v4"
"golang.org/x/sys/unix"
)
2023-05-13 12:47:52 +01:00
// TODO:
// - bold of escaped output doesn't work.
// - could use direct ANSI codes rather than aurora package.
// - configurable colours.
type Display struct {
a *aurora.Aurora
}
func NewDisplay(noColour bool) *Display {
if _, err := unix.IoctlGetTermios(1, unix.TCGETS); err != nil {
noColour = true
}
a := aurora.New(aurora.WithColors(!noColour))
return &Display{
a: a,
}
}
func (d *Display) Filename(name string) aurora.Value {
return d.a.BgBlue(d.a.White(name))
}
func (d *Display) LineNumber(lineNum int) aurora.Value {
return d.a.Green(lineNum)
}
func (d *Display) Match(text string) aurora.Value {
return d.a.Bold(text)
}
func (d *Display) TruncatedMarker() aurora.Value {
return d.a.Magenta("…")
}
2023-05-13 12:45:29 +01:00
func (d *Display) TruncatedChars(charCount int) aurora.Value {
return d.a.Faint(fmt.Sprintf("(%d chars)", charCount))
}
func (d *Display) BadUTF8Char() aurora.Value {
return d.a.Magenta("\uFFFD")
}
func (d *Display) CarriageReturn() aurora.Value {
return d.a.Magenta(`\r`)
}
func (d *Display) UnprintableChar() aurora.Value {
return d.a.Magenta(`·`)
}