1
1
package main
2
2
3
3
import (
4
+ "bufio"
4
5
"errors"
5
6
"fmt"
6
7
"io"
@@ -308,7 +309,12 @@ func ariMain(cmd *cobra.Command, args []string) int {
308
309
}
309
310
310
311
// REPL
311
- readEvalPrintLoop (mainCliSystem )
312
+ useRawREPL := viper .GetBool ("raw" )
313
+ if useRawREPL {
314
+ rawREPL (mainCliSystem )
315
+ } else {
316
+ editorREPL (mainCliSystem )
317
+ }
312
318
return 0
313
319
}
314
320
@@ -319,13 +325,17 @@ func registerCliGoalBindings(ariContext *ari.Context) {
319
325
goalContext .RegisterDyad ("tui.render" , VFTuiRender )
320
326
}
321
327
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
324
334
for {
325
335
line , err := cliEditor .GetLine ()
326
336
if err != nil {
327
337
if errors .Is (err , io .EOF ) {
328
- mainCliSystem .shutdown ()
338
+ cliSystem .shutdown ()
329
339
break
330
340
}
331
341
if errors .Is (err , bubbline .ErrInterrupted ) {
@@ -346,20 +356,20 @@ func readEvalPrintLoop(mainCliSystem CliSystem) {
346
356
347
357
// Future: Consider user commands with ]
348
358
if matchesSystemCommand (line ) {
349
- err = mainCliSystem .replEvalSystemCommand (line )
359
+ err = cliSystem .replEvalSystemCommand (line )
350
360
if err != nil {
351
361
fmt .Fprintf (os .Stderr , "Failed to execute system command %q with error: %v\n " , line , err )
352
362
}
353
363
continue
354
364
}
355
365
356
- switch mainCliSystem .cliMode {
366
+ switch cliSystem .cliMode {
357
367
case cliModeGoal :
358
- mainCliSystem .replEvalGoal (line )
368
+ cliSystem .replEvalGoal (line )
359
369
case cliModeSQLReadOnly :
360
- mainCliSystem .replEvalSQLReadOnly (line )
370
+ cliSystem .replEvalSQLReadOnly (line )
361
371
case cliModeSQLReadWrite :
362
- mainCliSystem .replEvalSQLReadWrite (line )
372
+ cliSystem .replEvalSQLReadWrite (line )
363
373
}
364
374
}
365
375
}
@@ -638,14 +648,36 @@ func runSource(cliSystem *CliSystem, source, loc string) (goal.V, error) {
638
648
printProgram (goalContext , cliSystem .programName )
639
649
return goal .NewGap (), nil
640
650
}
641
- r , err := goalContext .Run ()
651
+ value , err := goalContext .Run ()
642
652
if err != nil {
643
- return r , formatError (cliSystem .programName , err )
653
+ return value , formatError (cliSystem .programName , err )
644
654
}
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
+ }
647
680
}
648
- return r , nil
649
681
}
650
682
651
683
// printProgram prints debug information about the context and any compiled
@@ -776,8 +808,8 @@ working with SQL and HTTP APIs.`,
776
808
err = viper .BindPFlag (flagNameDatabase , pFlags .Lookup (flagNameDatabase ))
777
809
cobra .CheckErr (err )
778
810
779
- rootCmd .Flags ().Bool ("debug" , false , "enable detailed debugging output on panic" )
780
811
rootCmd .Flags ().Bool ("cpu-profile" , false , "write CPU profile to file" )
812
+ rootCmd .Flags ().Bool ("debug" , false , "enable detailed debugging output on panic" )
781
813
rootCmd .Flags ().StringP ("execute" , "e" , "" , "string of Goal code to execute, last result not printed automatically" )
782
814
rootCmd .Flags ().StringArrayP ("load" , "l" , nil , "Goal source files to load on startup" )
783
815
err = viper .BindPFlag ("load" , rootCmd .Flags ().Lookup ("load" ))
@@ -791,6 +823,9 @@ working with SQL and HTTP APIs.`,
791
823
rootCmd .Flags ().BoolP ("println" , "p" , false , "print final value of the script + newline" )
792
824
err = viper .BindPFlag ("println" , rootCmd .Flags ().Lookup ("println" ))
793
825
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 )
794
829
rootCmd .Flags ().BoolP ("version" , "v" , false , "print version info and exit" )
795
830
796
831
// NB: MUST be last in this method.
0 commit comments