Nicer API for setting loop options
This commit is contained in:
parent
93d08011f1
commit
5cf10023c9
3 changed files with 38 additions and 12 deletions
|
|
@ -71,9 +71,10 @@ type Loop struct {
|
|||
OnReceivedData func(data []byte) error
|
||||
}
|
||||
|
||||
func New() (*Loop, error) {
|
||||
func New(options ...func(self *Loop)) (*Loop, error) {
|
||||
l := Loop{controlling_term: nil, timers_temp: make([]*timer, 4)}
|
||||
l.terminal_options.alternate_screen = true
|
||||
l.terminal_options.restore_colors = true
|
||||
l.escape_code_parser.HandleCSI = l.handle_csi
|
||||
l.escape_code_parser.HandleOSC = l.handle_osc
|
||||
l.escape_code_parser.HandleDCS = l.handle_dcs
|
||||
|
|
@ -81,6 +82,9 @@ func New() (*Loop, error) {
|
|||
l.escape_code_parser.HandleSOS = l.handle_sos
|
||||
l.escape_code_parser.HandlePM = l.handle_pm
|
||||
l.escape_code_parser.HandleRune = l.handle_rune
|
||||
for _, f := range options {
|
||||
f(&l)
|
||||
}
|
||||
return &l, nil
|
||||
}
|
||||
|
||||
|
|
@ -109,12 +113,31 @@ func (self *Loop) RemoveTimer(id IdType) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (self *Loop) NoAlternateScreen() {
|
||||
func (self *Loop) NoAlternateScreen() *Loop {
|
||||
self.terminal_options.alternate_screen = false
|
||||
return self
|
||||
}
|
||||
|
||||
func NoAlternateScreen(self *Loop) {
|
||||
self.terminal_options.alternate_screen = false
|
||||
}
|
||||
|
||||
func (self *Loop) MouseTracking(mt MouseTracking) {
|
||||
func (self *Loop) MouseTrackingMode(mt MouseTracking) *Loop {
|
||||
self.terminal_options.mouse_tracking = mt
|
||||
return self
|
||||
}
|
||||
|
||||
func MouseTrackingMode(self *Loop, mt MouseTracking) {
|
||||
self.terminal_options.mouse_tracking = mt
|
||||
}
|
||||
|
||||
func (self *Loop) NoRestoreColors() *Loop {
|
||||
self.terminal_options.restore_colors = false
|
||||
return self
|
||||
}
|
||||
|
||||
func NoRestoreColors(self *Loop) {
|
||||
self.terminal_options.restore_colors = false
|
||||
}
|
||||
|
||||
func (self *Loop) DeathSignalName() string {
|
||||
|
|
|
|||
|
|
@ -76,8 +76,8 @@ const (
|
|||
)
|
||||
|
||||
type TerminalStateOptions struct {
|
||||
alternate_screen, no_kitty_keyboard_mode bool
|
||||
mouse_tracking MouseTracking
|
||||
alternate_screen, kitty_keyboard_mode, restore_colors bool
|
||||
mouse_tracking MouseTracking
|
||||
}
|
||||
|
||||
func set_modes(sb *strings.Builder, modes ...Mode) {
|
||||
|
|
@ -100,7 +100,9 @@ func (self *TerminalStateOptions) SetStateEscapeCodes() []byte {
|
|||
sb.WriteString(SAVE_CURSOR)
|
||||
}
|
||||
sb.WriteString(SAVE_PRIVATE_MODE_VALUES)
|
||||
sb.WriteString(SAVE_COLORS)
|
||||
if self.restore_colors {
|
||||
sb.WriteString(SAVE_COLORS)
|
||||
}
|
||||
sb.WriteString(DECSACE_DEFAULT_REGION_SELECT)
|
||||
reset_modes(&sb,
|
||||
IRM, DECKM, DECSCNM, BRACKETED_PASTE, FOCUS_TRACKING,
|
||||
|
|
@ -110,10 +112,10 @@ func (self *TerminalStateOptions) SetStateEscapeCodes() []byte {
|
|||
set_modes(&sb, ALTERNATE_SCREEN)
|
||||
sb.WriteString(CLEAR_SCREEN)
|
||||
}
|
||||
if self.no_kitty_keyboard_mode {
|
||||
sb.WriteString("\033[>u")
|
||||
} else {
|
||||
if self.kitty_keyboard_mode {
|
||||
sb.WriteString("\033[>31u")
|
||||
} else {
|
||||
sb.WriteString("\033[>u")
|
||||
}
|
||||
if self.mouse_tracking != NO_MOUSE_TRACKING {
|
||||
sb.WriteString(MOUSE_SGR_PIXEL_MODE.EscapeCodeToSet())
|
||||
|
|
@ -139,7 +141,9 @@ func (self *TerminalStateOptions) ResetStateEscapeCodes() []byte {
|
|||
sb.WriteString(SAVE_CURSOR)
|
||||
}
|
||||
sb.WriteString(RESTORE_PRIVATE_MODE_VALUES)
|
||||
sb.WriteString(RESTORE_CURSOR)
|
||||
if self.restore_colors {
|
||||
sb.WriteString(RESTORE_CURSOR)
|
||||
}
|
||||
sb.WriteString(RESTORE_COLORS)
|
||||
return []byte(sb.String())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,8 +21,7 @@ func (self *KilledBySignal) Error() string { return self.Msg }
|
|||
var Canceled = errors.New("Canceled by user")
|
||||
|
||||
func ReadPassword(prompt string, kill_if_signaled bool) (password string, err error) {
|
||||
lp, err := loop.New()
|
||||
lp.NoAlternateScreen()
|
||||
lp, err := loop.New(loop.NoAlternateScreen, loop.NoRestoreColors)
|
||||
shadow := ""
|
||||
if err != nil {
|
||||
return
|
||||
|
|
|
|||
Loading…
Reference in a new issue