From 6ef5a3f39b70e3250ef8dc54204a951a070fa30f Mon Sep 17 00:00:00 2001 From: Liu Cong Date: Thu, 19 Feb 2026 03:25:23 +0800 Subject: [PATCH] breakpoint: fix bug #189 Signed-off-by: Liu Cong --- pkg/cli/display/executor.go | 13 +++- pkg/cli/display/executor_test.go | 127 +++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 pkg/cli/display/executor_test.go diff --git a/pkg/cli/display/executor.go b/pkg/cli/display/executor.go index 2982b686..e891262a 100644 --- a/pkg/cli/display/executor.go +++ b/pkg/cli/display/executor.go @@ -413,20 +413,25 @@ func filterQuietCmds(env *model.Env, flow []model.ParsedCmd, currCmdIdx int) ([] } var newCmds []model.ParsedCmd - newIdx := currCmdIdx + newIdx := 0 for i, cmd := range flow { if cmd.IsEmpty() { continue } last := cmd.LastCmd() if last == nil || last.IsQuiet() { - if i < currCmdIdx { - newIdx -= 1 - } continue } + if i == currCmdIdx { + newIdx = len(newCmds) + } newCmds = append(newCmds, cmd) } + if len(newCmds) == 0 { + newIdx = 0 + } else if newIdx >= len(newCmds) { + newIdx = len(newCmds) - 1 + } return newCmds, newIdx } diff --git a/pkg/cli/display/executor_test.go b/pkg/cli/display/executor_test.go new file mode 100644 index 00000000..3086fcbe --- /dev/null +++ b/pkg/cli/display/executor_test.go @@ -0,0 +1,127 @@ +package display + +import ( + "testing" + + "github.com/innerr/ticat/pkg/core/model" +) + +func TestFilterQuietCmds(t *testing.T) { + tree := model.NewCmdTree(model.CmdTreeStrsForTest()) + + quietSub := tree.GetOrAddSub("quiet") + quietSub.RegEmptyCmd("quiet command").SetQuiet() + + newParsedCmd := func(name string, isQuiet bool) model.ParsedCmd { + sub := tree.GetOrAddSub(name) + if sub.Cmd() == nil { + cic := sub.RegEmptyCmd("test command") + if isQuiet { + cic.SetQuiet() + } + } + return model.ParsedCmd{ + Segments: []model.ParsedCmdSeg{ + { + Matched: model.MatchedCmd{ + Name: name, + Cmd: sub, + }, + }, + }, + } + } + + t.Run("filter_first_quiet_cmd", func(t *testing.T) { + env := model.NewEnv() + flow := []model.ParsedCmd{newParsedCmd("quiet", true), newParsedCmd("normal", false)} + newFlow, newIdx := filterQuietCmds(env, flow, 1) + + if len(newFlow) != 1 { + t.Errorf("expected 1 command, got %d", len(newFlow)) + } + if newIdx != 0 { + t.Errorf("expected newIdx 0, got %d", newIdx) + } + }) + + t.Run("filter_multiple_quiet_cmds", func(t *testing.T) { + env := model.NewEnv() + flow := []model.ParsedCmd{newParsedCmd("q1", true), newParsedCmd("q2", true), newParsedCmd("normal", false)} + newFlow, newIdx := filterQuietCmds(env, flow, 2) + + if len(newFlow) != 1 { + t.Errorf("expected 1 command, got %d", len(newFlow)) + } + if newIdx != 0 { + t.Errorf("expected newIdx 0, got %d", newIdx) + } + }) + + t.Run("no_filter_when_display_quiet_enabled", func(t *testing.T) { + env := model.NewEnv() + env.SetBool("display.mod.quiet", true) + flow := []model.ParsedCmd{newParsedCmd("quiet", true), newParsedCmd("normal", false)} + newFlow, newIdx := filterQuietCmds(env, flow, 1) + + if len(newFlow) != 2 { + t.Errorf("expected 2 commands, got %d", len(newFlow)) + } + if newIdx != 1 { + t.Errorf("expected newIdx 1, got %d", newIdx) + } + }) + + t.Run("handle_empty_cmd", func(t *testing.T) { + env := model.NewEnv() + emptyCmd := model.ParsedCmd{} + flow := []model.ParsedCmd{emptyCmd, newParsedCmd("n1", false), newParsedCmd("n2", false)} + newFlow, newIdx := filterQuietCmds(env, flow, 2) + + if len(newFlow) != 2 { + t.Errorf("expected 2 commands, got %d", len(newFlow)) + } + if newIdx != 1 { + t.Errorf("expected newIdx 1, got %d", newIdx) + } + }) + + t.Run("all_quiet_cmds", func(t *testing.T) { + env := model.NewEnv() + flow := []model.ParsedCmd{newParsedCmd("q1", true), newParsedCmd("q2", true)} + newFlow, newIdx := filterQuietCmds(env, flow, 0) + + if len(newFlow) != 0 { + t.Errorf("expected 0 commands, got %d", len(newFlow)) + } + if newIdx != 0 { + t.Errorf("expected newIdx 0, got %d", newIdx) + } + }) + + t.Run("middle_cmd_index", func(t *testing.T) { + env := model.NewEnv() + flow := []model.ParsedCmd{newParsedCmd("n1", false), newParsedCmd("n2", false), newParsedCmd("n3", false)} + newFlow, newIdx := filterQuietCmds(env, flow, 1) + + if len(newFlow) != 3 { + t.Errorf("expected 3 commands, got %d", len(newFlow)) + } + if newIdx != 1 { + t.Errorf("expected newIdx 1, got %d", newIdx) + } + }) + + t.Run("quiet_cmd_in_middle", func(t *testing.T) { + env := model.NewEnv() + flow := []model.ParsedCmd{newParsedCmd("n1", false), newParsedCmd("quiet", true), newParsedCmd("n2", false)} + newFlow, newIdx := filterQuietCmds(env, flow, 2) + + if len(newFlow) != 2 { + t.Errorf("expected 2 commands, got %d", len(newFlow)) + } + if newIdx != 1 { + t.Errorf("expected newIdx 1, got %d", newIdx) + } + }) +}