Make the loop API a thin wrapper around internal methods

This commit is contained in:
Kovid Goyal 2022-09-01 12:14:03 +05:30
parent 7325921070
commit 67115530b4
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 56 additions and 35 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/base64"
"fmt"
"kitty/tools/tty"
"os"
"time"
"golang.org/x/sys/unix"
@ -44,6 +45,7 @@ type Loop struct {
timers, timers_temp []*timer
timer_id_counter, write_msg_id_counter IdType
wakeup_channel chan byte
signal_channel chan os.Signal
pending_writes []*write_msg
// Callbacks
@ -72,45 +74,19 @@ type Loop struct {
}
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
l.escape_code_parser.HandleAPC = l.handle_apc
l.escape_code_parser.HandleSOS = l.handle_sos
l.escape_code_parser.HandlePM = l.handle_pm
l.escape_code_parser.HandleRune = l.handle_rune
l := new_loop()
for _, f := range options {
f(&l)
f(l)
}
return &l, nil
return l, nil
}
func (self *Loop) AddTimer(interval time.Duration, repeats bool, callback TimerCallback) (IdType, error) {
if self.timers == nil {
return 0, fmt.Errorf("Cannot add timers before starting the run loop, add them in OnInitialize instead")
}
self.timer_id_counter++
t := timer{interval: interval, repeats: repeats, callback: callback, id: self.timer_id_counter}
t.update_deadline(time.Now())
self.timers = append(self.timers, &t)
self.sort_timers()
return t.id, nil
return self.add_timer(interval, repeats, callback)
}
func (self *Loop) RemoveTimer(id IdType) bool {
if self.timers == nil {
return false
}
for i := 0; i < len(self.timers); i++ {
if self.timers[i].id == id {
self.timers = append(self.timers[:i], self.timers[i+1:]...)
return true
}
}
return false
return self.remove_timer(id)
}
func (self *Loop) NoAlternateScreen() *Loop {

View File

@ -19,6 +19,21 @@ import (
var SIGNULL unix.Signal
func new_loop() *Loop {
l := Loop{controlling_term: nil, timers_temp: make([]*timer, 4)}
l.terminal_options.alternate_screen = true
l.terminal_options.restore_colors = true
l.terminal_options.kitty_keyboard_mode = 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
l.escape_code_parser.HandleAPC = l.handle_apc
l.escape_code_parser.HandleSOS = l.handle_sos
l.escape_code_parser.HandlePM = l.handle_pm
l.escape_code_parser.HandleRune = l.handle_rune
return &l
}
func is_temporary_error(err error) bool {
return errors.Is(err, unix.EINTR) || errors.Is(err, unix.EAGAIN) || errors.Is(err, unix.EWOULDBLOCK) || errors.Is(err, io.ErrShortWrite)
}
@ -60,7 +75,7 @@ func (self *Loop) handle_csi(raw []byte) error {
}
func (self *Loop) handle_key_event(ev *KeyEvent) error {
// self.DebugPrintln(ev)
self.DebugPrintln(ev)
if self.OnKeyEvent != nil {
err := self.OnKeyEvent(ev)
if err != nil {
@ -163,6 +178,10 @@ func (self *Loop) on_SIGTERM() error {
}
func (self *Loop) on_SIGTSTP() error {
signal.Reset(unix.SIGTSTP)
unix.Kill(os.Getpid(), unix.SIGTSTP)
time.Sleep(20 * time.Millisecond)
signal.Notify(self.signal_channel, unix.SIGTSTP)
return nil
}
@ -173,9 +192,9 @@ func (self *Loop) on_SIGHUP() error {
}
func (self *Loop) run() (err error) {
sigchnl := make(chan os.Signal, 256)
self.signal_channel = make(chan os.Signal, 256)
handled_signals := []os.Signal{unix.SIGINT, unix.SIGTERM, unix.SIGTSTP, unix.SIGHUP, unix.SIGWINCH, unix.SIGPIPE}
signal.Notify(sigchnl, handled_signals...)
signal.Notify(self.signal_channel, handled_signals...)
defer signal.Reset(handled_signals...)
controlling_term, err := tty.OpenControllingTerm()
@ -277,7 +296,7 @@ func (self *Loop) run() (err error) {
return err
}
}
case s := <-sigchnl:
case s := <-self.signal_channel:
err = self.on_signal(s.(unix.Signal))
if err != nil {
return err

View File

@ -3,10 +3,36 @@
package loop
import (
"fmt"
"sort"
"time"
)
func (self *Loop) add_timer(interval time.Duration, repeats bool, callback TimerCallback) (IdType, error) {
if self.timers == nil {
return 0, fmt.Errorf("Cannot add timers before starting the run loop, add them in OnInitialize instead")
}
self.timer_id_counter++
t := timer{interval: interval, repeats: repeats, callback: callback, id: self.timer_id_counter}
t.update_deadline(time.Now())
self.timers = append(self.timers, &t)
self.sort_timers()
return t.id, nil
}
func (self *Loop) remove_timer(id IdType) bool {
if self.timers == nil {
return false
}
for i := 0; i < len(self.timers); i++ {
if self.timers[i].id == id {
self.timers = append(self.timers[:i], self.timers[i+1:]...)
return true
}
}
return false
}
func (self *Loop) dispatch_timers(now time.Time) error {
updated := false
self.timers_temp = self.timers_temp[:0]