Added input and libc files, updated basic example
parent
281f038f5f
commit
5032b04a47
@ -1,23 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"git.metznet.ca/MetzNet/go-ncurses"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := ncurses.Init()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ncurses.SetLocale(0, "")
|
||||
input, input_active := ncurses.UTF8Listener(100, os.Stdin)
|
||||
|
||||
window := ncurses.InitScr.Load()()
|
||||
os_sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(os_sigs, syscall.SIGINT, syscall.SIGABRT)
|
||||
|
||||
x := ncurses.GetMaxX.Load()(window)
|
||||
y := ncurses.GetMaxY.Load()(window)
|
||||
window := ncurses.InitScr()
|
||||
ret := ncurses.IdlOK(window, true)
|
||||
if ret != 0 {
|
||||
panic(ret)
|
||||
}
|
||||
ret = ncurses.ScrollOk(window, true)
|
||||
if ret != 0 {
|
||||
panic(ret)
|
||||
}
|
||||
ncurses.WRefresh(window)
|
||||
|
||||
fmt.Printf("%d x %d\n", x, y)
|
||||
active := true
|
||||
for active {
|
||||
select {
|
||||
case bytes := <-input:
|
||||
ncurses.WAddStr(window, string(bytes))
|
||||
ncurses.WRefresh(window)
|
||||
case <-os_sigs:
|
||||
active = false
|
||||
}
|
||||
}
|
||||
|
||||
ncurses.EndWin.Load()()
|
||||
ncurses.EndWin()
|
||||
input_active.Store(false)
|
||||
close(input)
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
package ncurses
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
func bitmatch(b byte, pattern byte, length int) bool {
|
||||
mask := ^(byte(1 << (8 - length)) - 1)
|
||||
return (b ^ pattern) & mask == 0
|
||||
}
|
||||
|
||||
func UTF8Listen(file *os.File, channel chan []byte, active *atomic.Bool) {
|
||||
b := [4]byte{}
|
||||
for active.Load() {
|
||||
file.Read(b[0:1])
|
||||
if bitmatch(b[0], 0b00000000, 1) {
|
||||
channel <- b[0:1]
|
||||
} else if bitmatch(b[0], 0b11000000, 3) {
|
||||
file.Read(b[1:2])
|
||||
channel <- b[0:2]
|
||||
} else if bitmatch(b[0], 0b11100000, 4) {
|
||||
file.Read(b[1:3])
|
||||
channel <- b[0:3]
|
||||
} else if bitmatch(b[0], 0b11110000, 5) {
|
||||
file.Read(b[1:4])
|
||||
channel <- b[0:4]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// To cleanup, set active to false then close the channel
|
||||
func UTF8Listener(buffer int, file *os.File) (chan []byte, *atomic.Bool) {
|
||||
channel := make(chan []byte, buffer)
|
||||
active := new(atomic.Bool)
|
||||
active.Store(true)
|
||||
|
||||
go UTF8Listen(file, channel, active)
|
||||
|
||||
return channel, active
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package ncurses
|
||||
|
||||
import (
|
||||
"github.com/ebitengine/purego"
|
||||
)
|
||||
|
||||
var libc uintptr = 0
|
||||
func libcFunction[T any](name string) T {
|
||||
if libc == 0 {
|
||||
var err error
|
||||
libc, err = purego.Dlopen(LIBC_PATH, purego.RTLD_GLOBAL | purego.RTLD_NOW)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
var tmp = new(T)
|
||||
purego.RegisterLibFunc(tmp, libc, name)
|
||||
return *tmp
|
||||
}
|
||||
|
||||
type setlocaleFP func(category int, locale string)
|
||||
var SetLocale = libcFunction[setlocaleFP]("setlocale")
|
@ -1,3 +1,4 @@
|
||||
package ncurses
|
||||
|
||||
const LIBRARY_PATH = "libncurses.dylib"
|
||||
const LIBC_PATH = "libSystem.B.dylib"
|
||||
|
Loading…
Reference in New Issue