github.com/undoio/delve@v1.9.0/pkg/terminal/starlark.go (about)

     1  package terminal
     2  
     3  import (
     4  	"github.com/undoio/delve/pkg/terminal/starbind"
     5  	"github.com/undoio/delve/service"
     6  	"github.com/undoio/delve/service/api"
     7  )
     8  
     9  type starlarkContext struct {
    10  	term *Term
    11  }
    12  
    13  var _ starbind.Context = starlarkContext{}
    14  
    15  func (ctx starlarkContext) Client() service.Client {
    16  	return ctx.term.client
    17  }
    18  
    19  func (ctx starlarkContext) RegisterCommand(name, helpMsg string, fn func(args string) error) {
    20  	cmdfn := func(t *Term, ctx callContext, args string) error {
    21  		return fn(args)
    22  	}
    23  
    24  	found := false
    25  	for i := range ctx.term.cmds.cmds {
    26  		cmd := &ctx.term.cmds.cmds[i]
    27  		for _, alias := range cmd.aliases {
    28  			if alias == name {
    29  				cmd.cmdFn = cmdfn
    30  				cmd.helpMsg = helpMsg
    31  				found = true
    32  				break
    33  			}
    34  		}
    35  		if found {
    36  			break
    37  		}
    38  	}
    39  	if !found {
    40  		newcmd := command{
    41  			aliases: []string{name},
    42  			helpMsg: helpMsg,
    43  			cmdFn:   cmdfn,
    44  		}
    45  		ctx.term.cmds.cmds = append(ctx.term.cmds.cmds, newcmd)
    46  	}
    47  }
    48  
    49  func (ctx starlarkContext) CallCommand(cmdstr string) error {
    50  	return ctx.term.cmds.Call(cmdstr, ctx.term)
    51  }
    52  
    53  func (ctx starlarkContext) Scope() api.EvalScope {
    54  	return api.EvalScope{GoroutineID: -1, Frame: ctx.term.cmds.frame}
    55  }
    56  
    57  func (ctx starlarkContext) LoadConfig() api.LoadConfig {
    58  	return ctx.term.loadConfig()
    59  }