79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/charmbracelet/x/ansi"
|
|
)
|
|
|
|
func TestTerminalCursorOutputPositionsHardwareBarAfterFrame(t *testing.T) {
|
|
file, err := os.CreateTemp(t.TempDir(), "cursor-output")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer file.Close()
|
|
|
|
output := newTerminalCursorOutput(file)
|
|
output.SetCursor(true, 7, 4)
|
|
if _, err := output.Write([]byte("frame")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
content, err := os.ReadFile(file.Name())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantSuffix := ansi.SetCursorStyle(5) + ansi.CursorPosition(7, 4) + ansi.ShowCursor
|
|
if !strings.HasSuffix(string(content), wantSuffix+ansi.ResetSynchronizedOutputMode) {
|
|
t.Fatalf("cursor output = %q, want suffix %q", content, wantSuffix)
|
|
}
|
|
}
|
|
|
|
func TestTerminalCursorOutputHidesCursorOutsideInsertMode(t *testing.T) {
|
|
file, err := os.CreateTemp(t.TempDir(), "cursor-output")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer file.Close()
|
|
|
|
output := newTerminalCursorOutput(file)
|
|
output.SetCursor(false, 0, 0)
|
|
if _, err := output.Write([]byte("frame")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
content, err := os.ReadFile(file.Name())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.HasSuffix(
|
|
string(content),
|
|
ansi.HideCursor+ansi.ResetSynchronizedOutputMode,
|
|
) {
|
|
t.Fatalf("cursor output did not hide cursor: %q", content)
|
|
}
|
|
}
|
|
|
|
func TestTerminalCursorOutputSynchronizesCompletedFrames(t *testing.T) {
|
|
file, err := os.CreateTemp(t.TempDir(), "cursor-output")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer file.Close()
|
|
|
|
output := newTerminalCursorOutput(file)
|
|
output.SetCursor(false, 0, 0)
|
|
if _, err := output.Write([]byte("animated frame")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
content, err := os.ReadFile(file.Name())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := ansi.SetSynchronizedOutputMode + "animated frame" +
|
|
ansi.HideCursor + ansi.ResetSynchronizedOutputMode
|
|
if string(content) != want {
|
|
t.Fatalf("synchronized frame output = %q, want %q", content, want)
|
|
}
|
|
}
|