Skip to content

Commit

Permalink
Restructure code
Browse files Browse the repository at this point in the history
  • Loading branch information
citizen428 committed Apr 30, 2022
1 parent aa3e822 commit 5e5e823
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
7 changes: 7 additions & 0 deletions cmd/repl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/citizen428/repl"

func main() {
repl.Run()
}
59 changes: 31 additions & 28 deletions repl.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package repl

import (
"bytes"
Expand All @@ -17,42 +17,28 @@ import (
var (
cmd string
debug bool
compDir string
histDir string
compDir string = os.Getenv("HOME") + "/.repl"
histDir string = os.Getenv("HOME")
histFile string
)

func init() {
flag.BoolVar(&debug, "debug", false, "Enable debug output")
compDirDefault := os.Getenv("HOME") + "/.repl"
flag.StringVar(&compDir, "compdir", compDirDefault, "Directory for completion files")
flag.StringVar(&histDir, "histdir", os.Getenv("HOME"), "Directory for history file")
flag.Parse()

func Run() {
parseFlags()
if flag.NArg() == 0 {
prog := path.Base(os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "Usage:\n %s cmd [options]\n\nOptions:\n", prog)
flag.PrintDefaults()
os.Exit(0)
printUsage()
}

histFile = path.Join(histDir, ".repl_history")
}
readLine := liner.NewLiner()
defer readLine.Close()

func main() {
cmd = flag.Arg(0)

line := liner.NewLiner()
defer line.Close()

line.SetCtrlCAborts(true)
loadCompletions(line)
loadHistory(line)
readLine.SetCtrlCAborts(true)
loadCompletions(readLine)
loadHistory(readLine)

for {
input, err := line.Prompt(cmd + ">> ")
input, err := readLine.Prompt(cmd + ">> ")
if err == io.EOF {
saveHistory(line)
saveHistory(readLine)
os.Exit(0)
}

Expand All @@ -68,11 +54,28 @@ func main() {
args := regexp.MustCompile(`\s+`).Split(input, -1)
if cmdOut, err := exec.Command(cmd, args...).Output(); err == nil {
fmt.Println(string(cmdOut))
line.AppendHistory(input)
readLine.AppendHistory(input)
}
}
}

func parseFlags() {
flag.BoolVar(&debug, "debug", false, "Enable debug output")
flag.StringVar(&compDir, "compdir", compDir, "Directory for completion files")
flag.StringVar(&histDir, "histdir", histDir, "Directory for history file")
flag.Parse()

cmd = flag.Arg(0)
histFile = path.Join(histDir, ".repl_history")
}

func printUsage() {
prog := path.Base(cmd)
fmt.Fprintf(flag.CommandLine.Output(), "Usage:\n %s cmd [options]\n\nOptions:\n", prog)
flag.PrintDefaults()
os.Exit(0)
}

func loadCompletions(line *liner.State) {
compFile := path.Join(compDir, cmd)
if f, err := os.Open(compFile); err == nil {
Expand Down

0 comments on commit 5e5e823

Please sign in to comment.