123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- 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: 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))
- },
- }
- 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)
- }
- 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))
- }
|