diff --git a/cmd/docgen/docgen.go b/cmd/docgen/docgen.go index 6f28f35..c69fe84 100644 --- a/cmd/docgen/docgen.go +++ b/cmd/docgen/docgen.go @@ -43,6 +43,7 @@ func main() { "fs": "f", "commander": "c", "bait": "b", + "terminal": "term", } docs := make(map[string][]string) diff --git a/docs/terminal.txt b/docs/terminal.txt new file mode 100644 index 0000000..80af3f4 --- /dev/null +++ b/docs/terminal.txt @@ -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 + diff --git a/golibs/terminal/terminal.go b/golibs/terminal/terminal.go new file mode 100644 index 0000000..b52523e --- /dev/null +++ b/golibs/terminal/terminal.go @@ -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 +} diff --git a/lua.go b/lua.go index 04e7698..7d36ccd 100644 --- a/lua.go +++ b/lua.go @@ -11,6 +11,7 @@ import ( "hilbish/golibs/bait" "hilbish/golibs/commander" "hilbish/golibs/fs" + "hilbish/golibs/terminal" "github.com/yuin/gopher-lua" "layeh.com/gopher-luar" @@ -40,8 +41,9 @@ func LuaInit() { l.PreloadModule("hilbish", HilbishLoader) 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("terminal", terminal.Loader) cmds := commander.New() // When a command from Lua is added, register it for use