package ui
import (
"fmt"
"strings"
"github.com/reeflective/readline/internal/color"
"github.com/reeflective/readline/internal/strutil"
"github.com/reeflective/readline/internal/term"
)
type Hint struct {
text []rune
persistent []rune
cleanup bool
temp bool
set bool
}
func (h *Hint ) Set (hint string ) {
h .text = []rune (hint )
h .set = true
}
func (h *Hint ) SetTemporary (hint string ) {
h .text = []rune (hint )
h .set = true
h .temp = true
}
func (h *Hint ) Persist (hint string ) {
h .persistent = []rune (hint )
}
func (h *Hint ) Text () string {
return string (h .text )
}
func (h *Hint ) Len () int {
return len (h .text )
}
func (h *Hint ) Reset () {
h .text = make ([]rune , 0 )
h .temp = false
h .set = false
}
func (h *Hint ) ResetPersist () {
h .cleanup = len (h .persistent ) > 0
h .persistent = make ([]rune , 0 )
}
func DisplayHint (hint *Hint ) {
if hint .temp && hint .set {
hint .set = false
} else if hint .temp {
hint .Reset ()
}
if len (hint .text ) == 0 && len (hint .persistent ) == 0 {
if hint .cleanup {
fmt .Print (term .ClearLineAfter )
}
hint .cleanup = false
return
}
text := hint .renderHint ()
if strutil .RealLength (text ) == 0 {
return
}
text += term .ClearLineAfter + color .Reset
if len (text ) > 0 {
fmt .Print (text )
}
}
func (h *Hint ) renderHint () (text string ) {
if len (h .persistent ) > 0 {
text += string (h .persistent ) + term .NewlineReturn
}
if len (h .text ) > 0 {
text += string (h .text ) + term .NewlineReturn
}
if strutil .RealLength (text ) == 0 {
return
}
text = strings .ReplaceAll (text , term .NewlineReturn , term .ClearLineAfter +term .NewlineReturn )
return text
}
func CoordinatesHint (hint *Hint ) int {
text := hint .renderHint ()
text = strings .TrimSuffix (text , term .ClearLineAfter +term .NewlineReturn )
if strutil .RealLength (text ) == 0 {
return 0
}
usedY := 0
lines := strings .Split (text , term .ClearLineAfter )
for i , line := range lines {
x , y := strutil .LineSpan ([]rune (line ), i , 0 )
if x != 0 {
y ++
}
usedY += y
}
return usedY
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .