refactor: dont export functions and types

dev
TorchedSammy 2021-12-06 17:21:31 -04:00
parent 369ddcc100
commit 5c6310b1ca
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
6 changed files with 36 additions and 37 deletions

View File

@ -22,7 +22,7 @@ var exports = map[string]lua.LGFunction {
"read": hlread, "read": hlread,
} }
func HilbishLoader(L *lua.LState) int { func hilbishLoader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports) mod := L.SetFuncs(L.NewTable(), exports)
host, _ := os.Hostname() host, _ := os.Hostname()
@ -103,7 +103,7 @@ func getenv(key, fallback string) string {
// Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen) // Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen)
func hlread(L *lua.LState) int { func hlread(L *lua.LState) int {
luaprompt := L.CheckString(1) luaprompt := L.CheckString(1)
lualr := NewLineReader(luaprompt) lualr := newLineReader(luaprompt)
input, err := lualr.Read() input, err := lualr.Read()
if err != nil { if err != nil {

6
lua.go
View File

@ -34,7 +34,7 @@ func LuaInit() {
l.SetGlobal("interval", l.NewFunction(hshinterval)) l.SetGlobal("interval", l.NewFunction(hshinterval))
// yes this is stupid, i know // yes this is stupid, i know
l.PreloadModule("hilbish", HilbishLoader) l.PreloadModule("hilbish", hilbishLoader)
l.DoString("hilbish = require 'hilbish'") l.DoString("hilbish = require 'hilbish'")
// Add fs and terminal module module to Lua // Add fs and terminal module module to Lua
@ -75,7 +75,7 @@ func LuaInit() {
} }
} }
} }
func RunConfig(confpath string) { func runConfig(confpath string) {
if !interactive { if !interactive {
return return
} }
@ -88,7 +88,7 @@ func RunConfig(confpath string) {
} }
} }
func RunLogin() { func runLogin() {
if _, err := os.Stat(curuser.HomeDir + "/.hprofile.lua"); os.IsNotExist(err) { if _, err := os.Stat(curuser.HomeDir + "/.hprofile.lua"); os.IsNotExist(err) {
return return
} }

27
main.go
View File

@ -21,7 +21,7 @@ import (
var ( var (
l *lua.LState l *lua.LState
lr *LineReader lr *lineReader
commands = map[string]*lua.LFunction{} commands = map[string]*lua.LFunction{}
aliases = map[string]string{} aliases = map[string]string{}
@ -138,20 +138,20 @@ func main() {
} }
} }
go HandleSignals() go handleSignals()
LuaInit() LuaInit()
RunLogin() runLogin()
RunConfig(*configflag) runConfig(*configflag)
if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 { if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin)) scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
for scanner.Scan() { for scanner.Scan() {
RunInput(scanner.Text()) runInput(scanner.Text())
} }
} }
if *cmdflag != "" { if *cmdflag != "" {
RunInput(*cmdflag) runInput(*cmdflag)
} }
if getopt.NArgs() > 0 { if getopt.NArgs() > 0 {
@ -169,7 +169,7 @@ func main() {
os.Exit(0) os.Exit(0)
} }
lr = NewLineReader("") lr = newLineReader("")
input: input:
for interactive { for interactive {
running = false running = false
@ -194,7 +194,7 @@ input:
if strings.HasSuffix(input, "\\") { if strings.HasSuffix(input, "\\") {
for { for {
input, err = ContinuePrompt(strings.TrimSuffix(input, "\\")) input, err = continuePrompt(strings.TrimSuffix(input, "\\"))
if err != nil { if err != nil {
goto input // continue inside nested loop goto input // continue inside nested loop
} }
@ -203,8 +203,8 @@ input:
} }
} }
} }
HandleHistory(input) handleHistory(input)
RunInput(input) runInput(input)
termwidth, _, err := term.GetSize(0) termwidth, _, err := term.GetSize(0)
if err != nil { if err != nil {
@ -214,7 +214,7 @@ input:
} }
} }
func ContinuePrompt(prev string) (string, error) { func continuePrompt(prev string) (string, error) {
hooks.Em.Emit("multiline", nil) hooks.Em.Emit("multiline", nil)
lr.SetPrompt(multilinePrompt) lr.SetPrompt(multilinePrompt)
cont, err := lr.Read() cont, err := lr.Read()
@ -259,8 +259,7 @@ func fmtPrompt() string {
return nprompt return nprompt
} }
// do i even have to say func handleSignals() {
func HandleSignals() {
c := make(chan os.Signal) c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGWINCH) signal.Notify(c, os.Interrupt, syscall.SIGWINCH)
@ -280,7 +279,7 @@ func HandleSignals() {
} }
} }
func HandleHistory(cmd string) { func handleHistory(cmd string) {
lr.AddHistory(cmd) lr.AddHistory(cmd)
// TODO: load history again (history shared between sessions like this ye) // TODO: load history again (history shared between sessions like this ye)
} }

