Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .serverdata/mh/Game.ini
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ ServerLagReportColor2=
ChatFeedWebhookURL=
ChatFeedColor=
ServerPassword=

19 changes: 19 additions & 0 deletions internal/ansi/util_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
17 changes: 13 additions & 4 deletions internal/fullterm/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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])
}
Expand Down Expand Up @@ -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 {
Expand Down
63 changes: 63 additions & 0 deletions internal/fullterm/app_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
}