From a2e270608ba7831888eff414f3152ca1386a886d Mon Sep 17 00:00:00 2001 From: MarvinJWendt Date: Sun, 9 May 2021 03:33:21 +0200 Subject: [PATCH 1/2] feat: implement `Area` --- area.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ cursor.go | 2 +- cursor_windows.go | 2 +- go.sum | 0 utils.go | 8 +++++--- 5 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 area.go delete mode 100644 go.sum diff --git a/area.go b/area.go new file mode 100644 index 0000000..76d9916 --- /dev/null +++ b/area.go @@ -0,0 +1,45 @@ +package cursor + +import ( + "fmt" + "runtime" + "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 +} + +// NewArea returns a new Area. +func NewArea() Area { + 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) { + area.Clear() + lines := strings.Split(content, "\n") + if runtime.GOOS == "windows" { + for _, line := range lines { + fmt.Print(line) + StartOfLineDown(1) + } + } else { + for _, line := range lines { + fmt.Println(line) + } + } + height = 0 + + area.height = len(lines) +} diff --git a/cursor.go b/cursor.go index ef66ecf..152d40c 100644 --- a/cursor.go +++ b/cursor.go @@ -15,7 +15,7 @@ func Up(n int) { // Down moves the cursor n lines down relative to the current position. func Down(n int) { fmt.Printf("\x1b[%dB", n) - if height-n < 0 { + if height-n <= 0 { height = 0 } else { height -= n diff --git a/cursor_windows.go b/cursor_windows.go index 232882b..35a5f77 100644 --- a/cursor_windows.go +++ b/cursor_windows.go @@ -15,7 +15,7 @@ func Up(n int) { // Down moves the cursor n lines down relative to the current position. func Down(n int) { move(0, n) - if height-n < 0 { + if height-n <= 0 { height = 0 } else { height -= n diff --git a/go.sum b/go.sum deleted file mode 100644 index e69de29..0000000 diff --git a/utils.go b/utils.go index ee20b73..819b05f 100644 --- a/utils.go +++ b/utils.go @@ -5,9 +5,11 @@ var height int // Bottom moves the cursor to the bottom of the terminal. // This is done by calculating how many lines were moved by Up and Down. func Bottom() { - Down(height) - StartOfLine() - height = 0 + if height > 0 { + Down(height) + StartOfLine() + height = 0 + } } // StartOfLine moves the cursor to the start of the current line. From 8f55ac5b43fa07210a2a7dbe6a502c5a4a195065 Mon Sep 17 00:00:00 2001 From: MarvinJWendt Date: Sun, 9 May 2021 01:34:22 +0000 Subject: [PATCH 2/2] docs: autoupdate --- README.md | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- go.sum | 0 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 go.sum diff --git a/README.md b/README.md index ed70d2a..16f6dcb 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@

Get The Module | -Documentation +Documentation | Contributing | @@ -84,6 +84,22 @@ func ClearLine() ``` ClearLine clears the current line and moves the cursor to it's start position. +#### func ClearLinesDown + +```go +func ClearLinesDown(n int) +``` +ClearLinesDown clears n lines downwards from the current position and moves the +cursor. + +#### func ClearLinesUp + +```go +func ClearLinesUp(n int) +``` +ClearLinesUp clears n lines upwards from the current position and moves the +cursor. + #### func Down ```go @@ -183,6 +199,37 @@ func UpAndClear(n int) ``` UpAndClear moves the cursor up by n lines, then clears the line. +#### type Area + +```go +type Area struct { +} +``` + +Area displays content which can be updated on the fly. You can use this to +create live output, charts, dropdowns, etc. + +#### func NewArea + +```go +func NewArea() Area +``` +NewArea returns a new Area. + +#### func (*Area) Clear + +```go +func (area *Area) Clear() +``` +Clear clears the content of the Area. + +#### func (*Area) Update + +```go +func (area *Area) Update(content string) +``` +Update overwrites the content of the Area. + --- > [AtomicGo.dev](https://atomicgo.dev)  ·  diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29