generated from atomicgo/template
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from atomicgo/feat/implement-ansi
- Loading branch information
Showing
12 changed files
with
428 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,4 @@ gen | |
### macOS | ||
# General | ||
.DS_Store | ||
experimenting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// +build !windows | ||
|
||
package cursor | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Up moves the cursor n lines up relative to the current position. | ||
func Up(n int) { | ||
fmt.Printf("\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) | ||
if height-n < 0 { | ||
height = 0 | ||
} else { | ||
height -= n | ||
} | ||
} | ||
|
||
// Right moves the cursor n characters to the right relative to the current position. | ||
func Right(n int) { | ||
fmt.Printf("\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) | ||
} | ||
|
||
// 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) | ||
} | ||
|
||
// 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 he reopens the terminal. | ||
func Show() { | ||
fmt.Print("\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 he reopens the terminal. | ||
func Hide() { | ||
fmt.Print("\x1b[?25l") | ||
} | ||
|
||
// ClearLine clears the current line and moves the cursor to it's start position. | ||
func ClearLine() { | ||
fmt.Print("\x1b[2K") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cursor | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
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) | ||
} | ||
} |
Oops, something went wrong.