This commit is contained in:
2026-03-26 11:50:27 +09:00
parent d9d75b9305
commit 24cdd59e12
6 changed files with 124 additions and 155 deletions

View File

@@ -2,9 +2,7 @@ package console
import (
"fmt"
"io"
"os"
"unicode/utf8"
"golang.org/x/term"
)
@@ -25,7 +23,7 @@ func Raw() {
func Cooked() {
if state == nil {
panic("state is nil")
panic("invalid state")
}
term.Restore(int(os.Stdin.Fd()), state)
}
@@ -53,47 +51,7 @@ func ShowCursor() {
func Size() (int, int) {
w, h, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
panic(err)
return 80, 24
}
return w, h
}
func runeSize(b byte) int {
switch {
case b&0x80 == 0:
return 1
case b&0xe0 == 0xc0:
return 2
case b&0xf0 == 0xe0:
return 3
case b&0xf8 == 0xf0:
return 4
default:
return -1 // invalid
}
}
func ReadRune() rune {
buf := make([]byte, 1)
_, err := io.ReadFull(os.Stdin, buf)
if err != nil {
panic(err)
}
expected := runeSize(buf[0])
if expected == -1 {
panic("Invalid UTF-8 head")
}
full := make([]byte, expected)
full[0] = buf[0]
if expected > 1 {
_, err := io.ReadFull(os.Stdin, full[1:])
if err != nil {
panic(err)
}
}
r, size := utf8.DecodeRune(full)
if r == utf8.RuneError && size == 1 {
panic("Invalid UTF-8 body")
}
return r
}