gg/display.go
Laurence Withers 4fd3ae4c7e Improvements to display code
Now switchable, detects terminal. Don't be weird with tabs. Known issues are
not being themeable and bold match output stopping if colours are used inside.
2023-05-13 10:58:06 +01:00

54 lines
1 KiB
Go

package main
import (
"fmt"
"github.com/logrusorgru/aurora/v4"
"golang.org/x/sys/unix"
)
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("…")
}
func (d *Display) TruncatedBytes(byteCount int) aurora.Value {
return d.a.Faint(fmt.Sprintf("(%d bytes)", byteCount))
}
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(`·`)
}