Skip to content

Commit b37f294

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

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

cmd/ari/root.go

Lines changed: 50 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,36 @@ 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+
// Adapted from Goal's implementation.
662+
func runStdin(ctx *goal.Context, outputFormat outputFormat) {
663+
sc := &scanner{r: bufio.NewReader(os.Stdin)}
664+
for {
665+
fmt.Fprint(os.Stdout, " ")
666+
s, err := sc.readLine()
667+
s = strings.TrimRight(s, "\n\r")
668+
if err != nil && s == "" {
669+
return
670+
}
671+
value, err := ctx.Eval(s)
672+
if err != nil {
673+
formatREPLError(err)
674+
continue
675+
}
676+
assigned := ctx.AssignedLast()
677+
if !assigned && value != (goal.V{}) {
678+
printInOutputFormat(ctx, outputFormat, value)
679+
}
647680
}
648-
return r, nil
649681
}
650682

651683
// printProgram prints debug information about the context and any compiled
@@ -776,8 +808,8 @@ working with SQL and HTTP APIs.`,
776808
err = viper.BindPFlag(flagNameDatabase, pFlags.Lookup(flagNameDatabase))
777809
cobra.CheckErr(err)
778810

779-
rootCmd.Flags().Bool("debug", false, "enable detailed debugging output on panic")
780811
rootCmd.Flags().Bool("cpu-profile", false, "write CPU profile to file")
812+
rootCmd.Flags().Bool("debug", false, "enable detailed debugging output on panic")
781813
rootCmd.Flags().StringP("execute", "e", "", "string of Goal code to execute, last result not printed automatically")
782814
rootCmd.Flags().StringArrayP("load", "l", nil, "Goal source files to load on startup")
783815
err = viper.BindPFlag("load", rootCmd.Flags().Lookup("load"))
@@ -791,6 +823,9 @@ working with SQL and HTTP APIs.`,
791823
rootCmd.Flags().BoolP("println", "p", false, "print final value of the script + newline")
792824
err = viper.BindPFlag("println", rootCmd.Flags().Lookup("println"))
793825
cobra.CheckErr(err)
826+
rootCmd.Flags().BoolP("raw", "r", false, "raw REPL w/out history or auto-complete")
827+
err = viper.BindPFlag("raw", rootCmd.Flags().Lookup("raw"))
828+
cobra.CheckErr(err)
794829
rootCmd.Flags().BoolP("version", "v", false, "print version info and exit")
795830

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

0 commit comments

Comments
 (0)