Skip to content

Commit

Permalink
adds the ability to set a custom writer (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
willmadison committed Oct 12, 2021
1 parent b614ba8 commit 4092d2d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
24 changes: 16 additions & 8 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ package cursor

import (
"fmt"
"io"
"os"
)

var target io.Writer = os.Stdout

func SetTarget(w io.Writer) {
target = w
}

// Up moves the cursor n lines up relative to the current position.
func Up(n int) {
fmt.Printf("\x1b[%dA", n)
fmt.Fprintf(target, "\x1b[%dA", n)
height += n
}

// Down moves the cursor n lines down relative to the current position.
func Down(n int) {
fmt.Printf("\x1b[%dB", n)
fmt.Fprintf(target, "\x1b[%dB", n)
if height-n <= 0 {
height = 0
} else {
Expand All @@ -24,36 +32,36 @@ func Down(n int) {

// Right moves the cursor n characters to the right relative to the current position.
func Right(n int) {
fmt.Printf("\x1b[%dC", n)
fmt.Fprintf(target, "\x1b[%dC", n)
}

// Left moves the cursor n characters to the left relative to the current position.
func Left(n int) {
fmt.Printf("\x1b[%dD", n)
fmt.Fprintf(target, "\x1b[%dD", n)
}

// HorizontalAbsolute moves the cursor to n horizontally.
// The position n is absolute to the start of the line.
func HorizontalAbsolute(n int) {
n += 1 // Moves the line to the character after n
fmt.Printf("\x1b[%dG", n)
fmt.Fprintf(target, "\x1b[%dG", n)
}

// Show the cursor if it was hidden previously.
// Don't forget to show the cursor at least at the end of your application.
// Otherwise the user might have a terminal with a permanently hidden cursor, until they reopen the terminal.
func Show() {
fmt.Print("\x1b[?25h")
fmt.Fprint(target, "\x1b[?25h")
}

// Hide the cursor.
// Don't forget to show the cursor at least at the end of your application with Show.
// Otherwise the user might have a terminal with a permanently hidden cursor, until they reopen the terminal.
func Hide() {
fmt.Print("\x1b[?25l")
fmt.Fprintf(target, "\x1b[?25l")
}

// ClearLine clears the current line and moves the cursor to it's start position.
func ClearLine() {
fmt.Print("\x1b[2K")
fmt.Fprintf(target, "\x1b[2K")
}
69 changes: 69 additions & 0 deletions cursor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cursor

import (
"bytes"
"fmt"
"testing"
)
Expand All @@ -25,3 +26,71 @@ func TestHeightCannotBeNegative(t *testing.T) {
t.Errorf("height is negative: %d", height)
}
}

func TestCustomIOWriter(t *testing.T) {
var w bytes.Buffer
SetTarget(&w)

Up(2)
expected := "\x1b[2A"
actual := w.String()
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

w.Reset()
Down(2)
expected = "\x1b[2B"
actual = w.String()
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

w.Reset()
Right(2)
expected = "\x1b[2C"
actual = w.String()
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

w.Reset()
Left(2)
expected = "\x1b[2D"
actual = w.String()
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

w.Reset()
Hide()
expected = "\x1b[?25l"
actual = w.String()
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

w.Reset()
Show()
expected = "\x1b[?25h"
actual = w.String()
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

w.Reset()
ClearLine()
expected = "\x1b[2K"
actual = w.String()
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

w.Reset()
HorizontalAbsolute(3)
expected = "\x1b[4G"
actual = w.String()
if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
}

0 comments on commit 4092d2d

Please sign in to comment.