Add initial version
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/kakiko
|
||||
7
LICENSE
Normal file
7
LICENSE
Normal file
@@ -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.
|
||||
16
Makefile
Normal file
16
Makefile
Normal file
@@ -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 ./...
|
||||
124
cmd/kakiko/main.go
Normal file
124
cmd/kakiko/main.go
Normal file
@@ -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
|
||||
}
|
||||
10
go.mod
Normal file
10
go.mod
Normal file
@@ -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
|
||||
6
go.sum
Normal file
6
go.sum
Normal file
@@ -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=
|
||||
77
internal/console/console.go
Normal file
77
internal/console/console.go
Normal file
@@ -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)
|
||||
}
|
||||
15
internal/console/output.go
Normal file
15
internal/console/output.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user