Refactor tty code into its own package

This commit is contained in:
Kovid Goyal 2022-08-22 21:20:54 +05:30
parent 13758e9600
commit 246277e7af
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 24 additions and 19 deletions

View File

@ -19,6 +19,7 @@ import (
"kitty/tools/base85" "kitty/tools/base85"
"kitty/tools/cli" "kitty/tools/cli"
"kitty/tools/crypto" "kitty/tools/crypto"
"kitty/tools/tty"
"kitty/tools/utils" "kitty/tools/utils"
) )
@ -183,8 +184,8 @@ func get_response(rc *utils.RemoteControlCmd, timeout float64) (ans *Response, e
} }
var device IOAbstraction var device IOAbstraction
if global_options.to_network == "" { if global_options.to_network == "" {
var term *utils.Term var term *tty.Term
term, err = utils.OpenControllingTerm(utils.SetRaw) term, err = tty.OpenControllingTerm(tty.SetRaw)
if err != nil { if err != nil {
return return
} }

View File

@ -1,4 +1,4 @@
package utils package tty
import ( import (
"encoding/base64" "encoding/base64"
@ -6,10 +6,11 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"syscall"
"time" "time"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"kitty/tools/utils"
) )
const ( const (
@ -188,7 +189,7 @@ func (self *Term) ReadWithTimeout(b []byte, d time.Duration) (n int, err error)
write.Zero() write.Zero()
in_err.Zero() in_err.Zero()
read.Set(self.fd) read.Set(self.fd)
return Select(self.fd+1, &read, &write, &in_err, d) return utils.Select(self.fd+1, &read, &write, &in_err, d)
} }
num_ready, err := pselect() num_ready, err := pselect()
if err != nil { if err != nil {
@ -229,16 +230,6 @@ func (self *Term) Write(b []byte) (int, error) {
return n, nil return n, nil
} }
func NsecToTimespec(d time.Duration) unix.Timespec {
nv := syscall.NsecToTimespec(int64(d))
return unix.Timespec{Sec: nv.Sec, Nsec: nv.Nsec}
}
func NsecToTimeval(d time.Duration) unix.Timeval {
nv := syscall.NsecToTimeval(int64(d))
return unix.Timeval{Sec: nv.Sec, Usec: nv.Usec}
}
func (self *Term) DebugPrintln(a ...interface{}) { func (self *Term) DebugPrintln(a ...interface{}) {
msg := []byte(fmt.Sprintln(a...)) msg := []byte(fmt.Sprintln(a...))
for i := 0; i < len(msg); i += 256 { for i := 0; i < len(msg); i += 256 {
@ -264,7 +255,7 @@ func (self *Term) WriteAllWithTimeout(b []byte, d time.Duration) (n int, err err
read.Zero() read.Zero()
in_err.Zero() in_err.Zero()
write.Set(self.fd) write.Set(self.fd)
return Select(self.fd+1, &read, &write, &in_err, d) return utils.Select(self.fd+1, &read, &write, &in_err, d)
} }
for { for {
if len(b) == 0 { if len(b) == 0 {
@ -297,7 +288,7 @@ func (self *Term) WriteAllWithTimeout(b []byte, d time.Duration) (n int, err err
} }
} }
func (self *Term) WriteFromReader(r Reader, read_timeout time.Duration, write_timeout time.Duration) (n int, err error) { func (self *Term) WriteFromReader(r utils.Reader, read_timeout time.Duration, write_timeout time.Duration) (n int, err error) {
buf := r.GetBuf() buf := r.GetBuf()
var rn, wn int var rn, wn int
var rerr error var rerr error

View File

@ -1,7 +1,7 @@
//go:build darwin || freebsd || openbsd || netbsd || dragonfly //go:build darwin || freebsd || openbsd || netbsd || dragonfly
// +build darwin freebsd openbsd netbsd dragonfly // +build darwin freebsd openbsd netbsd dragonfly
package utils package tty
import ( import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"

View File

@ -1,4 +1,4 @@
package utils package tty
import "golang.org/x/sys/unix" import "golang.org/x/sys/unix"

View File

@ -2,13 +2,26 @@ package utils
import ( import (
"io" "io"
"syscall"
"time" "time"
"golang.org/x/sys/unix"
) )
const ( const (
DEFAULT_IO_BUFFER_SIZE = 8192 DEFAULT_IO_BUFFER_SIZE = 8192
) )
func NsecToTimespec(d time.Duration) unix.Timespec {
nv := syscall.NsecToTimespec(int64(d))
return unix.Timespec{Sec: nv.Sec, Nsec: nv.Nsec}
}
func NsecToTimeval(d time.Duration) unix.Timeval {
nv := syscall.NsecToTimeval(int64(d))
return unix.Timeval{Sec: nv.Sec, Usec: nv.Usec}
}
type BytesReader struct { type BytesReader struct {
Data []byte Data []byte
} }