Extract termi

This commit is contained in:
2026-03-26 23:14:25 +09:00
parent 2480aedd9f
commit b571e36ed8
5 changed files with 27 additions and 119 deletions

7
go.mod
View File

@@ -4,7 +4,10 @@ go 1.25.0
require (
github.com/creack/pty v1.1.24
golang.org/x/term v0.41.0
tea.kareha.org/lab/termi v0.0.0-20260326135653-28299eeba224
)
require golang.org/x/sys v0.42.0 // indirect
require (
golang.org/x/sys v0.42.0 // indirect
golang.org/x/term v0.41.0 // indirect
)

2
go.sum
View File

@@ -4,3 +4,5 @@ golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/term v0.41.0 h1:QCgPso/Q3RTJx2Th4bDLqML4W6iJiaXFq2/ftQF13YU=
golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A=
tea.kareha.org/lab/termi v0.0.0-20260326135653-28299eeba224 h1:nf3D+GjzIP9ab7fXIZmaFloRFD478SV4+bPhB9Wa1U0=
tea.kareha.org/lab/termi v0.0.0-20260326135653-28299eeba224/go.mod h1:+ticjUt1pyuink8Qip4QHN3GGz1QaNPRuJrM+jWRLgU=

View File

@@ -1,82 +0,0 @@
package console
import (
"fmt"
"os"
"golang.org/x/term"
)
var state *term.State
func Raw() {
if state != nil {
term.Restore(int(os.Stdin.Fd()), state)
state = nil
}
s, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
state = s
}
func Cooked() {
if state == nil {
panic("invalid state")
}
term.Restore(int(os.Stdin.Fd()), state)
state = nil
}
func Clear() {
fmt.Print("\x1b[2J")
}
func HomeCursor() {
fmt.Print("\x1b[H")
}
func MoveCursor(x, y int) {
fmt.Printf("\x1b[%d;%dH", y+1, x+1)
}
func HideCursor() {
fmt.Print("\x1b[?25l")
}
func ShowCursor() {
fmt.Print("\x1b[?25h")
}
func Size() (int, int) {
w, h, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
return 80, 24
}
return w, h
}
func EnableInvert() {
fmt.Print("\x1b[7m")
}
func DisableInvert() {
fmt.Print("\x1b[0m")
}
func SaveCursor() {
fmt.Print("\x1b[s")
}
func LoadCursor() {
fmt.Print("\x1b[u")
}
func ScrollRange(top, bottom int) {
fmt.Printf("\x1b[%d;%dr", top+1, bottom)
}
func ClearLine() {
fmt.Print("\x1b[K")
}

View File

@@ -1,15 +0,0 @@
package console
import (
"fmt"
)
func Print(s string) {
fmt.Print(s)
}
func Printf(format string, a ...any) (n int, err error) {
s := fmt.Sprintf(format, a...)
Print(s)
return len(s), nil
}

View File

@@ -8,7 +8,7 @@ import (
"github.com/creack/pty"
"tea.kareha.org/lab/kakiko/internal/console"
"tea.kareha.org/lab/termi"
)
type Process func(b []byte) []byte
@@ -64,12 +64,12 @@ func Init(args []string, process Process, status Status) *FEP {
}
}()
_, h := console.Size()
console.ScrollRange(0, h-1)
_, h := termi.Size()
termi.ScrollRange(0, h-1)
console.Clear()
console.HomeCursor()
console.Raw()
termi.Clear()
termi.HomeCursor()
termi.Raw()
go func() {
for {
@@ -100,27 +100,27 @@ func Init(args []string, process Process, status Status) *FEP {
}
func (f *FEP) Finish() {
_, h := console.Size()
console.ScrollRange(0, h)
_, h := termi.Size()
termi.ScrollRange(0, h)
console.Clear()
console.HomeCursor()
console.Cooked()
console.ShowCursor()
termi.Clear()
termi.HomeCursor()
termi.Cooked()
termi.ShowCursor()
}
func (f *FEP) drawStatus() {
_, h := console.Size()
console.SaveCursor()
console.HideCursor()
console.MoveCursor(0, h-1)
_, h := termi.Size()
termi.SaveCursor()
termi.HideCursor()
termi.MoveCursor(0, h-1)
status := f.status()
console.Print(status)
console.ClearLine()
termi.Print(status)
termi.ClearTail()
console.ShowCursor()
console.LoadCursor()
termi.ShowCursor()
termi.LoadCursor()
}
func (f *FEP) Main() {