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,35 @@ 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
+ 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
+ }
647
679
}
648
- return r , nil
649
680
}
650
681
651
682
// printProgram prints debug information about the context and any compiled
@@ -776,8 +807,8 @@ working with SQL and HTTP APIs.`,
776
807
err = viper .BindPFlag (flagNameDatabase , pFlags .Lookup (flagNameDatabase ))
777
808
cobra .CheckErr (err )
778
809
779
- rootCmd .Flags ().Bool ("debug" , false , "enable detailed debugging output on panic" )
780
810
rootCmd .Flags ().Bool ("cpu-profile" , false , "write CPU profile to file" )
811
+ rootCmd .Flags ().Bool ("debug" , false , "enable detailed debugging output on panic" )
781
812
rootCmd .Flags ().StringP ("execute" , "e" , "" , "string of Goal code to execute, last result not printed automatically" )
782
813
rootCmd .Flags ().StringArrayP ("load" , "l" , nil , "Goal source files to load on startup" )
783
814
err = viper .BindPFlag ("load" , rootCmd .Flags ().Lookup ("load" ))
@@ -791,6 +822,9 @@ working with SQL and HTTP APIs.`,
791
822
rootCmd .Flags ().BoolP ("println" , "p" , false , "print final value of the script + newline" )
792
823
err = viper .BindPFlag ("println" , rootCmd .Flags ().Lookup ("println" ))
793
824
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 )
794
828
rootCmd .Flags ().BoolP ("version" , "v" , false , "print version info and exit" )
795
829
796
830
// NB: MUST be last in this method.
0 commit comments