diff --git a/aliases.go b/aliases.go index 3007cc3..87757c9 100644 --- a/aliases.go +++ b/aliases.go @@ -9,40 +9,40 @@ import ( rt "github.com/arnodel/golua/runtime" ) -var aliases *aliasHandler +var aliases *aliasModule -type aliasHandler struct { +type aliasModule struct { aliases map[string]string mu *sync.RWMutex } // initialize aliases map -func newAliases() *aliasHandler { - return &aliasHandler{ +func newAliases() *aliasModule { + return &aliasModule{ aliases: make(map[string]string), mu: &sync.RWMutex{}, } } -func (a *aliasHandler) Add(alias, cmd string) { +func (a *aliasModule) Add(alias, cmd string) { a.mu.Lock() defer a.mu.Unlock() a.aliases[alias] = cmd } -func (a *aliasHandler) All() map[string]string { +func (a *aliasModule) All() map[string]string { return a.aliases } -func (a *aliasHandler) Delete(alias string) { +func (a *aliasModule) Delete(alias string) { a.mu.Lock() defer a.mu.Unlock() delete(a.aliases, alias) } -func (a *aliasHandler) Resolve(cmdstr string) string { +func (a *aliasModule) Resolve(cmdstr string) string { a.mu.RLock() defer a.mu.RUnlock() @@ -66,7 +66,9 @@ func (a *aliasHandler) Resolve(cmdstr string) string { // lua section -func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table { +// #interface +// ALIAS LOADER TEST +func (a *aliasModule) Loader(rtm *rt.Runtime) *rt.Table { // create a lua module with our functions hshaliasesLua := map[string]util.LuaExport{ "add": util.LuaExport{hlalias, 2, false}, @@ -81,7 +83,7 @@ func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table { return mod } -func (a *aliasHandler) luaList(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func (a *aliasModule) luaList(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { aliasesList := rt.NewTable() for k, v := range a.All() { aliasesList.Set(rt.StringValue(k), rt.StringValue(v)) @@ -90,7 +92,7 @@ func (a *aliasHandler) luaList(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return c.PushingNext1(t.Runtime, rt.TableValue(aliasesList)), nil } -func (a *aliasHandler) luaDelete(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func (a *aliasModule) luaDelete(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.Check1Arg(); err != nil { return nil, err } @@ -103,7 +105,7 @@ func (a *aliasHandler) luaDelete(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return c.Next(), nil } -func (a *aliasHandler) luaResolve(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func (a *aliasModule) luaResolve(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.Check1Arg(); err != nil { return nil, err } diff --git a/api.go b/api.go index b4f2d1d..57993f6 100644 --- a/api.go +++ b/api.go @@ -159,10 +159,10 @@ func hilbishLoad(rtm *rt.Runtime) (rt.Value, func()) { mod.Set(rt.StringValue("jobs"), rt.TableValue(jobModule)) // hilbish.timers table - timers = newTimerHandler() - timerModule := timers.loader(rtm) - util.Document(timerModule, "Timer interface, for control of all intervals and timeouts.") - mod.Set(rt.StringValue("timers"), rt.TableValue(timerModule)) + timers = newTimersModule() + timersModule := timers.loader(rtm) + util.Document(timersModule, "Timer interface, for control of all intervals and timeouts.") + mod.Set(rt.StringValue("timers"), rt.TableValue(timersModule)) editorModule := editorLoader(rtm) util.Document(editorModule, "") diff --git a/cmd/docgen/docgen.go b/cmd/docgen/docgen.go index b39b016..99b5188 100644 --- a/cmd/docgen/docgen.go +++ b/cmd/docgen/docgen.go @@ -26,9 +26,10 @@ type emmyPiece struct { } type module struct { - Docs []docPiece + Docs map[string]docPiece ShortDescription string Description string + Interface bool } type docPiece struct { Doc []string @@ -36,6 +37,57 @@ type docPiece struct { FuncName string } + +var docs = make(map[string]module) +var emmyDocs = make(map[string][]emmyPiece) +var prefix = map[string]string{ + "main": "hl", + "hilbish": "hl", + "fs": "f", + "commander": "c", + "bait": "b", + "terminal": "term", +} + +func setupDoc(mod string, fun *doc.Func) *docPiece { + if !strings.HasPrefix(fun.Name, "hl") && mod == "main" { + return nil + } + if !strings.HasPrefix(fun.Name, prefix[mod]) || fun.Name == "Loader" { + return nil + } + parts := strings.Split(strings.TrimSpace(fun.Doc), "\n") + funcsig := parts[0] + doc := parts[1:] + funcdoc := []string{} + em := emmyPiece{FuncName: strings.TrimPrefix(fun.Name, prefix[mod])} + for _, d := range doc { + if strings.HasPrefix(d, "---") { + emmyLine := strings.TrimSpace(strings.TrimPrefix(d, "---")) + emmyLinePieces := strings.Split(emmyLine, " ") + emmyType := emmyLinePieces[0] + if emmyType == "@param" { + em.Params = append(em.Params, emmyLinePieces[1]) + } + if emmyType == "@vararg" { + em.Params = append(em.Params, "...") // add vararg + } + em.Docs = append(em.Docs, d) + } else { + funcdoc = append(funcdoc, d) + } + } + + dps := docPiece{ + Doc: funcdoc, + FuncSig: funcsig, + FuncName: strings.TrimPrefix(fun.Name, prefix[mod]), + } + + emmyDocs[mod] = append(emmyDocs[mod], em) + return &dps +} + // feel free to clean this up // it works, dont really care about the code func main() { @@ -65,88 +117,22 @@ func main() { } } - prefix := map[string]string{ - "hilbish": "hl", - "fs": "f", - "commander": "c", - "bait": "b", - "terminal": "term", - } - docs := make(map[string]module) - emmyDocs := make(map[string][]emmyPiece) - for l, f := range pkgs { p := doc.New(f, "./", doc.AllDecls) - pieces := []docPiece{} + pieces := make(map[string]docPiece) mod := l for _, t := range p.Funcs { - if strings.HasPrefix(t.Name, "hl") { mod = "hilbish" } - if !strings.HasPrefix(t.Name, "hl") && l == "main" { continue } - if !strings.HasPrefix(t.Name, prefix[mod]) || t.Name == "Loader" { continue } - parts := strings.Split(strings.TrimSpace(t.Doc), "\n") - funcsig := parts[0] - doc := parts[1:] - funcdoc := []string{} - em := emmyPiece{FuncName: strings.TrimPrefix(t.Name, prefix[mod])} - for _, d := range doc { - if strings.HasPrefix(d, "---") { - emmyLine := strings.TrimSpace(strings.TrimPrefix(d, "---")) - emmyLinePieces := strings.Split(emmyLine, " ") - emmyType := emmyLinePieces[0] - if emmyType == "@param" { - em.Params = append(em.Params, emmyLinePieces[1]) - } - if emmyType == "@vararg" { - em.Params = append(em.Params, "...") // add vararg - } - em.Docs = append(em.Docs, d) - } else { - funcdoc = append(funcdoc, d) - } + piece := setupDoc(mod, t) + if piece != nil { + pieces[piece.FuncName] = *piece } - - dps := docPiece{ - Doc: funcdoc, - FuncSig: funcsig, - FuncName: strings.TrimPrefix(t.Name, prefix[mod]), - } - - pieces = append(pieces, dps) - emmyDocs[mod] = append(emmyDocs[mod], em) } for _, t := range p.Types { for _, m := range t.Methods { - if !strings.HasPrefix(t.Name, "hl") && l == "main" { continue } - if !strings.HasPrefix(m.Name, prefix[l]) || m.Name == "Loader" { continue } - parts := strings.Split(strings.TrimSpace(m.Doc), "\n") - funcsig := parts[0] - doc := parts[1:] - funcdoc := []string{} - em := emmyPiece{FuncName: strings.TrimPrefix(m.Name, prefix[l])} - for _, d := range doc { - if strings.HasPrefix(d, "---") { - emmyLine := strings.TrimSpace(strings.TrimPrefix(d, "---")) - emmyLinePieces := strings.Split(emmyLine, " ") - emmyType := emmyLinePieces[0] - if emmyType == "@param" { - em.Params = append(em.Params, emmyLinePieces[1]) - } - if emmyType == "@vararg" { - em.Params = append(em.Params, "...") // add vararg - } - em.Docs = append(em.Docs, d) - } else { - funcdoc = append(funcdoc, d) - } + piece := setupDoc(mod, m) + if piece != nil { + pieces[piece.FuncName] = *piece } - dps := docPiece{ - Doc: funcdoc, - FuncSig: funcsig, - FuncName: strings.TrimPrefix(m.Name, prefix[l]), - } - - pieces = append(pieces, dps) - emmyDocs[l] = append(emmyDocs[l], em) } } @@ -161,9 +147,13 @@ func main() { } for mod, v := range docs { - if mod == "main" { continue } - f, _ := os.Create("docs/api/" + mod + ".md") - f.WriteString(fmt.Sprintf(header, mod, v.ShortDescription)) + modN := mod + if mod == "main" { + modN = "hilbish" + } + fmt.Println(mod) + f, _ := os.Create("docs/api/" + modN + ".md") + f.WriteString(fmt.Sprintf(header, modN, v.ShortDescription)) f.WriteString(fmt.Sprintf("## Introduction\n%s\n\n## Functions\n", v.Description)) for _, dps := range v.Docs { f.WriteString(fmt.Sprintf("### %s\n", dps.FuncSig)) @@ -174,25 +164,18 @@ func main() { } f.WriteString("\n") } - } - - for mod, v := range emmyDocs { - if mod == "main" { continue } - f, _ := os.Create("emmyLuaDocs/" + mod + ".lua") - f.WriteString("--- @meta\n\nlocal " + mod + " = {}\n\n") - for _, em := range v { - var funcdocs []string - for _, dps := range docs[mod].Docs { - if dps.FuncName == em.FuncName { - funcdocs = dps.Doc - } - } - f.WriteString("--- " + strings.Join(funcdocs, "\n--- ") + "\n") + + ff, _ := os.Create("emmyLuaDocs/" + modN + ".lua") + ff.WriteString("--- @meta\n\nlocal " + modN + " = {}\n\n") + for _, em := range emmyDocs[mod] { + funcdocs := v.Docs[em.FuncName].Doc + fmt.Println(funcdocs) + ff.WriteString("--- " + strings.Join(funcdocs, "\n--- ") + "\n") if len(em.Docs) != 0 { - f.WriteString(strings.Join(em.Docs, "\n") + "\n") + ff.WriteString(strings.Join(em.Docs, "\n") + "\n") } - f.WriteString("function " + mod + "." + em.FuncName + "(" + strings.Join(em.Params, ", ") + ") end\n\n") + ff.WriteString("function " + modN + "." + em.FuncName + "(" + strings.Join(em.Params, ", ") + ") end\n\n") } - f.WriteString("return " + mod + "\n") + ff.WriteString("return " + modN + "\n") } } diff --git a/docs/api/bait.md b/docs/api/bait.md index 2eaaffe..575a265 100644 --- a/docs/api/bait.md +++ b/docs/api/bait.md @@ -12,9 +12,6 @@ happened, like when you've changed directory, a command has failed, etc. To find all available hooks thrown by Hilbish, see doc hooks. ## Functions -### catch(name, cb) -Catches a hook with `name`. Runs the `cb` when it is thrown - ### catchOnce(name, cb) Same as catch, but only runs the `cb` once and then removes the hook @@ -29,3 +26,6 @@ an event, like one saved to a variable. ### throw(name, ...args) Throws a hook with `name` with the provided `args` +### catch(name, cb) +Catches a hook with `name`. Runs the `cb` when it is thrown + diff --git a/docs/api/fs.md b/docs/api/fs.md index 3800422..da9b005 100644 --- a/docs/api/fs.md +++ b/docs/api/fs.md @@ -10,9 +10,6 @@ and other things, and acts an addition to the Lua standard library's I/O and filesystem functions. ## Functions -### abs(path) -Gives an absolute version of `path`. - ### basename(path) Gives the basename of `path`. For the rules, see Go's filepath.Base @@ -24,6 +21,12 @@ Changes directory to `dir` Returns the directory part of `path`. For the rules, see Go's filepath.Dir +### readdir(dir) +Returns a table of files in `dir` + +### abs(path) +Gives an absolute version of `path`. + ### glob(pattern) Glob all files and directories that match the pattern. For the rules, see Go's filepath.Glob @@ -35,9 +38,6 @@ directory separator (forward or backward slash). ### mkdir(name, recursive) Makes a directory called `name`. If `recursive` is true, it will create its parent directories. -### readdir(dir) -Returns a table of files in `dir` - ### stat(path) Returns info about `path` diff --git a/docs/api/hilbish.md b/docs/api/hilbish.md index 96746ae..3e0739f 100644 --- a/docs/api/hilbish.md +++ b/docs/api/hilbish.md @@ -9,34 +9,19 @@ The Hilbish module includes the core API, containing interfaces and functions which directly relate to shell functionality. ## Functions -### alias(cmd, orig) -Sets an alias of `cmd` to `orig` +### runnerMode(mode) +Sets the execution/runner mode for interactive Hilbish. This determines whether +Hilbish wll try to run input as Lua and/or sh or only do one of either. +Accepted values for mode are hybrid (the default), hybridRev (sh first then Lua), +sh, and lua. It also accepts a function, to which if it is passed one +will call it to execute user input instead. ### appendPath(dir) Appends `dir` to $PATH -### complete(scope, cb) -Registers a completion handler for `scope`. -A `scope` is currently only expected to be `command.`, -replacing with the name of the command (for example `command.git`). -`cb` must be a function that returns a table of "completion groups." -Check `doc completions` for more information. - -### cwd() -Returns the current directory of the shell - -### exec(cmd) -Replaces running hilbish with `cmd` - ### goro(fn) Puts `fn` in a goroutine -### highlighter(line) -Line highlighter handler. This is mainly for syntax highlighting, but in -reality could set the input of the prompt to *display* anything. The -callback is passed the current line and is expected to return a line that -will be used as the input display. - ### hinter(line, pos) The command line hint handler. It gets called on every key insert to determine what text to use as an inline hint. It is passed the current @@ -44,6 +29,34 @@ line and cursor position. It is expected to return a string which is used as the text for the hint. This is by default a shim. To set hints, override this function with your custom handler. +### read(prompt?) -> input? +Read input from the user, using Hilbish's line editor/input reader. +This is a separate instance from the one Hilbish actually uses. +Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen) + +### run(cmd, returnOut) -> exitCode, stdout, stderr +Runs `cmd` in Hilbish's sh interpreter. +If returnOut is true, the outputs of `cmd` will be returned as the 2nd and +3rd values instead of being outputted to the terminal. + +### prompt(str, typ?) +Changes the shell prompt to `str` +There are a few verbs that can be used in the prompt text. +These will be formatted and replaced with the appropriate values. +`%d` - Current working directory +`%u` - Name of current user +`%h` - Hostname of device + +### timeout(cb, time) +Runs the `cb` function after `time` in milliseconds +Returns a `timer` object (see `doc timers`). + +### which(name) +Checks if `name` is a valid command + +### exec(cmd) +Replaces running hilbish with `cmd` + ### inputMode(mode) Sets the input mode for Hilbish's line reader. Accepts either emacs or vim @@ -57,35 +70,22 @@ Changes the continued line prompt to `str` ### prependPath(dir) Prepends `dir` to $PATH -### prompt(str, typ?) -Changes the shell prompt to `str` -There are a few verbs that can be used in the prompt text. -These will be formatted and replaced with the appropriate values. -`%d` - Current working directory -`%u` - Name of current user -`%h` - Hostname of device +### complete(scope, cb) +Registers a completion handler for `scope`. +A `scope` is currently only expected to be `command.`, +replacing with the name of the command (for example `command.git`). +`cb` must be a function that returns a table of "completion groups." +Check `doc completions` for more information. -### read(prompt?) -> input? -Read input from the user, using Hilbish's line editor/input reader. -This is a separate instance from the one Hilbish actually uses. -Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen) +### highlighter(line) +Line highlighter handler. This is mainly for syntax highlighting, but in +reality could set the input of the prompt to *display* anything. The +callback is passed the current line and is expected to return a line that +will be used as the input display. -### run(cmd, returnOut) -> exitCode, stdout, stderr -Runs `cmd` in Hilbish's sh interpreter. -If returnOut is true, the outputs of `cmd` will be returned as the 2nd and -3rd values instead of being outputted to the terminal. +### alias(cmd, orig) +Sets an alias of `cmd` to `orig` -### runnerMode(mode) -Sets the execution/runner mode for interactive Hilbish. This determines whether -Hilbish wll try to run input as Lua and/or sh or only do one of either. -Accepted values for mode are hybrid (the default), hybridRev (sh first then Lua), -sh, and lua. It also accepts a function, to which if it is passed one -will call it to execute user input instead. - -### timeout(cb, time) -Runs the `cb` function after `time` in milliseconds -Returns a `timer` object (see `doc timers`). - -### which(name) -Checks if `name` is a valid command +### cwd() +Returns the current directory of the shell diff --git a/docs/api/main.md b/docs/api/main.md new file mode 100644 index 0000000..3dcdc30 --- /dev/null +++ b/docs/api/main.md @@ -0,0 +1,91 @@ +--- +name: Module main +description: the core Hilbish API +layout: apidoc +--- + +## Introduction +The Hilbish module includes the core API, containing +interfaces and functions which directly relate to shell functionality. + +## Functions +### interval(cb, time) +Runs the `cb` function every `time` milliseconds. +Returns a `timer` object (see `doc timers`). + +### timeout(cb, time) +Runs the `cb` function after `time` in milliseconds +Returns a `timer` object (see `doc timers`). + +### cwd() +Returns the current directory of the shell + +### goro(fn) +Puts `fn` in a goroutine + +### prompt(str, typ?) +Changes the shell prompt to `str` +There are a few verbs that can be used in the prompt text. +These will be formatted and replaced with the appropriate values. +`%d` - Current working directory +`%u` - Name of current user +`%h` - Hostname of device + +### read(prompt?) -> input? +Read input from the user, using Hilbish's line editor/input reader. +This is a separate instance from the one Hilbish actually uses. +Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen) + +### run(cmd, returnOut) -> exitCode, stdout, stderr +Runs `cmd` in Hilbish's sh interpreter. +If returnOut is true, the outputs of `cmd` will be returned as the 2nd and +3rd values instead of being outputted to the terminal. + +### hinter(line, pos) +The command line hint handler. It gets called on every key insert to +determine what text to use as an inline hint. It is passed the current +line and cursor position. It is expected to return a string which is used +as the text for the hint. This is by default a shim. To set hints, +override this function with your custom handler. + +### inputMode(mode) +Sets the input mode for Hilbish's line reader. Accepts either emacs or vim + +### multiprompt(str) +Changes the continued line prompt to `str` + +### prependPath(dir) +Prepends `dir` to $PATH + +### runnerMode(mode) +Sets the execution/runner mode for interactive Hilbish. This determines whether +Hilbish wll try to run input as Lua and/or sh or only do one of either. +Accepted values for mode are hybrid (the default), hybridRev (sh first then Lua), +sh, and lua. It also accepts a function, to which if it is passed one +will call it to execute user input instead. + +### appendPath(dir) +Appends `dir` to $PATH + +### highlighter(line) +Line highlighter handler. This is mainly for syntax highlighting, but in +reality could set the input of the prompt to *display* anything. The +callback is passed the current line and is expected to return a line that +will be used as the input display. + +### exec(cmd) +Replaces running hilbish with `cmd` + +### which(name) +Checks if `name` is a valid command + +### alias(cmd, orig) +Sets an alias of `cmd` to `orig` + +### complete(scope, cb) +Registers a completion handler for `scope`. +A `scope` is currently only expected to be `command.`, +replacing with the name of the command (for example `command.git`). +`cb` must be a function that returns a table of "completion groups." +Check `doc completions` for more information. + diff --git a/docs/api/terminal.md b/docs/api/terminal.md index f1007d8..1e49ffd 100644 --- a/docs/api/terminal.md +++ b/docs/api/terminal.md @@ -8,6 +8,10 @@ layout: apidoc The terminal library is a simple and lower level library for certain terminal interactions. ## Functions +### 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 + ### restoreState() Restores the last saved state of the terminal @@ -17,7 +21,3 @@ Saves the current state of the terminal ### setRaw() Puts the terminal in raw mode -### 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/emmyLuaDocs/main.lua b/emmyLuaDocs/main.lua new file mode 100644 index 0000000..540ea36 --- /dev/null +++ b/emmyLuaDocs/main.lua @@ -0,0 +1,110 @@ +--- @meta + +local main = {} + +--- Sets an alias of `cmd` to `orig` +--- @param cmd string +--- @param orig string +function main.hlalias(cmd, orig) end + +--- Appends `dir` to $PATH +--- @param dir string|table +function main.hlappendPath(dir) end + +--- Registers a completion handler for `scope`. +--- A `scope` is currently only expected to be `command.`, +--- replacing with the name of the command (for example `command.git`). +--- `cb` must be a function that returns a table of "completion groups." +--- Check `doc completions` for more information. +--- @param scope string +--- @param cb function +function main.hlcomplete(scope, cb) end + +--- Returns the current directory of the shell +function main.hlcwd() end + +--- Replaces running hilbish with `cmd` +--- @param cmd string +function main.hlexec(cmd) end + +--- Puts `fn` in a goroutine +--- @param fn function +function main.hlgoro(fn) end + +--- Line highlighter handler. This is mainly for syntax highlighting, but in +--- reality could set the input of the prompt to *display* anything. The +--- callback is passed the current line and is expected to return a line that +--- will be used as the input display. +--- @param line string +function main.hlhighlighter(line) end + +--- The command line hint handler. It gets called on every key insert to +--- determine what text to use as an inline hint. It is passed the current +--- line and cursor position. It is expected to return a string which is used +--- as the text for the hint. This is by default a shim. To set hints, +--- override this function with your custom handler. +--- @param line string +--- @param pos int +function main.hlhinter(line, pos) end + +--- Sets the input mode for Hilbish's line reader. Accepts either emacs or vim +--- @param mode string +function main.hlinputMode(mode) end + +--- Runs the `cb` function every `time` milliseconds. +--- Returns a `timer` object (see `doc timers`). +--- @param cb function +--- @param time number +--- @return table +function main.hlinterval(cb, time) end + +--- Changes the continued line prompt to `str` +--- @param str string +function main.hlmultiprompt(str) end + +--- Prepends `dir` to $PATH +--- @param dir string +function main.hlprependPath(dir) end + +--- Changes the shell prompt to `str` +--- There are a few verbs that can be used in the prompt text. +--- These will be formatted and replaced with the appropriate values. +--- `%d` - Current working directory +--- `%u` - Name of current user +--- `%h` - Hostname of device +--- @param str string +--- @param typ string Type of prompt, being left or right. Left by default. +function main.hlprompt(str, typ) end + +--- Read input from the user, using Hilbish's line editor/input reader. +--- This is a separate instance from the one Hilbish actually uses. +--- Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen) +--- @param prompt string +function main.hlread(prompt) end + +--- Runs `cmd` in Hilbish's sh interpreter. +--- If returnOut is true, the outputs of `cmd` will be returned as the 2nd and +--- 3rd values instead of being outputted to the terminal. +--- @param cmd string +function main.hlrun(cmd) end + +--- Sets the execution/runner mode for interactive Hilbish. This determines whether +--- Hilbish wll try to run input as Lua and/or sh or only do one of either. +--- Accepted values for mode are hybrid (the default), hybridRev (sh first then Lua), +--- sh, and lua. It also accepts a function, to which if it is passed one +--- will call it to execute user input instead. +--- @param mode string|function +function main.hlrunnerMode(mode) end + +--- Runs the `cb` function after `time` in milliseconds +--- Returns a `timer` object (see `doc timers`). +--- @param cb function +--- @param time number +--- @return table +function main.hltimeout(cb, time) end + +--- Checks if `name` is a valid command +--- @param binName string +function main.hlwhich(binName) end + +return main diff --git a/timer.go b/timer.go index 74d13c4..f5ab9a3 100644 --- a/timer.go +++ b/timer.go @@ -21,7 +21,7 @@ type timer struct{ running bool dur time.Duration fun *rt.Closure - th *timerHandler + th *timersModule ticker *time.Ticker ud *rt.UserData channel chan struct{} diff --git a/timerhandler.go b/timerhandler.go index 64caff8..29c7a41 100644 --- a/timerhandler.go +++ b/timerhandler.go @@ -10,10 +10,10 @@ import ( rt "github.com/arnodel/golua/runtime" ) -var timers *timerHandler +var timers *timersModule var timerMetaKey = rt.StringValue("hshtimer") -type timerHandler struct { +type timersModule struct { mu *sync.RWMutex wg *sync.WaitGroup timers map[int]*timer @@ -21,8 +21,8 @@ type timerHandler struct { running int } -func newTimerHandler() *timerHandler { - return &timerHandler{ +func newTimersModule() *timersModule { + return &timersModule{ timers: make(map[int]*timer), latestID: 0, mu: &sync.RWMutex{}, @@ -30,11 +30,11 @@ func newTimerHandler() *timerHandler { } } -func (th *timerHandler) wait() { +func (th *timersModule) wait() { th.wg.Wait() } -func (th *timerHandler) create(typ timerType, dur time.Duration, fun *rt.Closure) *timer { +func (th *timersModule) create(typ timerType, dur time.Duration, fun *rt.Closure) *timer { th.mu.Lock() defer th.mu.Unlock() @@ -54,14 +54,14 @@ func (th *timerHandler) create(typ timerType, dur time.Duration, fun *rt.Closure return t } -func (th *timerHandler) get(id int) *timer { +func (th *timersModule) get(id int) *timer { th.mu.RLock() defer th.mu.RUnlock() return th.timers[id] } -func (th *timerHandler) luaCreate(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func (th *timersModule) luaCreate(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.CheckNArgs(3); err != nil { return nil, err } @@ -83,7 +83,7 @@ func (th *timerHandler) luaCreate(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return c.PushingNext1(t.Runtime, rt.UserDataValue(tmr.ud)), nil } -func (th *timerHandler) luaGet(thr *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func (th *timersModule) luaGet(thr *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.Check1Arg(); err != nil { return nil, err } @@ -100,7 +100,7 @@ func (th *timerHandler) luaGet(thr *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return c.Next(), nil } -func (th *timerHandler) loader(rtm *rt.Runtime) *rt.Table { +func (th *timersModule) loader(rtm *rt.Runtime) *rt.Table { timerMethods := rt.NewTable() timerFuncs := map[string]util.LuaExport{ "start": {timerStart, 1, false},