Merge branch 'v2-exp' into v2-viewable
This commit is contained in:
commit
c76fa36f1b
6 changed files with 69 additions and 33 deletions
|
|
@ -48,6 +48,10 @@ func (s *cursedRenderer) close() (err error) {
|
|||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
// Reset cursor state to ensure we restore the cursor shape and style when
|
||||
// we restore the terminal.
|
||||
s.cursor = Cursor{}
|
||||
|
||||
if err := s.scr.Close(); err != nil {
|
||||
return fmt.Errorf("bubbletea: error closing screen writer: %w", err)
|
||||
}
|
||||
|
|
@ -59,9 +63,8 @@ func (s *cursedRenderer) flush() error {
|
|||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
// Render and queue changes to the screen buffer.
|
||||
s.scr.Render()
|
||||
if s.lastCur != nil {
|
||||
// Set the cursor style and shape before rendering the screen cells.
|
||||
if s.lastCur != nil { //nolint:nestif
|
||||
if s.lastCur.Shape != s.cursor.Shape || s.lastCur.Blink != s.cursor.Blink {
|
||||
cursorStyle := encodeCursorStyle(s.lastCur.Shape, s.lastCur.Blink)
|
||||
_, _ = io.WriteString(s.w, ansi.SetCursorStyle(cursorStyle))
|
||||
|
|
@ -79,7 +82,11 @@ func (s *cursedRenderer) flush() error {
|
|||
_, _ = io.WriteString(s.w, seq)
|
||||
s.cursor.Color = s.lastCur.Color
|
||||
}
|
||||
}
|
||||
|
||||
// Render and queue changes to the screen buffer.
|
||||
s.scr.Render()
|
||||
if s.lastCur != nil {
|
||||
// MoveTo must come after [cellbuf.Screen.Render] because the cursor
|
||||
// position might get updated during rendering.
|
||||
s.scr.MoveTo(s.lastCur.X, s.lastCur.Y)
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ func (m model) Init() tea.Cmd {
|
|||
// Attempt to enable keyboard enhancements. By default, this just
|
||||
// enables key disabiguation. For key releases, you'll need to opt-in
|
||||
// to that feature.
|
||||
tea.RequestKeyboardEnhancements(tea.WithKeyReleases),
|
||||
tea.RequestKeyReleases,
|
||||
|
||||
tea.RequestBackgroundColor,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ func (m model) View() string {
|
|||
}
|
||||
|
||||
func main() {
|
||||
p := tea.NewProgram(model{}, tea.WithKeyboardEnhancements(tea.WithKeyReleases, tea.WithUniformKeyLayout))
|
||||
p := tea.NewProgram(model{}, tea.WithKeyReleases(), tea.WithUniformKeyLayout())
|
||||
if _, err := p.Run(); err != nil {
|
||||
log.Printf("Error running program: %v", err)
|
||||
}
|
||||
|
|
|
|||
48
keyboard.go
48
keyboard.go
|
|
@ -43,24 +43,24 @@ type KeyboardEnhancements struct {
|
|||
// KeyboardEnhancementOption is a type that represents a keyboard enhancement.
|
||||
type KeyboardEnhancementOption func(k *KeyboardEnhancements)
|
||||
|
||||
// WithKeyReleases enables support for reporting release key events. This is
|
||||
// withKeyReleases enables support for reporting release key events. This is
|
||||
// useful for terminals that support the Kitty keyboard protocol "Report event
|
||||
// types" progressive enhancement feature.
|
||||
//
|
||||
// Note that not all terminals support this feature.
|
||||
func WithKeyReleases(k *KeyboardEnhancements) {
|
||||
func withKeyReleases(k *KeyboardEnhancements) {
|
||||
k.kittyFlags |= ansi.KittyReportEventTypes
|
||||
k.keyReleases = true
|
||||
}
|
||||
|
||||
// WithUniformKeyLayout enables support for reporting key events as though they
|
||||
// withUniformKeyLayout enables support for reporting key events as though they
|
||||
// were on a PC-101 layout. This is useful for uniform key event reporting
|
||||
// across different keyboard layouts. This is equivalent to the Kitty keyboard
|
||||
// protocol "Report alternate keys" and "Report all keys as escape codes"
|
||||
// progressive enhancement features.
|
||||
//
|
||||
// Note that not all terminals support this feature.
|
||||
func WithUniformKeyLayout(k *KeyboardEnhancements) {
|
||||
func withUniformKeyLayout(k *KeyboardEnhancements) {
|
||||
k.kittyFlags |= ansi.KittyReportAlternateKeys | ansi.KittyReportAllKeysAsEscapeCodes
|
||||
}
|
||||
|
||||
|
|
@ -77,25 +77,41 @@ func withKeyDisambiguation(k *KeyboardEnhancements) {
|
|||
|
||||
type enableKeyboardEnhancementsMsg []KeyboardEnhancementOption
|
||||
|
||||
// RequestKeyboardEnhancements is a command that enables keyboard enhancements
|
||||
// in the terminal.
|
||||
// RequestKeyDisambiguation is a command that enables support for reporting
|
||||
// disambiguous keys as escape codes. This is enabled by default in Bubble Tea
|
||||
// and there's no need to call this function unless you disabled keyboard
|
||||
// enhancements through [DisableKeyboardEnhancements].
|
||||
//
|
||||
// This command can be used to request specific keyboard enhancements. Use this
|
||||
// command to request enabling support for key disambiguation. You can also
|
||||
// request other enhancements by passing additional options. For example:
|
||||
// If the terminal supports the requested enhancements, it will send a
|
||||
// [KeyboardEnhancementsMsg] message with the supported enhancements.
|
||||
//
|
||||
// - [WithKeyReleases] enables support for reporting release key events.
|
||||
// - [WithUniformKeyLayout] enables support for reporting key events as though
|
||||
// they were on a PC-101 layout.
|
||||
// Note that not all terminals support this feature. If the terminal does not
|
||||
// support this feature, the program will not receive disambiguated key
|
||||
// events.
|
||||
func RequestKeyDisambiguation() Msg {
|
||||
return enableKeyboardEnhancementsMsg{withKeyDisambiguation}
|
||||
}
|
||||
|
||||
// RequestKeyReleases is a command that enables support for reporting key
|
||||
// release events.
|
||||
//
|
||||
// If the terminal supports the requested enhancements, it will send a
|
||||
// [KeyboardEnhancementsMsg] message with the supported enhancements.
|
||||
//
|
||||
// Note that not all terminals support all enhancements.
|
||||
func RequestKeyboardEnhancements(enhancements ...KeyboardEnhancementOption) Cmd {
|
||||
return func() Msg {
|
||||
return enableKeyboardEnhancementsMsg(append(enhancements, withKeyDisambiguation))
|
||||
}
|
||||
func RequestKeyReleases() Msg {
|
||||
return enableKeyboardEnhancementsMsg{withKeyReleases}
|
||||
}
|
||||
|
||||
// RequestUniformKeyLayout is a command that enables support for reporting key
|
||||
// events as though they were on a PC-101 layout.
|
||||
//
|
||||
// If the terminal supports the requested enhancements, it will send a
|
||||
// [KeyboardEnhancementsMsg] message with the supported enhancements.
|
||||
//
|
||||
// Note that not all terminals support all enhancements.
|
||||
func RequestUniformKeyLayout() Msg {
|
||||
return enableKeyboardEnhancementsMsg{withUniformKeyLayout}
|
||||
}
|
||||
|
||||
type disableKeyboardEnhancementsMsg struct{}
|
||||
|
|
|
|||
35
options.go
35
options.go
|
|
@ -6,6 +6,7 @@ import (
|
|||
"sync/atomic"
|
||||
|
||||
"github.com/charmbracelet/colorprofile"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
)
|
||||
|
||||
// ProgramOption is used to set options when initializing a Program. Program can
|
||||
|
|
@ -225,19 +226,31 @@ func WithReportFocus() ProgramOption {
|
|||
}
|
||||
}
|
||||
|
||||
// WithKeyboardEnhancements enables support for enhanced keyboard features. You
|
||||
// can enable different keyboard features by passing one or more
|
||||
// KeyboardEnhancement functions.
|
||||
// WithKeyReleases enables support for reporting key release events. This is
|
||||
// useful for terminals that support the Kitty keyboard protocol "Report event
|
||||
// types" progressive enhancement feature.
|
||||
//
|
||||
// This is not supported on all terminals. On Windows, these features are
|
||||
// enabled by default.
|
||||
func WithKeyboardEnhancements(enhancements ...KeyboardEnhancementOption) ProgramOption {
|
||||
var ke KeyboardEnhancements
|
||||
for _, e := range append(enhancements, withKeyDisambiguation) {
|
||||
e(&ke)
|
||||
}
|
||||
// Note that not all terminals support this feature. If the terminal does not
|
||||
// support this feature, the program will not receive key release events.
|
||||
func WithKeyReleases() ProgramOption {
|
||||
return func(p *Program) {
|
||||
p.requestedEnhancements = ke
|
||||
p.requestedEnhancements.kittyFlags |= ansi.KittyReportEventTypes
|
||||
p.requestedEnhancements.keyReleases = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithUniformKeyLayout enables support for reporting key events as though they
|
||||
// were on a PC-101 layout. This is useful for uniform key event reporting
|
||||
// across different keyboard layouts. This is equivalent to the Kitty keyboard
|
||||
// protocol "Report alternate keys" and "Report all keys as escape codes"
|
||||
// progressive enhancement features.
|
||||
//
|
||||
// Note that not all terminals support this feature. If the terminal does not
|
||||
// support this feature, the program will not receive key events in
|
||||
// uniform layout format.
|
||||
func WithUniformKeyLayout() ProgramOption {
|
||||
return func(p *Program) {
|
||||
p.requestedEnhancements.kittyFlags |= ansi.KittyReportAlternateKeys | ansi.KittyReportAllKeysAsEscapeCodes
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ func TestClearMsg(t *testing.T) {
|
|||
},
|
||||
{
|
||||
name: "kitty_start",
|
||||
cmds: []Cmd{DisableKeyboardEnhancements, RequestKeyboardEnhancements(WithKeyReleases)},
|
||||
cmds: []Cmd{DisableKeyboardEnhancements, RequestKeyReleases},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue