This commit is contained in:
Robin Bärtschi 2025-01-30 11:09:44 +01:00
parent dbaa77aa1b
commit 4554892b6a
6 changed files with 64 additions and 9 deletions

3
go.mod
View File

@ -2,5 +2,4 @@ module robaertschi.xyz/robaertschi/tt
go 1.23.4
require (
)
require golang.org/x/sys v0.29.0 // indirect

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

12
main.go
View File

@ -14,8 +14,12 @@ import (
)
func main() {
r, c, err := term.GetCursorPosition()
fmt.Printf("%d, %d, %v\n", r, c, err)
err := term.EnterRawMode()
if err != nil {
fmt.Printf("could not enter raw mode %v", err)
return
}
defer term.LeaveRawMode()
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s [flags] input\nPossible flags:\n", os.Args[0])
@ -35,7 +39,7 @@ func main() {
input := flag.Arg(0)
if input == "" {
flag.Usage()
os.Exit(1)
term.Exit(1)
}
if output == "" {
@ -58,6 +62,6 @@ func main() {
err = build.NewSourceProgram(input, output).Build(asm.Fasm, *emitAsmOnly, build.ToPrintFlags(toPrint))
if err != nil {
logger.Fatalln(err)
os.Exit(1)
term.Exit(1)
}
}

View File

@ -3,7 +3,6 @@ package term
import (
"errors"
"fmt"
"internal/syscall/unix"
"os"
)
@ -184,3 +183,8 @@ func GetCursorPosition() (row, column int, err error) {
return
}
func Exit(val int) {
LeaveRawMode()
os.Exit(val)
}

47
term/term_unix.go Normal file
View File

@ -0,0 +1,47 @@
//go:build unix
// +build unix
package term
import (
"errors"
"golang.org/x/sys/unix"
)
var restore unix.Termios
var rawModeEnabled = false
func EnterRawMode() error {
termios, err := unix.IoctlGetTermios(unix.Stdin, unix.TCGETS)
if err != nil {
return err
}
restore = *termios
termios.Lflag = termios.Lflag &^ (unix.ECHO | unix.ICANON | unix.ISIG | unix.IEXTEN)
termios.Iflag = termios.Iflag &^ (unix.IXON | unix.ICRNL | unix.BRKINT | unix.INPCK | unix.ISTRIP)
termios.Cflag = termios.Cflag | unix.CS8
if err := unix.IoctlSetTermios(unix.Stdin, unix.TCSETSF, termios); err != nil {
return err
}
rawModeEnabled = true
return nil
}
func LeaveRawMode() error {
if !rawModeEnabled {
return errors.New("raw mode is not enabled")
}
err := unix.IoctlSetTermios(unix.Stdin, unix.TCSETSF, &restore)
if err != nil {
return err
}
rawModeEnabled = false
return nil
}

View File

@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"os"
"strings"
"sync"
@ -128,7 +127,7 @@ func (l *Logger) Msg(level Level, msg string) {
}
if level == Fatal {
os.Exit(1)
term.Exit(1)
}
}