Skip to content

Commit 9a332b8

Browse files
committed
Support --raw for raw REPL
1 parent b641dfe commit 9a332b8

File tree

1 file changed

+49
-15
lines changed

1 file changed

+49
-15
lines changed

cmd/ari/root.go

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"errors"
56
"fmt"
67
"io"
@@ -308,7 +309,12 @@ func ariMain(cmd *cobra.Command, args []string) int {
308309
}
309310

310311
// REPL
311-
readEvalPrintLoop(mainCliSystem)
312+
useRawREPL := viper.GetBool("raw")
313+
if useRawREPL {
314+
rawREPL(mainCliSystem)
315+
} else {
316+
editorREPL(mainCliSystem)
317+
}
312318
return 0
313319
}
314320

@@ -319,13 +325,17 @@ func registerCliGoalBindings(ariContext *ari.Context) {
319325
goalContext.RegisterDyad("tui.render", VFTuiRender)
320326
}
321327

322-
func readEvalPrintLoop(mainCliSystem CliSystem) {
323-
cliEditor := mainCliSystem.cliEditor
328+
func rawREPL(cliSystem CliSystem) {
329+
runStdin(cliSystem.ariContext.GoalContext, cliSystem.outputFormat)
330+
}
331+
332+
func editorREPL(cliSystem CliSystem) {
333+
cliEditor := cliSystem.cliEditor
324334
for {
325335
line, err := cliEditor.GetLine()
326336
if err != nil {
327337
if errors.Is(err, io.EOF) {
328-
mainCliSystem.shutdown()
338+
cliSystem.shutdown()
329339
break
330340
}
331341
if errors.Is(err, bubbline.ErrInterrupted) {
@@ -346,20 +356,20 @@ func readEvalPrintLoop(mainCliSystem CliSystem) {
346356

347357
// Future: Consider user commands with ]
348358
if matchesSystemCommand(line) {
349-
err = mainCliSystem.replEvalSystemCommand(line)
359+
err = cliSystem.replEvalSystemCommand(line)
350360
if err != nil {
351361
fmt.Fprintf(os.Stderr, "Failed to execute system command %q with error: %v\n", line, err)
352362
}
353363
continue
354364
}
355365

356-
switch mainCliSystem.cliMode {
366+
switch cliSystem.cliMode {
357367
case cliModeGoal:
358-
mainCliSystem.replEvalGoal(line)
368+
cliSystem.replEvalGoal(line)
359369
case cliModeSQLReadOnly:
360-
mainCliSystem.replEvalSQLReadOnly(line)
370+
cliSystem.replEvalSQLReadOnly(line)
361371
case cliModeSQLReadWrite:
362-
mainCliSystem.replEvalSQLReadWrite(line)
372+
cliSystem.replEvalSQLReadWrite(line)
363373
}
364374
}
365375
}
@@ -638,14 +648,35 @@ func runSource(cliSystem *CliSystem, source, loc string) (goal.V, error) {
638648
printProgram(goalContext, cliSystem.programName)
639649
return goal.NewGap(), nil
640650
}
641-
r, err := goalContext.Run()
651+
value, err := goalContext.Run()
642652
if err != nil {
643-
return r, formatError(cliSystem.programName, err)
653+
return value, formatError(cliSystem.programName, err)
644654
}
645-
if r.IsError() {
646-
return r, fmt.Errorf("%s", formatGoalError(goalContext, r))
655+
if value.IsError() {
656+
return value, fmt.Errorf("%s", formatGoalError(goalContext, value))
657+
}
658+
return value, nil
659+
}
660+
661+
func runStdin(ctx *goal.Context, outputFormat outputFormat) {
662+
sc := &scanner{r: bufio.NewReader(os.Stdin)}
663+
for {
664+
fmt.Fprint(os.Stdout, " ")
665+
s, err := sc.readLine()
666+
s = strings.TrimRight(s, "\n\r")
667+
if err != nil && s == "" {
668+
return
669+
}
670+
value, err := ctx.Eval(s)
671+
if err != nil {
672+
formatREPLError(err)
673+
continue
674+
}
675+
assigned := ctx.AssignedLast()
676+
if !assigned && value != (goal.V{}) {
677+
printInOutputFormat(ctx, outputFormat, value)
678+
}
647679
}
648-
return r, nil
649680
}
650681

651682
// printProgram prints debug information about the context and any compiled
@@ -776,8 +807,8 @@ working with SQL and HTTP APIs.`,
776807
err = viper.BindPFlag(flagNameDatabase, pFlags.Lookup(flagNameDatabase))
777808
cobra.CheckErr(err)
778809

779-
rootCmd.Flags().Bool("debug", false, "enable detailed debugging output on panic")
780810
rootCmd.Flags().Bool("cpu-profile", false, "write CPU profile to file")
811+
rootCmd.Flags().Bool("debug", false, "enable detailed debugging output on panic")
781812
rootCmd.Flags().StringP("execute", "e", "", "string of Goal code to execute, last result not printed automatically")
782813
rootCmd.Flags().StringArrayP("load", "l", nil, "Goal source files to load on startup")
783814
err = viper.BindPFlag("load", rootCmd.Flags().Lookup("load"))
@@ -791,6 +822,9 @@ working with SQL and HTTP APIs.`,
791822
rootCmd.Flags().BoolP("println", "p", false, "print final value of the script + newline")
792823
err = viper.BindPFlag("println", rootCmd.Flags().Lookup("println"))
793824
cobra.CheckErr(err)
825+
rootCmd.Flags().BoolP("raw", "r", false, "raw REPL w/out history or auto-complete")
826+
err = viper.BindPFlag("raw", rootCmd.Flags().Lookup("raw"))
827+
cobra.CheckErr(err)
794828
rootCmd.Flags().BoolP("version", "v", false, "print version info and exit")
795829

796830
// NB: MUST be last in this method.

0 commit comments

Comments
 (0)