diff --git a/.serverdata/mh/Game.ini b/.serverdata/mh/Game.ini index 14de959..2b9167a 100644 --- a/.serverdata/mh/Game.ini +++ b/.serverdata/mh/Game.ini @@ -54,3 +54,4 @@ ServerLagReportColor2= ChatFeedWebhookURL= ChatFeedColor= ServerPassword= + diff --git a/internal/ansi/util_test.go b/internal/ansi/util_test.go new file mode 100644 index 0000000..3d24c89 --- /dev/null +++ b/internal/ansi/util_test.go @@ -0,0 +1,19 @@ +package ansi + +import "testing" + +func TestFormatSingleFlag(t *testing.T) { + result := Format("hello", Blue) + expected := "\033[34mhello\033[0m" + if result != expected { + t.Errorf("expected %q, got %q", expected, result) + } +} + +func TestFormatMultipleFlags(t *testing.T) { + result := Format("hello", Blue, Bold) + expected := "\033[34;1mhello\033[0m" + if result != expected { + t.Errorf("expected %q, got %q", expected, result) + } +} diff --git a/internal/fullterm/app.go b/internal/fullterm/app.go index dbdaaa6..97b7424 100644 --- a/internal/fullterm/app.go +++ b/internal/fullterm/app.go @@ -51,6 +51,17 @@ func (src *app) Submissions() <-chan string { return src.submissionChan } +func visibleContent(content []string, height int) []string { + currentRows := len(content) + // ngl i forgot why we adding plus 1.. oh well + startRow := max(currentRows-(height+1), 0) + return content[startRow:] +} + +func formatCommandEcho(cmd string) string { + return ansi.Format("> "+cmd+"\n", ansi.Blue) +} + func (src *app) DrawContent(finalDraw bool) error { _, height, err := term.GetSize(src.fd) if err != nil { @@ -59,9 +70,7 @@ func (src *app) DrawContent(finalDraw bool) error { if !finalDraw { fmt.Print(ansi.ClearScreen + ansi.CursorHome) } - currentRows := len(src.content) - startRow := max(currentRows-(height+1), 0) - drawableRows := src.content[startRow:] + drawableRows := visibleContent(src.content, height) for i := range drawableRows { fmt.Print(drawableRows[i]) } @@ -115,7 +124,7 @@ func (src *app) Run(context context.Context) error { case newStdinInput := <-src.stdinChannel: newCmd, isSubmission := constructCmdLine(newStdinInput, src.cmdLine) if isSubmission { - src.content = append(src.content, ansi.Format("> "+string(newCmd)+"\n", ansi.Blue)) + src.content = append(src.content, formatCommandEcho(string(newCmd))) src.cmdLine = []byte{} src.submissionChan <- string(newCmd) } else { diff --git a/internal/fullterm/app_test.go b/internal/fullterm/app_test.go new file mode 100644 index 0000000..cbbc101 --- /dev/null +++ b/internal/fullterm/app_test.go @@ -0,0 +1,63 @@ +package fullterm + +import ( + "testing" +) + +func TestVisibleContentShorterThanWindow(t *testing.T) { + content := []string{"line1\n", "line2\n", "line3\n"} + result := visibleContent(content, 10) + if len(result) != len(content) { + t.Fatalf("expected %d rows, got %d", len(content), len(result)) + } + for i, row := range result { + if row != content[i] { + t.Errorf("row %d: expected %q, got %q", i, content[i], row) + } + } +} + +func TestVisibleContentExactlyFitsWindow(t *testing.T) { + content := []string{"line1\n", "line2\n", "line3\n"} + // height+1 == len(content), so startRow == 0 + result := visibleContent(content, len(content)-1) + if len(result) != len(content) { + t.Fatalf("expected %d rows, got %d", len(content), len(result)) + } +} + +func TestVisibleContentOverflowsWindow(t *testing.T) { + content := []string{"line1\n", "line2\n", "line3\n", "line4\n", "line5\n"} + height := 2 + result := visibleContent(content, height) + // startRow = max(5 - 3, 0) = 2, so rows 2,3,4 + expectedLen := height + 1 + if len(result) != expectedLen { + t.Fatalf("expected %d rows, got %d", expectedLen, len(result)) + } + if result[0] != "line3\n" { + t.Errorf("expected first visible row to be 'line3\\n', got %q", result[0]) + } + if result[len(result)-1] != "line5\n" { + t.Errorf("expected last visible row to be 'line5\\n', got %q", result[len(result)-1]) + } +} + +func TestVisibleContentEmpty(t *testing.T) { + result := visibleContent([]string{}, 10) + if len(result) != 0 { + t.Fatalf("expected empty result, got %d rows", len(result)) + } +} + +func TestVisibleContentZeroHeight(t *testing.T) { + content := []string{"line1\n", "line2\n", "line3\n"} + result := visibleContent(content, 0) + // startRow = max(3 - 1, 0) = 2, so only last row + if len(result) != 1 { + t.Fatalf("expected 1 row, got %d", len(result)) + } + if result[0] != "line3\n" { + t.Errorf("expected 'line3\\n', got %q", result[0]) + } +}