package uv
import (
)
var (
ErrNotTerminal = fmt.Errorf("not a terminal")
ErrPlatformNotSupported = fmt.Errorf("platform not supported")
)
type Drawable interface {
Draw(scr Screen, area Rectangle)
}
type DrawableFunc func(scr Screen, rect Rectangle)
var _ Drawable = (DrawableFunc)(nil)
func ( DrawableFunc) ( Screen, Rectangle) {
(, )
}
type WidthMethod interface {
StringWidth(s string) int
}
type Screen interface {
Bounds() Rectangle
CellAt(x, y int) *Cell
SetCell(x, y int, c *Cell)
WidthMethod() WidthMethod
}
type Cursor struct {
Position
Color color.Color
Shape CursorShape
Blink bool
Hidden bool
}
func (, int) *Cursor {
return &Cursor{
Position: Position{X: , Y: },
Color: nil,
Shape: CursorBlock,
Blink: true,
}
}
type ProgressBarState int
const (
ProgressBarNone ProgressBarState = iota
ProgressBarDefault
ProgressBarError
ProgressBarIndeterminate
ProgressBarWarning
)
func ( ProgressBarState) () string {
return [...]string{
"None",
"Default",
"Error",
"Indeterminate",
"Warning",
}[]
}
type ProgressBar struct {
State ProgressBarState
Value int
}
func ( ProgressBarState, int) *ProgressBar {
return &ProgressBar{
State: ,
Value: min(max(, 0), 100),
}
}
type KeyboardEnhancements struct {
DisambiguateEscapeCodes bool
ReportEventTypes bool
ReportAlternateKeys bool
ReportAllKeysAsEscapeCodes bool
ReportAssociatedText bool
}
func ( int) *KeyboardEnhancements {
if <= 0 {
return &KeyboardEnhancements{}
}
return &KeyboardEnhancements{
DisambiguateEscapeCodes: &ansi.KittyDisambiguateEscapeCodes != 0,
ReportEventTypes: &ansi.KittyReportEventTypes != 0,
}
}
func ( KeyboardEnhancements) () int {
:= 0
if .DisambiguateEscapeCodes {
|= ansi.KittyDisambiguateEscapeCodes
}
if .ReportEventTypes {
|= ansi.KittyReportEventTypes
}
if .ReportAlternateKeys {
|= ansi.KittyReportAlternateKeys
}
if .ReportAllKeysAsEscapeCodes {
|= ansi.KittyReportAllKeysAsEscapeCodes
}
if .ReportAssociatedText {
|= ansi.KittyReportAssociatedKeys
}
return
}
func ( io.Writer, color.Color) error {
var string
if == nil {
= ansi.ResetBackgroundColor
} else if , := colorful.MakeColor(); {
= ansi.SetBackgroundColor(.Hex())
} else {
return fmt.Errorf("invalid color: %v", )
}
, := io.WriteString(, )
if != nil {
return fmt.Errorf("failed to set background color: %w", )
}
return nil
}
func ( io.Writer, color.Color) error {
var string
if == nil {
= ansi.ResetForegroundColor
} else if , := colorful.MakeColor(); {
= ansi.SetForegroundColor(.Hex())
} else {
return fmt.Errorf("invalid color: %v", )
}
, := io.WriteString(, )
if != nil {
return fmt.Errorf("failed to set foreground color: %w", )
}
return nil
}
func ( io.Writer, color.Color) error {
var string
if == nil {
= ansi.ResetCursorColor
} else if , := colorful.MakeColor(); {
= ansi.SetCursorColor(.Hex())
} else {
return fmt.Errorf("invalid color: %v", )
}
, := io.WriteString(, )
if != nil {
return fmt.Errorf("failed to set cursor color: %w", )
}
return nil
}
func ( io.Writer, CursorShape, bool) error {
:= ansi.SetCursorStyle(.Encode())
, := io.WriteString(, )
if != nil {
return fmt.Errorf("failed to set cursor style: %w", )
}
return nil
}
func ( io.Writer, bool) error {
var string
if {
= ansi.SetModeBracketedPaste
} else {
= ansi.ResetModeBracketedPaste
}
, := io.WriteString(, )
if != nil {
return fmt.Errorf("failed to set bracketed paste mode: %w", )
}
return nil
}
func ( io.Writer, MouseMode) error {
var string
switch {
case MouseModeNone:
= ansi.ResetModeMouseX10 +
ansi.ResetModeMouseNormal +
ansi.ResetModeMouseButtonEvent +
ansi.ResetModeMouseAnyEvent
case MouseModePress:
= ansi.SetModeMouseX10
case MouseModeClick:
= ansi.SetModeMouseNormal
case MouseModeDrag:
= ansi.SetModeMouseButtonEvent
case MouseModeMotion:
= ansi.SetModeMouseAnyEvent
}
, := io.WriteString(, )
if != nil {
return fmt.Errorf("failed to set mouse mode: %w", )
}
return nil
}
func ( io.Writer, MouseEncoding) error {
var string
switch {
case MouseEncodingLegacy:
= ansi.ResetModeMouseExtSgr +
ansi.ResetModeMouseExtUrxvt +
ansi.ResetModeMouseExtSgrPixel
case MouseEncodingSGR:
= ansi.SetModeMouseExtSgr
case MouseEncodingSGRPixel:
= ansi.SetModeMouseExtSgrPixel
}
, := io.WriteString(, )
if != nil {
return fmt.Errorf("failed to set mouse encoding: %w", )
}
return nil
}
func ( io.Writer, *ProgressBar) error {
if == nil {
= &ProgressBar{State: ProgressBarNone}
}
var string
:= clamp(.Value, 0, 100)
switch .State {
case ProgressBarNone:
= ansi.ResetProgressBar
case ProgressBarDefault:
= ansi.SetProgressBar()
case ProgressBarError:
= ansi.SetErrorProgressBar()
case ProgressBarIndeterminate:
= ansi.SetIndeterminateProgressBar
case ProgressBarWarning:
= ansi.SetWarningProgressBar()
default:
return fmt.Errorf("invalid progress bar state: %d", .State)
}
, := io.WriteString(, )
if != nil {
return fmt.Errorf("failed to set progress bar: %w", )
}
return nil
}
func ( io.Writer, *KeyboardEnhancements) error {
var int
if != nil {
= .Flags()
}
, := io.WriteString(, ansi.KittyKeyboard(, 1))
if != nil {
return fmt.Errorf("failed to set keyboard enhancements: %w", )
}
return nil
}
func ( io.Writer, string) error {
, := io.WriteString(, ansi.SetWindowTitle())
if != nil {
return fmt.Errorf("failed to set window title: %w", )
}
return nil
}