16
rl.go
View File

@ -16,11 +16,11 @@ import (
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
) )
type LineReader struct { type lineReader struct {
Prompt string Prompt string
} }
func NewLineReader(prompt string) *LineReader { func newLineReader(prompt string) *lineReader {
readline.Init() readline.Init()
readline.Completer = func(query string, ctx string) []string { readline.Completer = func(query string, ctx string) []string {
@ -154,30 +154,30 @@ func NewLineReader(prompt string) *LineReader {
} }
readline.LoadHistory(defaultHistPath) readline.LoadHistory(defaultHistPath)
return &LineReader{ return &lineReader{
Prompt: prompt, Prompt: prompt,
} }
} }
func (lr *LineReader) Read() (string, error) { func (lr *lineReader) Read() (string, error) {
hooks.Em.Emit("command.precmd", nil) hooks.Em.Emit("command.precmd", nil)
return readline.String(lr.Prompt) return readline.String(lr.Prompt)
} }
func (lr *LineReader) SetPrompt(prompt string) { func (lr *lineReader) SetPrompt(prompt string) {
lr.Prompt = prompt lr.Prompt = prompt
} }
func (lr *LineReader) AddHistory(cmd string) { func (lr *lineReader) AddHistory(cmd string) {
readline.AddHistory(cmd) readline.AddHistory(cmd)
readline.SaveHistory(defaultHistPath) readline.SaveHistory(defaultHistPath)
} }
func (lr *LineReader) ClearInput() { func (lr *lineReader) ClearInput() {
readline.ReplaceLine("", 0) readline.ReplaceLine("", 0)
readline.RefreshLine() readline.RefreshLine()
} }
func (lr *LineReader) Resize() { func (lr *lineReader) Resize() {
readline.Resize() readline.Resize()
} }

View File

@ -8,36 +8,36 @@ package main
import "github.com/Rosettea/Hilbiline" import "github.com/Rosettea/Hilbiline"
type LineReader struct { type lineReader struct {
hl *hilbiline.HilbilineState hl *hilbiline.HilbilineState
} }
// other gophers might hate this naming but this is local, shut up // other gophers might hate this naming but this is local, shut up
func NewLineReader(prompt string) *LineReader { func newLineReader(prompt string) *lineReader {
hl := hilbiline.New(prompt) hl := hilbiline.New(prompt)
return &LineReader{ return &lineReader{
&hl, &hl,
} }
} }
func (lr *LineReader) Read() (string, error) { func (lr *lineReader) Read() (string, error) {
return lr.hl.Read() return lr.hl.Read()
} }
func (lr *LineReader) SetPrompt(prompt string) { func (lr *lineReader) SetPrompt(prompt string) {
lr.hl.SetPrompt(prompt) lr.hl.SetPrompt(prompt)
} }
func (lr *LineReader) AddHistory(cmd string) { func (lr *lineReader) AddHistory(cmd string) {
return return
} }
func (lr *LineReader) ClearInput() { func (lr *lineReader) ClearInput() {
return return
} }
func (lr *LineReader) Resize() { func (lr *lineReader) Resize() {
return return
} }

View File

@ -14,7 +14,7 @@ import (
"mvdan.cc/sh/v3/syntax" "mvdan.cc/sh/v3/syntax"
) )
func RunInput(input string) { func runInput(input string) {
running = true running = true
cmdArgs, cmdString := splitInput(input) cmdArgs, cmdString := splitInput(input)
@ -61,7 +61,7 @@ func RunInput(input string) {
// If input is incomplete, start multiline prompting // If input is incomplete, start multiline prompting
if syntax.IsIncomplete(err) { if syntax.IsIncomplete(err) {
for { for {
cmdString, err = ContinuePrompt(strings.TrimSuffix(cmdString, "\\")) cmdString, err = continuePrompt(strings.TrimSuffix(cmdString, "\\"))
if err != nil { if err != nil {
break break
} }