mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-20 23:44:59 +02:00
Convenient loop API to print styled strings
This commit is contained in:
@@ -5,13 +5,14 @@ package loop
|
|||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"kitty/tools/tty"
|
|
||||||
"kitty/tools/utils"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
|
"kitty/tools/tty"
|
||||||
|
"kitty/tools/utils"
|
||||||
|
"kitty/tools/utils/style"
|
||||||
"kitty/tools/wcswidth"
|
"kitty/tools/wcswidth"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,6 +60,8 @@ type Loop struct {
|
|||||||
pending_writes []*write_msg
|
pending_writes []*write_msg
|
||||||
pending_mouse_events *utils.RingBuffer[MouseEvent]
|
pending_mouse_events *utils.RingBuffer[MouseEvent]
|
||||||
on_SIGTSTP func() error
|
on_SIGTSTP func() error
|
||||||
|
style_cache map[string]func(...any) string
|
||||||
|
style_ctx style.Context
|
||||||
|
|
||||||
// Suspend the loop restoring terminal state. Call the return resume function to restore the loop
|
// Suspend the loop restoring terminal state. Call the return resume function to restore the loop
|
||||||
Suspend func() (func() error, error)
|
Suspend func() (func() error, error)
|
||||||
@@ -201,6 +204,19 @@ func (self *Loop) Println(args ...any) {
|
|||||||
self.QueueWriteString("\r\n")
|
self.QueueWriteString("\r\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *Loop) SprintStyled(style string, args ...any) string {
|
||||||
|
f := self.style_cache[style]
|
||||||
|
if f == nil {
|
||||||
|
f = self.style_ctx.SprintFunc(style)
|
||||||
|
self.style_cache[style] = f
|
||||||
|
}
|
||||||
|
return f(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *Loop) PrintStyled(style string, args ...any) {
|
||||||
|
self.QueueWriteString(self.SprintStyled(style, args...))
|
||||||
|
}
|
||||||
|
|
||||||
func (self *Loop) SaveCursorPosition() {
|
func (self *Loop) SaveCursorPosition() {
|
||||||
self.QueueWriteString("\x1b7")
|
self.QueueWriteString("\x1b7")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ func new_loop() *Loop {
|
|||||||
l.escape_code_parser.HandlePM = l.handle_pm
|
l.escape_code_parser.HandlePM = l.handle_pm
|
||||||
l.escape_code_parser.HandleRune = l.handle_rune
|
l.escape_code_parser.HandleRune = l.handle_rune
|
||||||
l.escape_code_parser.HandleEndOfBracketedPaste = l.handle_end_of_bracketed_paste
|
l.escape_code_parser.HandleEndOfBracketedPaste = l.handle_end_of_bracketed_paste
|
||||||
|
l.style_cache = make(map[string]func(...any) string)
|
||||||
|
l.style_ctx.AllowEscapeCodes = true
|
||||||
return &l
|
return &l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user