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

View File

@ -1,4 +1,4 @@
package utils
package tty
import (
"encoding/base64"
@ -6,10 +6,11 @@ import (
"fmt"
"io"
"os"
"syscall"
"time"
"golang.org/x/sys/unix"
"kitty/tools/utils"
)
const (
@ -188,7 +189,7 @@ func (self *Term) ReadWithTimeout(b []byte, d time.Duration) (n int, err error)
write.Zero()
in_err.Zero()
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()
if err != nil {
@ -229,16 +230,6 @@ func (self *Term) Write(b []byte) (int, error) {
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{}) {
msg := []byte(fmt.Sprintln(a...))
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()
in_err.Zero()
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 {
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()
var rn, wn int
var rerr error

View File

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

View File

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

View File

@ -2,13 +2,26 @@ package utils
import (
"io"
"syscall"
"time"
"golang.org/x/sys/unix"
)
const (
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 {
Data []byte
}