From f401723dbe608f7b1807ad3a6b51653f2929cc14 Mon Sep 17 00:00:00 2001 From: Brooke Hatton Date: Wed, 16 Mar 2022 17:14:15 +0000 Subject: [PATCH 1/7] add custom writer for area --- area.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/area.go b/area.go index 76d9916..2b71326 100644 --- a/area.go +++ b/area.go @@ -2,6 +2,7 @@ package cursor import ( "fmt" + "io" "runtime" "strings" ) @@ -10,6 +11,7 @@ import ( // You can use this to create live output, charts, dropdowns, etc. type Area struct { height int + writer io.Writer } // NewArea returns a new Area. @@ -17,6 +19,13 @@ func NewArea() Area { return Area{} } +// WithCustomWriter sets the custom writer +func (area *Area) WithCustomWriter(writer io.Writer) *Area { + area.writer = writer + + return area +} + // Clear clears the content of the Area. func (area *Area) Clear() { Bottom() @@ -36,7 +45,7 @@ func (area *Area) Update(content string) { } } else { for _, line := range lines { - fmt.Println(line) + fmt.Fprintln(area.writer, line) } } height = 0 From 19199ee5b558be8d3b05dd82533f2e1c90a9f978 Mon Sep 17 00:00:00 2001 From: Brooke Hatton Date: Wed, 16 Mar 2022 17:17:39 +0000 Subject: [PATCH 2/7] add default writer --- area.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/area.go b/area.go index 2b71326..a252d4b 100644 --- a/area.go +++ b/area.go @@ -3,6 +3,7 @@ package cursor import ( "fmt" "io" + "os" "runtime" "strings" ) @@ -16,7 +17,9 @@ type Area struct { // NewArea returns a new Area. func NewArea() Area { - return Area{} + return Area{ + writer: os.Stdout, + } } // WithCustomWriter sets the custom writer From 051010dcb2c147c359e60c3fbe2b93b87ede3755 Mon Sep 17 00:00:00 2001 From: Brooke Hatton Date: Wed, 16 Mar 2022 17:22:47 +0000 Subject: [PATCH 3/7] add to windows --- area.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/area.go b/area.go index a252d4b..4f9e1c5 100644 --- a/area.go +++ b/area.go @@ -43,7 +43,7 @@ func (area *Area) Update(content string) { lines := strings.Split(content, "\n") if runtime.GOOS == "windows" { for _, line := range lines { - fmt.Print(line) + fmt.Fprint(area.writer, line) StartOfLineDown(1) } } else { From 99e96b0adff15b6648ae3aae5a025c4942e1b086 Mon Sep 17 00:00:00 2001 From: MarvinJWendt Date: Sun, 18 Jun 2023 18:24:28 +0200 Subject: [PATCH 4/7] refactor: change method name --- area.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/area.go b/area.go index 4f9e1c5..3b4db23 100644 --- a/area.go +++ b/area.go @@ -22,8 +22,8 @@ func NewArea() Area { } } -// WithCustomWriter sets the custom writer -func (area *Area) WithCustomWriter(writer io.Writer) *Area { +// WithWriter sets the custom writer +func (area Area) WithWriter(writer io.Writer) Area { area.writer = writer return area From 14c171652b81d5a3c4d22c84830ddef24fbf322d Mon Sep 17 00:00:00 2001 From: MarvinJWendt Date: Sun, 18 Jun 2023 18:44:14 +0200 Subject: [PATCH 5/7] refactor: change method name --- area.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/area.go b/area.go index 8dece16..35bddad 100644 --- a/area.go +++ b/area.go @@ -2,9 +2,7 @@ package cursor import ( "fmt" - "io" "os" - "runtime" "strings" ) @@ -12,7 +10,7 @@ import ( // You can use this to create live output, charts, dropdowns, etc. type Area struct { height int - writer io.Writer + writer Writer } // NewArea returns a new Area. @@ -22,8 +20,8 @@ func NewArea() Area { } } -// WithWriter sets the custom writer -func (area Area) WithWriter(writer io.Writer) Area { +// WithWriter sets a custom writer for the Area. +func (area Area) WithWriter(writer Writer) Area { area.writer = writer return area @@ -39,8 +37,11 @@ func (area *Area) Clear() { // 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) + SetTarget(oldWriter) // Reset the target to the old writer + fmt.Fprintln(area.writer, content) height = 0 area.height = len(strings.Split(content, "\n")) From 010bf2a4bb8b63d06d32797167c4d78232799f50 Mon Sep 17 00:00:00 2001 From: MarvinJWendt Date: Sun, 18 Jun 2023 18:48:17 +0200 Subject: [PATCH 6/7] ci: updated ci system --- .github/release.yml | 2 +- .github/workflows/atomicgo.yml | 7 +++++-- .github/workflows/go.yml | 1 - .github/workflows/golangci.yml | 16 ---------------- .github/workflows/lint.yml | 17 +++++++++++++++++ 5 files changed, 23 insertions(+), 20 deletions(-) delete mode 100644 .github/workflows/golangci.yml create mode 100644 .github/workflows/lint.yml diff --git a/.github/release.yml b/.github/release.yml index 219005a..4f5b8e6 100644 --- a/.github/release.yml +++ b/.github/release.yml @@ -16,4 +16,4 @@ changelog: - fix - title: Other Changes labels: - - "*" \ No newline at end of file + - "*" diff --git a/.github/workflows/atomicgo.yml b/.github/workflows/atomicgo.yml index f8d3e91..1a9de94 100644 --- a/.github/workflows/atomicgo.yml +++ b/.github/workflows/atomicgo.yml @@ -1,6 +1,9 @@ -on: push - name: AtomicGo + +on: + push: + branches: [ main ] + jobs: docs: if: "!contains(github.event.head_commit.message, 'autoupdate')" diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 597968d..d258e38 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -4,7 +4,6 @@ on: push: branches: [ main ] pull_request: - branches: [ main ] jobs: build: diff --git a/.github/workflows/golangci.yml b/.github/workflows/golangci.yml deleted file mode 100644 index 6237a22..0000000 --- a/.github/workflows/golangci.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: golangci-lint -on: [ push, pull_request ] -jobs: - golangci: - name: lint - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - - name: Set up Go - uses: actions/setup-go@v2 - - - name: golangci-lint - uses: golangci/golangci-lint-action@v3 - with: - version: latest diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..800caea --- /dev/null +++ b/.github/workflows/lint.yml @@ -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 From 4e7ffdde299a43de65366b11c05d4c8c90377e5a Mon Sep 17 00:00:00 2001 From: MarvinJWendt Date: Sun, 18 Jun 2023 19:01:35 +0200 Subject: [PATCH 7/7] refactor: fixed linting --- area.go | 5 ++++- cursor.go | 3 ++- cursor_test.go | 5 +++++ cursor_test_linux.go | 23 ++++++++++++++++++++++- utils.go | 1 + 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/area.go b/area.go index 35bddad..e467b56 100644 --- a/area.go +++ b/area.go @@ -17,6 +17,7 @@ type Area struct { func NewArea() Area { return Area{ writer: os.Stdout, + height: 0, } } @@ -30,6 +31,7 @@ func (area Area) WithWriter(writer Writer) Area { // Clear clears the content of the Area. func (area *Area) Clear() { Bottom() + if area.height > 0 { ClearLinesUp(area.height) } @@ -38,11 +40,12 @@ func (area *Area) Clear() { // 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() SetTarget(oldWriter) // Reset the target to the old writer fmt.Fprintln(area.writer, content) - height = 0 + height = 0 area.height = len(strings.Split(content, "\n")) } diff --git a/cursor.go b/cursor.go index fb4c010..e59e968 100644 --- a/cursor.go +++ b/cursor.go @@ -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 { @@ -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) } diff --git a/cursor_test.go b/cursor_test.go index b74902c..23fdeed 100644 --- a/cursor_test.go +++ b/cursor_test.go @@ -9,11 +9,15 @@ 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) } @@ -21,6 +25,7 @@ func TestHeightChanges(t *testing.T) { func TestHeightCannotBeNegative(t *testing.T) { Down(10) + if height < 0 { t.Errorf("height is negative: %d", height) } diff --git a/cursor_test_linux.go b/cursor_test_linux.go index 25179b2..6a37f40 100644 --- a/cursor_test_linux.go +++ b/cursor_test_linux.go @@ -6,75 +6,93 @@ 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) } @@ -82,6 +100,7 @@ func TestCustomIOWriter(t *testing.T) { 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) @@ -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") diff --git a/utils.go b/utils.go index cde3668..6bf619b 100644 --- a/utils.go +++ b/utils.go @@ -10,6 +10,7 @@ func Bottom() { if height > 0 { Down(height) StartOfLine() + height = 0 } }