package hhc_mangekyo import ( "fmt" "git.swzry.com/zry/go-hhc-cli/hhc_ast" "git.swzry.com/zry/go-hhc-cli/hhc_telws" "git.swzry.com/zry/go-hhc-cli/hhc_validators" "git.swzry.com/zry/go-hhc-cli/hhccli" "strconv" ) type MangekyoUserView struct { treeroot *hhc_ast.SyntaxDefinitionTreeRoot ctx *hhc_telws.TelwsSessionContext cvps []MangekyoCustomViewPortalCommandDefine cli *hhccli.TerminalInteractive } func NewMangeKyoUserView( ctx *hhc_telws.TelwsSessionContext, cvps []MangekyoCustomViewPortalCommandDefine, cli *hhccli.TerminalInteractive, ) *MangekyoUserView { uv := &MangekyoUserView{ ctx: ctx, cvps: cvps, cli: cli, } uv.makeTree() return uv } func (uv *MangekyoUserView) GetSDTRoot() *hhc_ast.SyntaxDefinitionTreeRoot { return uv.treeroot } func (uv *MangekyoUserView) makeTree() { uv.treeroot = &hhc_ast.SyntaxDefinitionTreeRoot{} for _, v := range uv.cvps { cmd := &hhc_ast.SDTNode_Command{ Name: v.CommandName, Description: v.Description, } end := &hhc_ast.SDTNode_End{ Exec: uv.getViewChangeExecFunc(v), } cmd.AddEnd(end) uv.treeroot.AddCommand(cmd) } uv.addCommands() } func (uv *MangekyoUserView) addCommands() { c_termwidth := &hhc_ast.SDTNode_Command{ Name: "termwidth", Description: "Set terminal width", } c_termwidth_arg1 := &hhc_ast.SDTNode_Argument{ FormatDescription: "INTEGER<10-1024>", Description: "Terminal width", Validator: hhc_validators.RangedInteger{ Min: 10, Max: 1024, }.Validate, } c_termwidth_arg1_end := &hhc_ast.SDTNode_End{ Exec: uv.execTermwidth, } c_termwidth_arg1.AddEnd(c_termwidth_arg1_end) c_termwidth.AddValArgument(c_termwidth_arg1) uv.treeroot.AddCommand(c_termwidth) c_disp := &hhc_ast.SDTNode_Command{ Name: "disp", Description: "Display information", } c_disp_termwidth := &hhc_ast.SDTNode_Command{ Name: "termwidth", Description: "Display terminal width", } c_disp_termwidth_end := &hhc_ast.SDTNode_End{ Exec: uv.execDispTermwidth, } c_disp_termwidth.AddEnd(c_disp_termwidth_end) c_disp.AddSubCommand(c_disp_termwidth) uv.treeroot.AddCommand(c_disp) c_exit := &hhc_ast.SDTNode_Command{ Name: "exit", Description: "Disconnect this session.", } c_exit_end := &hhc_ast.SDTNode_End{ Exec: uv.execExit, } c_exit.AddEnd(c_exit_end) uv.treeroot.AddCommand(c_exit) } func (uv *MangekyoUserView) execDispTermwidth(ctx *hhc_ast.ExecUserContext) { tw := uv.cli.GetTerminalWidth() ctx.ResultPrintSingleLineString(fmt.Sprintf("Terminal width: %d", tw)) } func (uv *MangekyoUserView) execTermwidth(ctx *hhc_ast.ExecUserContext) { v, ok := ctx.GetArgument(1) if !ok { ctx.ResultPrintSingleLineString(" % invalid argument") return } n, err := strconv.Atoi(v) if err != nil { ctx.ResultPrintSingleLineString(fmt.Sprintf(" %% invalid argument: %s", v)) return } if n < 10 { ctx.ResultPrintSingleLineString(" % invalid argument - too small") return } if n > 1024 { ctx.ResultPrintSingleLineString(" % invalid argument - too large") return } uv.cli.SetTerminalWidth(n) ctx.ResultPrintSingleLineString(fmt.Sprintf("Terminal width set to %d.", n)) } func (uv *MangekyoUserView) execExit(ctx *hhc_ast.ExecUserContext) { uv.ctx.GetConn().Close() } func (uv *MangekyoUserView) getViewChangeExecFunc(v MangekyoCustomViewPortalCommandDefine) func(ectx *hhc_ast.ExecUserContext) { return func(ectx *hhc_ast.ExecUserContext) { fview := v.GetViewNameFunc fprompt := v.GetPromptFunc ftitle := v.GetTitleFunc ectx.ChangeView(fview(uv.ctx)) ectx.ChangePrompt(fprompt(uv.ctx)) ectx.ChangeTitle(ftitle(uv.ctx)) } }