Skip to content

Commit

Permalink
Add custom writer for area (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Brooke Hatton <brookehatton@improbable.io>
Co-authored-by: MarvinJWendt <git@marvinjwendt.com>
  • Loading branch information
3 people authored Jun 18, 2023
1 parent da2c261 commit a812c8e
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ changelog:
- fix
- title: Other Changes
labels:
- "*"
- "*"
7 changes: 5 additions & 2 deletions .github/workflows/atomicgo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
on: push

name: AtomicGo

on:
push:
branches: [ main ]

jobs:
docs:
if: "!contains(github.event.head_commit.message, 'autoupdate')"
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:
Expand Down
16 changes: 0 additions & 16 deletions .github/workflows/golangci.yml

This file was deleted.

17 changes: 17 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Code analysis

on: [pull_request]

jobs:
golangci-lint:
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Linting with golangci-lint
uses: reviewdog/action-golangci-lint@v2
with:
github_token: ${{ secrets.ACCESS_TOKEN }}
reporter: github-pr-review
23 changes: 20 additions & 3 deletions area.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,50 @@ package cursor

import (
"fmt"
"os"
"strings"
)

// Area displays content which can be updated on the fly.
// You can use this to create live output, charts, dropdowns, etc.
type Area struct {
height int
writer Writer
}

// NewArea returns a new Area.
func NewArea() Area {
return Area{}
return Area{
writer: os.Stdout,
height: 0,
}
}

// WithWriter sets a custom writer for the Area.
func (area Area) WithWriter(writer Writer) Area {
area.writer = writer

return area
}

// Clear clears the content of the Area.
func (area *Area) Clear() {
Bottom()

if area.height > 0 {
ClearLinesUp(area.height)
}
}

// Update overwrites the content of the Area.
func (area *Area) Update(content string) {
oldWriter := target

SetTarget(area.writer) // Temporary set the target to the Area's writer so we can use the cursor functions
area.Clear()
fmt.Println(content)
height = 0
SetTarget(oldWriter) // Reset the target to the old writer
fmt.Fprintln(area.writer, content)

height = 0
area.height = len(strings.Split(content, "\n"))
}
3 changes: 2 additions & 1 deletion cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func Up(n int) {
// Down moves the cursor n lines down relative to the current position.
func Down(n int) {
fmt.Fprintf(target, "\x1b[%dB", n)

if height-n <= 0 {
height = 0
} else {
Expand All @@ -45,7 +46,7 @@ func Left(n int) {
// 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
n++ // Moves the line to the character after n
fmt.Fprintf(target, "\x1b[%dG", n)
}

Expand Down
5 changes: 5 additions & 0 deletions cursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ func TestHeightChanges(t *testing.T) {
for i := 0; i < 4; i++ {
fmt.Println()
}

Up(3)

if height != 3 {
t.Errorf("height should be 3 but is %d", height)
}

Down(3)

if height != 0 {
t.Errorf("height should be 0 but is %d", height)
}
}

func TestHeightCannotBeNegative(t *testing.T) {
Down(10)

if height < 0 {
t.Errorf("height is negative: %d", height)
}
Expand Down
23 changes: 22 additions & 1 deletion cursor_test_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,101 @@ import (
"testing"
)

// TestCustomIOWriter tests the cursor functions with a custom Writer.
func TestCustomIOWriter(t *testing.T) {
tmpFile, err := os.CreateTemp("", "testingTmpFile-")
defer os.Remove(tmpFile.Name())

if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpFile.Name())

w := tmpFile
SetTarget(w)

Up(2)

expected := "\x1b[2A"
actual := getFileContent(t, w.Name())

if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

clearFile(t, w)
Down(2)

expected = "\x1b[2B"
actual = getFileContent(t, w.Name())

if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

clearFile(t, w)
Right(2)

expected = "\x1b[2C"
actual = getFileContent(t, w.Name())

if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

clearFile(t, w)
Left(2)

expected = "\x1b[2D"
actual = getFileContent(t, w.Name())

if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

clearFile(t, w)
Hide()

expected = "\x1b[?25l"
actual = getFileContent(t, w.Name())

if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

clearFile(t, w)
Show()

expected = "\x1b[?25h"
actual = getFileContent(t, w.Name())

if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

clearFile(t, w)
ClearLine()

expected = "\x1b[2K"
actual = getFileContent(t, w.Name())

if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}

clearFile(t, w)
HorizontalAbsolute(3)

expected = "\x1b[4G"
actual = getFileContent(t, w.Name())

if expected != actual {
t.Errorf("wanted: %v, got %v", expected, actual)
}
}

func getFileContent(t *testing.T, fileName string) string {
t.Helper()

content, err := os.ReadFile(fileName)
if err != nil {
t.Errorf("failed to read file contents: %s", err)
Expand All @@ -94,12 +113,14 @@ func getFileContent(t *testing.T, fileName string) string {

func clearFile(t *testing.T, file *os.File) {
t.Helper()

err := file.Truncate(0)
if err != nil {
t.Errorf("failed to clear file")

return
}

_, err = file.Seek(0, 0)
if err != nil {
t.Errorf("failed to clear file")
Expand Down
1 change: 1 addition & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func Bottom() {
if height > 0 {
Down(height)
StartOfLine()

height = 0
}
}
Expand Down

0 comments on commit a812c8e

Please sign in to comment.