From fc41b5bd436a8d01bbdcda3304602814b2bb2207 Mon Sep 17 00:00:00 2001 From: Aki Kareha Date: Thu, 26 Mar 2026 16:22:37 +0900 Subject: [PATCH] Add initial version --- .gitignore | 1 + LICENSE | 7 ++ Makefile | 16 +++++ README | 1 + cmd/kakiko/main.go | 124 ++++++++++++++++++++++++++++++++++++ go.mod | 10 +++ go.sum | 6 ++ internal/console/console.go | 77 ++++++++++++++++++++++ internal/console/output.go | 15 +++++ 9 files changed, 257 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README create mode 100644 cmd/kakiko/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/console/console.go create mode 100644 internal/console/output.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa573ee --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/kakiko diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..82c2ddc --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2026 Aki Kareha + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dda371a --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +all: build + +build: + go build -o kakiko ./cmd/kakiko + +clean: + rm -f kakiko + +run: + go run ./cmd/kakiko + +fmt: + go fmt ./... + +test: + go test ./... diff --git a/README b/README new file mode 100644 index 0000000..e2764dc --- /dev/null +++ b/README @@ -0,0 +1 @@ +kakiko - An SKK Front End Processor for Text Terminals diff --git a/cmd/kakiko/main.go b/cmd/kakiko/main.go new file mode 100644 index 0000000..2aa6d03 --- /dev/null +++ b/cmd/kakiko/main.go @@ -0,0 +1,124 @@ +package main + +import ( + "os" + "os/exec" + "os/signal" + "syscall" + + "github.com/creack/pty" + + "tea.kareha.org/lab/kakiko/internal/console" +) + +var defaultCommand []string = []string{ + "/bin/sh", +} + +func main() { + var cmd []string + if len(os.Args) > 1 { + cmd = os.Args[1:] + } else { + cmd = defaultCommand + } + var c *exec.Cmd + if len(cmd) < 2 { + c = exec.Command(cmd[0]) + } else { + c = exec.Command(cmd[0], cmd[1:]...) + } + + ptmx, err := pty.Start(c) + if err != nil { + panic(err) + } + + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGWINCH) + + go func() { + for range ch { + rows, cols, err := pty.Getsize(os.Stdin) + if err != nil { + panic(err) + } + pty.Setsize(ptmx, &pty.Winsize{ + Rows: uint16(rows - 1), + Cols: uint16(cols), + }) + } + }() + rows, cols, err := pty.Getsize(os.Stdin) + if err != nil { + panic(err) + } + pty.Setsize(ptmx, &pty.Winsize{ + Rows: uint16(rows - 1), + Cols: uint16(cols), + }) + + console.ScrollRange(0, rows-1) + defer console.ScrollRange(0, rows) + + console.Clear() + console.HomeCursor() + console.Raw() + defer func() { + console.Clear() + console.HomeCursor() + console.Cooked() + console.ShowCursor() + }() + + go func() { + for { + b := make([]byte, 1) + n, err := os.Stdin.Read(b) + if err != nil { + return + } + if n == 0 { + continue + } + + processed := skkProcess(b) + + _, err = ptmx.Write(processed) + if err != nil { + return + } + } + }() + + go func() { + c.Wait() + os.Exit(0) + }() + + buf := make([]byte, 1024) + for { + n, err := ptmx.Read(buf) + if err != nil { + return + } + + os.Stdout.Write(buf[:n]) + + drawStatus() + } +} + +func drawStatus() { + _, h := console.Size() + console.SaveCursor() + console.HideCursor() + console.MoveCursor(0, h-1) + console.Print("Hello, World!") + console.ShowCursor() + console.LoadCursor() +} + +func skkProcess(b []byte) []byte { + return b +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3f848d5 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module tea.kareha.org/lab/kakiko + +go 1.25.0 + +require ( + github.com/creack/pty v1.1.24 + golang.org/x/term v0.41.0 +) + +require golang.org/x/sys v0.42.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f4245ea --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s= +github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= +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= diff --git a/internal/console/console.go b/internal/console/console.go new file mode 100644 index 0000000..0f70b36 --- /dev/null +++ b/internal/console/console.go @@ -0,0 +1,77 @@ +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) +} + +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) +} diff --git a/internal/console/output.go b/internal/console/output.go new file mode 100644 index 0000000..5899bd1 --- /dev/null +++ b/internal/console/output.go @@ -0,0 +1,15 @@ +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 +}