feat: add terminal package

a simple, smaller but lower level terminal library, mainly used to set
raw mode. functions:
size, save and restore current state, set raw mode. documentation is in
docs/terminal.txt or `doc terminal` in hilbish
pull/78/head
sammyette 2021-10-17 16:10:57 -04:00
parent 85b347d5f3
commit 3d53e85fc9
No known key found for this signature in database
GPG Key ID: 50EE40A2809851F5
4 changed files with 93 additions and 1 deletions

View File

@ -43,6 +43,7 @@ func main() {
"fs": "f", "fs": "f",
"commander": "c", "commander": "c",
"bait": "b", "bait": "b",
"terminal": "term",
} }
docs := make(map[string][]string) docs := make(map[string][]string)

View File

@ -0,0 +1,9 @@
setRaw() > Puts the terminal in raw mode
restoreState() > Restores the last saved state of the terminal
saveState() > Saves the current state of the terminal
size() > Gets the dimensions of the terminal. Returns a table with `width` and `height`
Note: this is not the size in relation to the dimensions of the display

View File

@ -0,0 +1,80 @@
package terminal
import (
"os"
"hilbish/util"
"golang.org/x/term"
"github.com/yuin/gopher-lua"
)
var termState *term.State
func Loader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports)
util.Document(L, mod, "The terminal library is a simple and lower level library for certain terminal interactions.")
L.Push(mod)
return 1
}
var exports = map[string]lua.LGFunction{
"setRaw": termraw,
"restoreState": termrestoreState,
"size": termsize,
"saveState": termsaveState,
}
// size()
// Gets the dimensions of the terminal. Returns a table with `width` and `height`
// Note: this is not the size in relation to the dimensions of the display
func termsize(L *lua.LState) int {
w, h, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
L.RaiseError(err.Error())
return 0
}
dimensions := L.NewTable()
L.SetField(dimensions, "width", lua.LNumber(w))
L.SetField(dimensions, "height", lua.LNumber(h))
L.Push(dimensions)
return 1
}
// saveState()
// Saves the current state of the terminal
func termsaveState(L *lua.LState) int {
state, err := term.GetState(int(os.Stdin.Fd()))
if err != nil {
L.RaiseError(err.Error())
return 0
}
termState = state
return 0
}
// restoreState()
// Restores the last saved state of the terminal
func termrestoreState(L *lua.LState) int {
err := term.Restore(int(os.Stdin.Fd()), termState)
if err != nil {
L.RaiseError(err.Error())
}
return 0
}
// setRaw()
// Puts the terminal in raw mode
func termraw(L *lua.LState) int {
_, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
L.RaiseError(err.Error())
}
return 0
}

4
lua.go
View File

@ -11,6 +11,7 @@ import (
"hilbish/golibs/bait" "hilbish/golibs/bait"
"hilbish/golibs/commander" "hilbish/golibs/commander"
"hilbish/golibs/fs" "hilbish/golibs/fs"
"hilbish/golibs/terminal"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
"layeh.com/gopher-luar" "layeh.com/gopher-luar"
@ -40,8 +41,9 @@ func LuaInit() {
l.PreloadModule("hilbish", HilbishLoader) l.PreloadModule("hilbish", HilbishLoader)
l.DoString("hilbish = require 'hilbish'") l.DoString("hilbish = require 'hilbish'")
// Add fs module to Lua // Add fs and terminal module module to Lua
l.PreloadModule("fs", fs.Loader) l.PreloadModule("fs", fs.Loader)
l.PreloadModule("terminal", terminal.Loader)
cmds := commander.New() cmds := commander.New()
// When a command from Lua is added, register it for use // When a command from Lua is added, register it for use