mirror of
				https://github.com/sammy-ette/Hilbish
				synced 2025-08-10 02:52:03 +00:00 
			
		
		
		
	refactor: rewrite terminal library with moonlight
This commit is contained in:
		
							parent
							
								
									80e6dedf9e
								
							
						
					
					
						commit
						f37f5b7ddc
					
				@ -5,52 +5,47 @@ package terminal
 | 
			
		||||
import (
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"hilbish/util"
 | 
			
		||||
	"hilbish/moonlight"
 | 
			
		||||
 | 
			
		||||
	rt "github.com/arnodel/golua/runtime"
 | 
			
		||||
	"github.com/arnodel/golua/lib/packagelib"
 | 
			
		||||
	"golang.org/x/term"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var termState *term.State
 | 
			
		||||
var Loader = packagelib.Loader{
 | 
			
		||||
	Load: loaderFunc,
 | 
			
		||||
	Name: "terminal",
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
 | 
			
		||||
	exports := map[string]util.LuaExport{
 | 
			
		||||
		"setRaw": util.LuaExport{termsetRaw, 0, false},
 | 
			
		||||
		"restoreState": util.LuaExport{termrestoreState, 0, false},
 | 
			
		||||
		"size": util.LuaExport{termsize, 0, false},
 | 
			
		||||
		"saveState": util.LuaExport{termsaveState, 0, false},
 | 
			
		||||
func Loader(rtm *moonlight.Runtime) moonlight.Value {
 | 
			
		||||
	exports := map[string]moonlight.Export{
 | 
			
		||||
		"setRaw": {termsetRaw, 0, false},
 | 
			
		||||
		"restoreState": {termrestoreState, 0, false},
 | 
			
		||||
		"size": {termsize, 0, false},
 | 
			
		||||
		"saveState": {termsaveState, 0, false},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	mod := rt.NewTable()
 | 
			
		||||
	util.SetExports(rtm, mod, exports)
 | 
			
		||||
	mod := moonlight.NewTable()
 | 
			
		||||
	rtm.SetExports(mod, exports)
 | 
			
		||||
 | 
			
		||||
	return rt.TableValue(mod), nil
 | 
			
		||||
	return moonlight.TableValue(mod)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// size()
 | 
			
		||||
// Gets the dimensions of the terminal. Returns a table with `width` and `height`
 | 
			
		||||
// NOTE: The size refers to the amount of columns and rows of text that can fit in the terminal.
 | 
			
		||||
func termsize(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
 | 
			
		||||
func termsize(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
 | 
			
		||||
	w, h, err := term.GetSize(int(os.Stdin.Fd()))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dimensions := rt.NewTable()
 | 
			
		||||
	dimensions := moonlight.NewTable()
 | 
			
		||||
	dimensions.Set(rt.StringValue("width"), rt.IntValue(int64(w)))
 | 
			
		||||
	dimensions.Set(rt.StringValue("height"), rt.IntValue(int64(h)))
 | 
			
		||||
 | 
			
		||||
	return c.PushingNext1(t.Runtime, rt.TableValue(dimensions)), nil
 | 
			
		||||
	return mlr.PushNext1(c, moonlight.TableValue(dimensions)), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// saveState()
 | 
			
		||||
// Saves the current state of the terminal.
 | 
			
		||||
func termsaveState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
 | 
			
		||||
func termsaveState(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
 | 
			
		||||
	state, err := term.GetState(int(os.Stdin.Fd()))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@ -62,7 +57,7 @@ func termsaveState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
 | 
			
		||||
 | 
			
		||||
// restoreState()
 | 
			
		||||
// Restores the last saved state of the terminal
 | 
			
		||||
func termrestoreState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
 | 
			
		||||
func termrestoreState(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
 | 
			
		||||
	err := term.Restore(int(os.Stdin.Fd()), termState)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@ -73,7 +68,7 @@ func termrestoreState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
 | 
			
		||||
 | 
			
		||||
// setRaw()
 | 
			
		||||
// Puts the terminal into raw mode.
 | 
			
		||||
func termsetRaw(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
 | 
			
		||||
func termsetRaw(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
 | 
			
		||||
	_, err := term.MakeRaw(int(os.Stdin.Fd()))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										8
									
								
								lua.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								lua.go
									
									
									
									
									
								
							@ -8,7 +8,7 @@ import (
 | 
			
		||||
	"hilbish/golibs/bait"
 | 
			
		||||
	"hilbish/golibs/commander"
 | 
			
		||||
	"hilbish/golibs/fs"
 | 
			
		||||
	//"hilbish/golibs/terminal"
 | 
			
		||||
	"hilbish/golibs/terminal"
 | 
			
		||||
 | 
			
		||||
	"hilbish/moonlight"
 | 
			
		||||
)
 | 
			
		||||
@ -26,9 +26,9 @@ func luaInit() {
 | 
			
		||||
	// Add fs and terminal module module to Lua
 | 
			
		||||
	f := fs.New(runner)
 | 
			
		||||
	l.LoadLibrary(f.Loader, "fs")
 | 
			
		||||
	/*
 | 
			
		||||
	lib.LoadLibs(l, terminal.Loader)
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
	l.LoadLibrary(terminal.Loader, "terminal")
 | 
			
		||||
 | 
			
		||||
	cmds = commander.New(l)
 | 
			
		||||
	l.LoadLibrary(cmds.Loader, "commander")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user