diff --git a/cmd/lgo-internal/kernel.go b/cmd/lgo-internal/kernel.go index 319faea..c3644ee 100644 --- a/cmd/lgo-internal/kernel.go +++ b/cmd/lgo-internal/kernel.go @@ -162,8 +162,17 @@ func (h *handlers) HandleExecuteRequest(ctx context.Context, r *scaffold.Execute fmt.Fprintf(os.Stderr, "panic: %v\n\n%s", p, debug.Stack()) } }() + + var f func(core.LgoContext, string) error + trimmedCode := strings.TrimSpace(r.Code) + if strings.Index(trimmedCode, "\n") == -1 && strings.HasPrefix(trimmedCode, "%"){ + f = h.runner.RunMagics + } else { + f = h.runner.Run + } + // Print the err in the notebook - if err = h.runner.Run(lgoCtx, r.Code); err != nil { + if err = f(lgoCtx, r.Code); err != nil { runner.PrintError(os.Stderr, err) } }() diff --git a/cmd/lgo-internal/magics/line_magics.go b/cmd/lgo-internal/magics/line_magics.go new file mode 100644 index 0000000..8d794f3 --- /dev/null +++ b/cmd/lgo-internal/magics/line_magics.go @@ -0,0 +1,37 @@ +package magics + +import ( + "time" + "github.com/yunabe/lgo/core" + "io/ioutil" + "fmt" + "strings" +) + +var magics = map[string]func(ctx core.LgoContext, source string, runner func(core.LgoContext, string) error) error{ + "run": run, + "time": timeMagic, +} + +func run(ctx core.LgoContext, source string, runner func(core.LgoContext, string) error) error { + srcBytes, err := ioutil.ReadFile(strings.TrimSpace(source)) + if err != nil { + core.LgoPrintln(fmt.Sprintf("Error while reading the file %s: %v", source, err)) + return err + } + + runner(ctx, string(srcBytes)) + + return nil +} + +func timeMagic(ctx core.LgoContext, source string, runner func(core.LgoContext, string) error) error { + start := time.Now() + runner(ctx, source) + core.LgoPrintln(fmt.Sprintf("time taken: %v", time.Since(start))) + return nil +} + +func GetRegisteredMagics() map[string]func(ctx core.LgoContext, source string, runner func(core.LgoContext, string) error) error { + return magics +} diff --git a/cmd/runner/runner.go b/cmd/runner/runner.go index 28e7b93..aaa4784 100644 --- a/cmd/runner/runner.go +++ b/cmd/runner/runner.go @@ -19,6 +19,7 @@ import ( "github.com/yunabe/lgo/cmd/install" "github.com/yunabe/lgo/converter" "github.com/yunabe/lgo/core" + "github.com/yunabe/lgo/cmd/lgo-internal/magics" ) /* @@ -259,3 +260,24 @@ func (rn *LgoRunner) Inspect(ctx context.Context, src string, index int) (string } return strings.Replace(buf.String(), lgoExportPrefix, "", -1), nil } + +func (rn *LgoRunner) RunMagics(ctx core.LgoContext, src string) error { + rn.execCount++ + magicFunctionWithArgs := strings.TrimSpace(src)[1:] + magic := magicFunctionWithArgs + magicFunctionIndex := strings.Index(magicFunctionWithArgs, " ") + var arguments string + if magicFunctionIndex != -1 { + magic = magicFunctionWithArgs[:magicFunctionIndex] + arguments = magicFunctionWithArgs[magicFunctionIndex:] + } + + if f, ok := magics.GetRegisteredMagics()[magic]; ok { + f(ctx, arguments, rn.Run) + } else { + core.LgoPrintln(fmt.Sprintf("UsageError: Line magic function `%%%s` not found.", magic)) + return nil + } + + return nil +}