Skip to content

Commit

Permalink
Merge pull request #3 from atomicgo/feat/live-area
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt authored May 9, 2021
2 parents 3bebaa2 + 6452595 commit ea80532
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,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)  · 
Expand Down
45 changes: 45 additions & 0 deletions area.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cursor_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ea80532

Please sign in to comment.