pax_global_header00006660000000000000000000000064146501176630014523gustar00rootroot0000000000000052 comment=68361c4d5b911e743585970ceaa733e4a1960e19 golang-github-atomicgo-cursor-0.2.0/000077500000000000000000000000001465011766300173645ustar00rootroot00000000000000golang-github-atomicgo-cursor-0.2.0/.github/000077500000000000000000000000001465011766300207245ustar00rootroot00000000000000golang-github-atomicgo-cursor-0.2.0/.github/release.yml000066400000000000000000000005371465011766300230740ustar00rootroot00000000000000changelog: exclude: labels: - ignore-for-release authors: - octocat categories: - title: Breaking Changes 🛠 labels: - breaking - title: Exciting New Features 🎉 labels: - feature - title: Fixes 🔧 labels: - fix - title: Other Changes labels: - "*" golang-github-atomicgo-cursor-0.2.0/.github/settings.yml000066400000000000000000000016271465011766300233150ustar00rootroot00000000000000_extends: .github repository: # See https://developer.github.com/v3/repos/#edit for all available settings. # A short description of the repository that will show up on GitHub description: 🖱️ Go module to move the terminal cursor in any direction on every operating system. # A comma-separated list of topics to set on the repository topics: atomicgo, go, golang, golang-library, terminal, cursor, tui # Either `true` to make the repository private, or `false` to make it public. private: false # Either `true` to enable issues for this repository, `false` to disable them. has_issues: true # Either `true` to enable projects for this repository, or `false` to disable them. # If projects are disabled for the organization, passing `true` will cause an API error. has_projects: false # Either `true` to enable the wiki for this repository, `false` to disable it. has_wiki: false golang-github-atomicgo-cursor-0.2.0/.github/workflows/000077500000000000000000000000001465011766300227615ustar00rootroot00000000000000golang-github-atomicgo-cursor-0.2.0/.github/workflows/atomicgo.yml000066400000000000000000000005051465011766300253060ustar00rootroot00000000000000name: AtomicGo on: push: branches: [ main ] jobs: docs: if: "!contains(github.event.head_commit.message, 'autoupdate')" runs-on: ubuntu-latest steps: - name: Update Docs uses: atomicgo/ci@main env: ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} TERM: xterm-256color golang-github-atomicgo-cursor-0.2.0/.github/workflows/go.yml000066400000000000000000000013261465011766300241130ustar00rootroot00000000000000name: Go on: push: branches: [ main ] pull_request: jobs: build: name: Build runs-on: ${{ matrix.os }} strategy: matrix: os: [ ubuntu-latest, windows-latest, macos-latest ] steps: - name: Set up Go 1.x uses: actions/setup-go@v2 with: go-version: ^1 id: go - name: Check out code into the Go module directory uses: actions/checkout@v2 - name: Get dependencies run: go get -v -t -d ./... - name: Build run: go build -v . - name: Test run: go test -coverprofile="coverage.txt" -covermode=atomic -v -p 1 . - name: Upload coverage to Codecov uses: codecov/codecov-action@v1 golang-github-atomicgo-cursor-0.2.0/.github/workflows/lint.yml000066400000000000000000000006471465011766300244610ustar00rootroot00000000000000name: Code Analysis on: [ push, pull_request ] jobs: lint: if: "!contains(github.event.head_commit.message, 'autoupdate')" runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Go uses: actions/setup-go@v4 with: go-version: 'stable' - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: version: latest golang-github-atomicgo-cursor-0.2.0/.github/workflows/tweet-release.yml000066400000000000000000000016161465011766300262560ustar00rootroot00000000000000name: tweet-release # Listen to the `release` event on: release: types: [published] jobs: tweet: runs-on: ubuntu-latest steps: - uses: Eomm/why-don-t-you-tweet@v1 # We don't want to tweet if the repository is not a public one if: ${{ !github.event.repository.private }} with: tweet-message: "New ${{ github.event.repository.name }} release: ${{ github.event.release.tag_name }}! 🎉 Try it out: ${{ github.event.release.html_url }} #go #golang #opensource #library #release #atomicgo" env: TWITTER_CONSUMER_API_KEY: ${{ secrets.TWITTER_CONSUMER_API_KEY }} TWITTER_CONSUMER_API_SECRET: ${{ secrets.TWITTER_CONSUMER_API_SECRET }} TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }} TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }} golang-github-atomicgo-cursor-0.2.0/.gitignore000066400000000000000000000006311465011766300213540ustar00rootroot00000000000000### Go template # Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) vendor/ ### IntelliJ .idea *.iml out gen ### VisualStudioCode .vscode *.code-workspace ### macOS # General .DS_Store experimenting golang-github-atomicgo-cursor-0.2.0/.golangci.yml000066400000000000000000000032341465011766300217520ustar00rootroot00000000000000linters-settings: gocritic: enabled-tags: - diagnostic - experimental - opinionated - performance - style disabled-checks: - dupImport - ifElseChain - octalLiteral - whyNoLint - wrapperFunc - exitAfterDefer - hugeParam - ptrToRefParam - paramTypeCombine - unnamedResult linters: disable-all: true enable: # default linters - errcheck - gosimple - govet - ineffassign - staticcheck - typecheck - unused # additional linters - asasalint - asciicheck - bidichk - bodyclose - containedctx - contextcheck - decorder - dupl - durationcheck - errchkjson - errname - errorlint - exhaustive - exhaustruct - exportloopref - forcetypeassert - gocheckcompilerdirectives - gocritic - godot - godox - goerr113 - gofmt - goprintffuncname - gosec - gosmopolitan - importas - ireturn - nakedret - nestif - nilerr - nilnil - prealloc - predeclared - revive - rowserrcheck - tagalign - tenv - thelper - tparallel - unconvert - unparam - usestdlibvars - wastedassign - whitespace - wrapcheck - wsl - gocyclo - misspell issues: include: - EXC0012 - EXC0014 exclude-rules: - path: _test\.go linters: - gocyclo - errcheck - dupl - gosec - gocritic - linters: - gocritic text: "unnecessaryDefer:" - linters: - gocritic text: "preferDecodeRune:" service: golangci-lint-version: 1.53.x golang-github-atomicgo-cursor-0.2.0/CHANGELOG.md000066400000000000000000000006431465011766300212000ustar00rootroot00000000000000 ## [Unreleased] ## v0.0.1 - 2021-05-09 ### Features - implement `Area` - add `ClearLinesUp` and `ClearLinesDown` - implement cross-platform support - implement cursor functions ### Bug Fixes - height can no longer be negative on non windows systems ### Code Refactoring - remove debug code [Unreleased]: https://github.com/MarvinJWendt/testza/compare/v0.0.1...HEAD golang-github-atomicgo-cursor-0.2.0/LICENSE000066400000000000000000000020741465011766300203740ustar00rootroot00000000000000MIT License Copyright (c) 2020 Marvin Wendt (MarvinJWendt) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-atomicgo-cursor-0.2.0/README.md000066400000000000000000000425371465011766300206560ustar00rootroot00000000000000

AtomicGo | cursor

Downloads Latest Release Tests Coverage Unit test count License: MIT Go report

---

Documentation | Contributing | Code of Conduct

---

AtomicGo

go get atomicgo.dev/cursor

# cursor ```go import "atomicgo.dev/cursor" ``` Package cursor contains cross\-platform methods to move the terminal cursor in different directions. This package can be used to create interactive CLI tools and games, live charts, algorithm visualizations and other updatable output of any kind. Works niceley with https://github.com/atomicgo/keyboard Special thanks to github.com/k0kubun/go\-ansi which this project is based on. ## Index - [func Bottom\(\)](<#Bottom>) - [func Clear\(\)](<#Clear>) - [func ClearLine\(\)](<#ClearLine>) - [func ClearLinesDown\(n int\)](<#ClearLinesDown>) - [func ClearLinesUp\(n int\)](<#ClearLinesUp>) - [func Down\(n int\)](<#Down>) - [func DownAndClear\(n int\)](<#DownAndClear>) - [func Hide\(\)](<#Hide>) - [func HorizontalAbsolute\(n int\)](<#HorizontalAbsolute>) - [func Left\(n int\)](<#Left>) - [func Move\(x, y int\)](<#Move>) - [func Right\(n int\)](<#Right>) - [func SetTarget\(w Writer\)](<#SetTarget>) - [func Show\(\)](<#Show>) - [func StartOfLine\(\)](<#StartOfLine>) - [func StartOfLineDown\(n int\)](<#StartOfLineDown>) - [func StartOfLineUp\(n int\)](<#StartOfLineUp>) - [func TestCustomIOWriter\(t \*testing.T\)](<#TestCustomIOWriter>) - [func Up\(n int\)](<#Up>) - [func UpAndClear\(n int\)](<#UpAndClear>) - [type Area](<#Area>) - [func NewArea\(\) Area](<#NewArea>) - [func \(area \*Area\) Bottom\(\)](<#Area.Bottom>) - [func \(area \*Area\) Clear\(\)](<#Area.Clear>) - [func \(area \*Area\) ClearLinesDown\(n int\)](<#Area.ClearLinesDown>) - [func \(area \*Area\) ClearLinesUp\(n int\)](<#Area.ClearLinesUp>) - [func \(area \*Area\) Down\(n int\)](<#Area.Down>) - [func \(area \*Area\) DownAndClear\(n int\)](<#Area.DownAndClear>) - [func \(area \*Area\) Move\(x, y int\)](<#Area.Move>) - [func \(area \*Area\) StartOfLine\(\)](<#Area.StartOfLine>) - [func \(area \*Area\) StartOfLineDown\(n int\)](<#Area.StartOfLineDown>) - [func \(area \*Area\) StartOfLineUp\(n int\)](<#Area.StartOfLineUp>) - [func \(area \*Area\) Top\(\)](<#Area.Top>) - [func \(area \*Area\) Up\(n int\)](<#Area.Up>) - [func \(area \*Area\) UpAndClear\(n int\)](<#Area.UpAndClear>) - [func \(area \*Area\) Update\(content string\)](<#Area.Update>) - [func \(area Area\) WithWriter\(writer Writer\) Area](<#Area.WithWriter>) - [type Cursor](<#Cursor>) - [func NewCursor\(\) \*Cursor](<#NewCursor>) - [func \(c \*Cursor\) Clear\(\)](<#Cursor.Clear>) - [func \(c \*Cursor\) ClearLine\(\)](<#Cursor.ClearLine>) - [func \(c \*Cursor\) Down\(n int\)](<#Cursor.Down>) - [func \(c \*Cursor\) Hide\(\)](<#Cursor.Hide>) - [func \(c \*Cursor\) HorizontalAbsolute\(n int\)](<#Cursor.HorizontalAbsolute>) - [func \(c \*Cursor\) Left\(n int\)](<#Cursor.Left>) - [func \(c \*Cursor\) Right\(n int\)](<#Cursor.Right>) - [func \(c \*Cursor\) Show\(\)](<#Cursor.Show>) - [func \(c \*Cursor\) Up\(n int\)](<#Cursor.Up>) - [func \(c \*Cursor\) WithWriter\(w Writer\) \*Cursor](<#Cursor.WithWriter>) - [type Writer](<#Writer>) ## func [Bottom]() ```go func Bottom() ``` 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 [Clear]() ```go func Clear() ``` Clear clears the current position and moves the cursor to the left. ## func [ClearLine]() ```go 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 func Down(n int) ``` Down moves the cursor n lines down relative to the current position. ## func [DownAndClear]() ```go func DownAndClear(n int) ``` DownAndClear moves the cursor down by n lines, then clears the line. ## func [Hide]() ```go func Hide() ``` 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 they reopen the terminal. ## func [HorizontalAbsolute]() ```go func HorizontalAbsolute(n int) ``` HorizontalAbsolute moves the cursor to n horizontally. The position n is absolute to the start of the line. ## func [Left]() ```go func Left(n int) ``` Left moves the cursor n characters to the left relative to the current position. ## func [Move]() ```go func Move(x, y int) ``` Move moves the cursor relative by x and y. ## func [Right]() ```go func Right(n int) ``` Right moves the cursor n characters to the right relative to the current position. ## func [SetTarget]() ```go func SetTarget(w Writer) ``` SetTarget sets to output target of the default curser to the provided cursor.Writer \(wrapping io.Writer\). ## func [Show]() ```go func Show() ``` 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 they reopen the terminal. ## func [StartOfLine]() ```go func StartOfLine() ``` StartOfLine moves the cursor to the start of the current line. ## func [StartOfLineDown]() ```go func StartOfLineDown(n int) ``` StartOfLineDown moves the cursor down by n lines, then moves to cursor to the start of the line. ## func [StartOfLineUp]() ```go func StartOfLineUp(n int) ``` StartOfLineUp moves the cursor up by n lines, then moves to cursor to the start of the line. ## func [TestCustomIOWriter]() ```go func TestCustomIOWriter(t *testing.T) ``` TestCustomIOWriter tests the cursor functions with a custom Writer. ## func [Up]() ```go func Up(n int) ``` Up moves the cursor n lines up relative to the current position. ## func [UpAndClear]() ```go func UpAndClear(n int) ``` UpAndClear moves the cursor up by n lines, then clears the line. ## type [Area]() Area displays content which can be updated on the fly. You can use this to create live output, charts, dropdowns, etc. ```go type Area struct { // contains filtered or unexported fields } ``` ### func [NewArea]() ```go func NewArea() Area ``` NewArea returns a new Area. ### func \(\*Area\) [Bottom]() ```go func (area *Area) Bottom() ``` 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 \(\*Area\) [Clear]() ```go func (area *Area) Clear() ``` Clear clears the content of the Area. ### func \(\*Area\) [ClearLinesDown]() ```go func (area *Area) ClearLinesDown(n int) ``` ClearLinesDown clears n lines downwards from the current position and moves the cursor. ### func \(\*Area\) [ClearLinesUp]() ```go func (area *Area) ClearLinesUp(n int) ``` ClearLinesUp clears n lines upwards from the current position and moves the cursor. ### func \(\*Area\) [Down]() ```go func (area *Area) Down(n int) ``` Down moves the cursor of the area down one line. ### func \(\*Area\) [DownAndClear]() ```go func (area *Area) DownAndClear(n int) ``` DownAndClear moves the cursor down by n lines, then clears the line. ### func \(\*Area\) [Move]() ```go func (area *Area) Move(x, y int) ``` Move moves the cursor relative by x and y. ### func \(\*Area\) [StartOfLine]() ```go func (area *Area) StartOfLine() ``` StartOfLine moves the cursor to the start of the current line. ### func \(\*Area\) [StartOfLineDown]() ```go func (area *Area) StartOfLineDown(n int) ``` StartOfLineDown moves the cursor down by n lines, then moves to cursor to the start of the line. ### func \(\*Area\) [StartOfLineUp]() ```go func (area *Area) StartOfLineUp(n int) ``` StartOfLineUp moves the cursor up by n lines, then moves to cursor to the start of the line. ### func \(\*Area\) [Top]() ```go func (area *Area) Top() ``` Top moves the cursor to the top of the area. This is done by calculating how many lines were moved by Up and Down. ### func \(\*Area\) [Up]() ```go func (area *Area) Up(n int) ``` Up moves the cursor of the area up one line. ### func \(\*Area\) [UpAndClear]() ```go func (area *Area) UpAndClear(n int) ``` UpAndClear moves the cursor up by n lines, then clears the line. ### func \(\*Area\) [Update]() ```go func (area *Area) Update(content string) ``` Update overwrites the content of the Area and adjusts its height based on content. ### func \(Area\) [WithWriter]() ```go func (area Area) WithWriter(writer Writer) Area ``` WithWriter sets the custom writer. ## type [Cursor]() Cursor displays content which can be updated on the fly. You can use this to create live output, charts, dropdowns, etc. ```go type Cursor struct { // contains filtered or unexported fields } ``` ### func [NewCursor]() ```go func NewCursor() *Cursor ``` NewCursor creates a new Cursor instance writing to os.Stdout. ### func \(\*Cursor\) [Clear]() ```go func (c *Cursor) Clear() ``` Clear clears the current position and moves the cursor to the left. ### func \(\*Cursor\) [ClearLine]() ```go func (c *Cursor) ClearLine() ``` ClearLine clears the current line and moves the cursor to it's start position. ### func \(\*Cursor\) [Down]() ```go func (c *Cursor) Down(n int) ``` Down moves the cursor n lines down relative to the current position. ### func \(\*Cursor\) [Hide]() ```go func (c *Cursor) Hide() ``` 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 they reopen the terminal. ### func \(\*Cursor\) [HorizontalAbsolute]() ```go func (c *Cursor) HorizontalAbsolute(n int) ``` HorizontalAbsolute moves the cursor to n horizontally. The position n is absolute to the start of the line. ### func \(\*Cursor\) [Left]() ```go func (c *Cursor) Left(n int) ``` Left moves the cursor n characters to the left relative to the current position. ### func \(\*Cursor\) [Right]() ```go func (c *Cursor) Right(n int) ``` Right moves the cursor n characters to the right relative to the current position. ### func \(\*Cursor\) [Show]() ```go func (c *Cursor) Show() ``` 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 they reopen the terminal. ### func \(\*Cursor\) [Up]() ```go func (c *Cursor) Up(n int) ``` Up moves the cursor n lines up relative to the current position. ### func \(\*Cursor\) [WithWriter]() ```go func (c *Cursor) WithWriter(w Writer) *Cursor ``` WithWriter allows for any arbitrary Writer to be used for cursor movement abstracted. ## type [Writer]() Writer is an expanded io.Writer interface with a file descriptor. ```go type Writer interface { io.Writer Fd() uintptr } ``` Generated by [gomarkdoc]() --- > [AtomicGo.dev](https://atomicgo.dev)  ·  > with ❤️ by [@MarvinJWendt](https://github.com/MarvinJWendt) | > [MarvinJWendt.com](https://marvinjwendt.com) golang-github-atomicgo-cursor-0.2.0/_examples/000077500000000000000000000000001465011766300213415ustar00rootroot00000000000000golang-github-atomicgo-cursor-0.2.0/_examples/area/000077500000000000000000000000001465011766300222515ustar00rootroot00000000000000golang-github-atomicgo-cursor-0.2.0/_examples/area/movement/000077500000000000000000000000001465011766300241035ustar00rootroot00000000000000golang-github-atomicgo-cursor-0.2.0/_examples/area/movement/main.go000066400000000000000000000026431465011766300253630ustar00rootroot00000000000000package main import ( "fmt" "time" "atomicgo.dev/cursor" ) func main() { fmt.Println("Cursor area movement demo") fmt.Println("--------------------------") area := cursor.NewArea() content := `Start content with some rows 1. Row1 2. Row2 --- ` area.Update(content) time.Sleep(1 * time.Second) area.Up(2) area.Move(3, 0) fmt.Print("Replaced row 2") time.Sleep(1 * time.Second) area.StartOfLine() area.Move(8, -1) fmt.Print("3. Appended row") time.Sleep(1 * time.Second) area.Update(content + "(restored content after move)") time.Sleep(1 * time.Second) area.Up(6) fmt.Print("<<< AFTER Up(6)") time.Sleep(1 * time.Second) area.Update(content + "(restored content after cursor up out of bounds)") time.Sleep(1 * time.Second) area.Down(6) fmt.Print("<<< AFTER Down(6)") time.Sleep(1 * time.Second) area.Update(content + "(restored content after cursor down out of bounds)") time.Sleep(1 * time.Second) area.Top() fmt.Print("<<< AFTER Top()") time.Sleep(1 * time.Second) area.Update(content + "(restored content after cursor top)") time.Sleep(1 * time.Second) area.Bottom() fmt.Print("<<< AFTER Bottom()") time.Sleep(1 * time.Second) area.Update(content + "(restored content after cursor bottom)") time.Sleep(1 * time.Second) area.Update("") time.Sleep(1 * time.Second) area.Update(content + "(restored content after empty line)") time.Sleep(1 * time.Second) fmt.Println("\n--- DONE") } golang-github-atomicgo-cursor-0.2.0/_examples/area/multiline/000077500000000000000000000000001465011766300242535ustar00rootroot00000000000000golang-github-atomicgo-cursor-0.2.0/_examples/area/multiline/main.go000066400000000000000000000031001465011766300255200ustar00rootroot00000000000000package main import ( "fmt" "math/rand" "time" "atomicgo.dev/cursor" ) func main() { fmt.Println("Multiline cursor area demo") fmt.Println("--------------------------") area := cursor.NewArea() header := "This is a multiline demo\nwith 2 lines:\n" area.Update(header) content := header for i := 1; i < 6; i++ { if i%2 == 0 { content += fmt.Sprintf(" + %d\n", i) } else { content += fmt.Sprintf(" - line: %d", i) } time.Sleep(1 * time.Second) area.Update(content) } time.Sleep(1 * time.Second) area.Update("Test varying area sizes now") time.Sleep(500 * time.Millisecond) area.Update(buildContent(1, 2)) time.Sleep(500 * time.Millisecond) area.Update(buildContent(2, 9)) time.Sleep(500 * time.Millisecond) area.Update(buildContent(3, 5)) time.Sleep(500 * time.Millisecond) area.Update(buildContent(4, 0)) time.Sleep(500 * time.Millisecond) area.Update(buildContent(5, 6)) time.Sleep(500 * time.Millisecond) area.Update(buildContent(6, 1)) time.Sleep(500 * time.Millisecond) area.Update(buildContent(7, 3)) time.Sleep(1 * time.Second) fmt.Println("\n--- DONE") } func buildContent(idx int, n int) string { content := fmt.Sprintf(">>> START OF CONTENT %d/%d <<<\n", idx, n) for i := 0; i < n; i++ { for i := 0; i < 5; i++ { content += words[rand.Intn(len(words))] + " " } content += "\n" } return content } var words = []string{ "ball", "summer", "hint", "mountain", "island", "onion", "world", "run", "hit", "fly", "swim", "crawl", "build", "dive", "jump", "crazy", "funny", "strange", "yellow", "red", "blue", "green", "white", } golang-github-atomicgo-cursor-0.2.0/_examples/area/singleline/000077500000000000000000000000001465011766300244025ustar00rootroot00000000000000golang-github-atomicgo-cursor-0.2.0/_examples/area/singleline/main.go000066400000000000000000000011521465011766300256540ustar00rootroot00000000000000package main import ( "fmt" "time" "atomicgo.dev/cursor" ) func main() { fmt.Println("Single line cursor area demo") fmt.Println("----------------------------") area := cursor.NewArea() header := "This is a singleline without newline" area.Update(header) for i := 1; i < 6; i++ { time.Sleep(1 * time.Second) area.Update(fmt.Sprintf("%s: %d", header, i)) } header = "This is a singleline with newline" area.Update(header + "\n") for i := 1; i < 6; i++ { time.Sleep(1 * time.Second) area.Update(fmt.Sprintf("%s: %d\n", header, i)) } time.Sleep(1 * time.Second) fmt.Println("\n--- DONE") } golang-github-atomicgo-cursor-0.2.0/area.go000066400000000000000000000067531465011766300206360ustar00rootroot00000000000000package cursor import ( "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 cursor *Cursor cursorPosY int } // NewArea returns a new Area. func NewArea() Area { return Area{ height: 0, writer: os.Stdout, cursor: cursor, cursorPosY: 0, } } // WithWriter sets the custom writer. func (area Area) WithWriter(writer Writer) Area { area.writer = writer area.cursor = area.cursor.WithWriter(writer) return area } // Clear clears the content of the Area. func (area *Area) Clear() { // Initialize writer if not done yet if area.writer == nil { area.writer = os.Stdout } if area.height > 0 { area.Bottom() area.ClearLinesUp(area.height) area.StartOfLine() } else { area.StartOfLine() area.cursor.ClearLine() } } // Update overwrites the content of the Area and adjusts its height based on content. func (area *Area) Update(content string) { area.Clear() area.writeArea(content) area.cursorPosY = 0 area.height = strings.Count(content, "\n") } // Up moves the cursor of the area up one line. func (area *Area) Up(n int) { if n > 0 { if area.cursorPosY+n > area.height { n = area.height - area.cursorPosY } area.cursor.Up(n) area.cursorPosY += n } } // Down moves the cursor of the area down one line. func (area *Area) Down(n int) { if n > 0 { if area.cursorPosY-n < 0 { n = area.height - area.cursorPosY } area.cursor.Down(n) area.cursorPosY -= n } } // 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 (area *Area) Bottom() { if area.cursorPosY > 0 { area.Down(area.cursorPosY) area.cursorPosY = 0 } } // Top moves the cursor to the top of the area. // This is done by calculating how many lines were moved by Up and Down. func (area *Area) Top() { if area.cursorPosY < area.height { area.Up(area.height - area.cursorPosY) area.cursorPosY = area.height } } // StartOfLine moves the cursor to the start of the current line. func (area *Area) StartOfLine() { area.cursor.HorizontalAbsolute(0) } // StartOfLineDown moves the cursor down by n lines, then moves to cursor to the start of the line. func (area *Area) StartOfLineDown(n int) { area.Down(n) area.StartOfLine() } // StartOfLineUp moves the cursor up by n lines, then moves to cursor to the start of the line. func (area *Area) StartOfLineUp(n int) { area.Up(n) area.StartOfLine() } // UpAndClear moves the cursor up by n lines, then clears the line. func (area *Area) UpAndClear(n int) { area.Up(n) area.cursor.ClearLine() } // DownAndClear moves the cursor down by n lines, then clears the line. func (area *Area) DownAndClear(n int) { area.Down(n) area.cursor.ClearLine() } // Move moves the cursor relative by x and y. func (area *Area) Move(x, y int) { if x > 0 { area.cursor.Right(x) } else if x < 0 { area.cursor.Left(-x) } if y > 0 { area.Up(y) } else if y < 0 { area.Down(-y) } } // ClearLinesUp clears n lines upwards from the current position and moves the cursor. func (area *Area) ClearLinesUp(n int) { area.StartOfLine() area.cursor.ClearLine() for i := 0; i < n; i++ { area.UpAndClear(1) } } // ClearLinesDown clears n lines downwards from the current position and moves the cursor. func (area *Area) ClearLinesDown(n int) { area.StartOfLine() area.cursor.ClearLine() for i := 0; i < n; i++ { area.DownAndClear(1) } } golang-github-atomicgo-cursor-0.2.0/area_other.go000066400000000000000000000003631465011766300220260ustar00rootroot00000000000000//go:build !windows // +build !windows package cursor import ( "fmt" ) // Update overwrites the content of the Area and adjusts its height based on content. func (area *Area) writeArea(content string) { fmt.Fprint(area.writer, content) } golang-github-atomicgo-cursor-0.2.0/area_windows.go000066400000000000000000000006321465011766300223760ustar00rootroot00000000000000//go:build windows // +build windows package cursor import ( "fmt" ) // writeArea is a helper for platform dependant output. // For Windows newlines '\n' in the content are replaced by '\r\n' func (area *Area) writeArea(content string) { last := ' ' for _, r := range content { if r == '\n' && last != '\r' { fmt.Fprint(area.writer, "\r\n") continue } fmt.Fprint(area.writer, string(r)) } } golang-github-atomicgo-cursor-0.2.0/codecov.yml000066400000000000000000000002021465011766300215230ustar00rootroot00000000000000coverage: status: project: default: informational: true patch: default: informational: true golang-github-atomicgo-cursor-0.2.0/cursor.go000066400000000000000000000010041465011766300212230ustar00rootroot00000000000000package cursor import ( "os" ) // Cursor displays content which can be updated on the fly. // You can use this to create live output, charts, dropdowns, etc. type Cursor struct { writer Writer } // NewCursor creates a new Cursor instance writing to os.Stdout. func NewCursor() *Cursor { return &Cursor{writer: os.Stdout} } // WithWriter allows for any arbitrary Writer to be used // for cursor movement abstracted. func (c *Cursor) WithWriter(w Writer) *Cursor { if w != nil { c.writer = w } return c } golang-github-atomicgo-cursor-0.2.0/cursor_other.go000066400000000000000000000034661465011766300224420ustar00rootroot00000000000000//go:build !windows // +build !windows package cursor import ( "fmt" ) // Up moves the cursor n lines up relative to the current position. func (c *Cursor) Up(n int) { if n > 0 { fmt.Fprintf(c.writer, "\x1b[%dA", n) } } // Down moves the cursor n lines down relative to the current position. func (c *Cursor) Down(n int) { if n > 0 { fmt.Fprintf(c.writer, "\x1b[%dB", n) } } // Right moves the cursor n characters to the right relative to the current position. func (c *Cursor) Right(n int) { if n > 0 { fmt.Fprintf(c.writer, "\x1b[%dC", n) } } // Left moves the cursor n characters to the left relative to the current position. func (c *Cursor) Left(n int) { if n > 0 { fmt.Fprintf(c.writer, "\x1b[%dD", n) } } // HorizontalAbsolute moves the cursor to n horizontally. // The position n is absolute to the start of the line. func (c *Cursor) HorizontalAbsolute(n int) { n++ // Moves the line to the character after n fmt.Fprintf(c.writer, "\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 they reopen the terminal. func (c *Cursor) Show() { fmt.Fprint(c.writer, "\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 they reopen the terminal. func (c *Cursor) Hide() { fmt.Fprintf(c.writer, "\x1b[?25l") } // ClearLine clears the current line and moves the cursor to it's start position. func (c *Cursor) ClearLine() { fmt.Fprintf(c.writer, "\x1b[2K") } // Clear clears the current position and moves the cursor to the left. func (c *Cursor) Clear() { fmt.Fprintf(c.writer, "\x1b[K") } golang-github-atomicgo-cursor-0.2.0/cursor_test.go000066400000000000000000000006761465011766300223000ustar00rootroot00000000000000package cursor import ( "fmt" "testing" ) func TestHeightChanges(t *testing.T) { for i := 0; i < 4; i++ { fmt.Println() } Up(3) if autoheight != 3 { t.Errorf("height should be 3 but is %d", autoheight) } Down(3) if autoheight != 0 { t.Errorf("height should be 0 but is %d", autoheight) } } func TestHeightCannotBeNegative(t *testing.T) { Down(10) if autoheight < 0 { t.Errorf("height is negative: %d", autoheight) } } golang-github-atomicgo-cursor-0.2.0/cursor_test_linux.go000066400000000000000000000041301465011766300235040ustar00rootroot00000000000000package cursor import ( "log" "os" "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) } 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) return "" } return string(content) } 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") return } } golang-github-atomicgo-cursor-0.2.0/cursor_windows.go000066400000000000000000000073061465011766300230100ustar00rootroot00000000000000//go:build windows // +build windows package cursor import ( "syscall" "unsafe" ) // Up moves the cursor n lines up relative to the current position. func (c *Cursor) Up(n int) { c.move(0, -n) } // Down moves the cursor n lines down relative to the current position. func (c *Cursor) Down(n int) { c.move(0, n) } // Right moves the cursor n characters to the right relative to the current position. func (c *Cursor) Right(n int) { c.move(n, 0) } // Left moves the cursor n characters to the left relative to the current position. func (c *Cursor) Left(n int) { c.move(-n, 0) } func (c *Cursor) move(x int, y int) { handle := syscall.Handle(c.writer.Fd()) var csbi consoleScreenBufferInfo _, _, _ = procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi))) var cursor coord cursor.x = csbi.cursorPosition.x + short(x) cursor.y = csbi.cursorPosition.y + short(y) _, _, _ = procSetConsoleCursorPosition.Call(uintptr(handle), uintptr(*(*int32)(unsafe.Pointer(&cursor)))) } // HorizontalAbsolute moves the cursor to n horizontally. // The position n is absolute to the start of the line. func (c *Cursor) HorizontalAbsolute(n int) { handle := syscall.Handle(c.writer.Fd()) var csbi consoleScreenBufferInfo _, _, _ = procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi))) var cursor coord cursor.x = short(n) cursor.y = csbi.cursorPosition.y if csbi.size.x < cursor.x { cursor.x = csbi.size.x } _, _, _ = procSetConsoleCursorPosition.Call(uintptr(handle), uintptr(*(*int32)(unsafe.Pointer(&cursor)))) } // 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 (c *Cursor) Show() { handle := syscall.Handle(c.writer.Fd()) var cci consoleCursorInfo _, _, _ = procGetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&cci))) cci.visible = 1 _, _, _ = procSetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&cci))) } // 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 (c *Cursor) Hide() { handle := syscall.Handle(c.writer.Fd()) var cci consoleCursorInfo _, _, _ = procGetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&cci))) cci.visible = 0 _, _, _ = procSetConsoleCursorInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&cci))) } // ClearLine clears the current line and moves the cursor to its start position. func (c *Cursor) ClearLine() { handle := syscall.Handle(c.writer.Fd()) var csbi consoleScreenBufferInfo _, _, _ = procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi))) var w uint32 var x short cursor := csbi.cursorPosition x = csbi.size.x _, _, _ = procFillConsoleOutputCharacter.Call(uintptr(handle), uintptr(' '), uintptr(x), uintptr(*(*int32)(unsafe.Pointer(&cursor))), uintptr(unsafe.Pointer(&w))) } // Clear clears the current position and moves the cursor to the left. func (c *Cursor) Clear() { handle := syscall.Handle(c.writer.Fd()) var csbi consoleScreenBufferInfo _, _, _ = procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi))) var w uint32 cursor := csbi.cursorPosition _, _, _ = procFillConsoleOutputCharacter.Call(uintptr(handle), uintptr(' '), uintptr(1), uintptr(*(*int32)(unsafe.Pointer(&cursor))), uintptr(unsafe.Pointer(&w))) if cursor.x > 0 { cursor.x-- } _, _, _ = procSetConsoleCursorPosition.Call(uintptr(handle), uintptr(*(*int32)(unsafe.Pointer(&cursor)))) } golang-github-atomicgo-cursor-0.2.0/doc.go000066400000000000000000000006221465011766300204600ustar00rootroot00000000000000/* Package cursor contains cross-platform methods to move the terminal cursor in different directions. This package can be used to create interactive CLI tools and games, live charts, algorithm visualizations and other updatable output of any kind. Works niceley with https://github.com/atomicgo/keyboard Special thanks to github.com/k0kubun/go-ansi which this project is based on. */ package cursor golang-github-atomicgo-cursor-0.2.0/go.mod000066400000000000000000000000441465011766300204700ustar00rootroot00000000000000module atomicgo.dev/cursor go 1.15 golang-github-atomicgo-cursor-0.2.0/go.sum000066400000000000000000000000001465011766300205050ustar00rootroot00000000000000golang-github-atomicgo-cursor-0.2.0/go.work000066400000000000000000000004141465011766300206740ustar00rootroot00000000000000go 1.18 use . // replace git.neotel.at/go/c5rest => /home/rl/work/c5rest // replace git.neotel.at/go/c5db => /home/rl/work/c5db // replace github.com/pterm/pterm => H:/work/github.com/pterm/pterm // replace atomicgo.dev/cursor => H:/github.com/atomicgo.dev/cursor golang-github-atomicgo-cursor-0.2.0/syscall_windows.go000066400000000000000000000016271465011766300231450ustar00rootroot00000000000000package cursor import ( "syscall" ) var ( kernel32 = syscall.NewLazyDLL("kernel32.dll") procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW") procGetConsoleCursorInfo = kernel32.NewProc("GetConsoleCursorInfo") procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo") procSetConsoleCursorInfo = kernel32.NewProc("SetConsoleCursorInfo") procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition") ) type short int16 type dword uint32 type word uint16 type coord struct { x short y short } type smallRect struct { bottom short left short right short top short } type consoleScreenBufferInfo struct { size coord cursorPosition coord attributes word window smallRect maximumWindowSize coord } type consoleCursorInfo struct { size dword visible int32 } golang-github-atomicgo-cursor-0.2.0/utils.go000066400000000000000000000063571465011766300210660ustar00rootroot00000000000000package cursor import ( "io" "os" ) // // Helpers for global cursor handling on os.Stdout // var autoheight int var cursor = &Cursor{writer: os.Stdout} // Writer is an expanded io.Writer interface with a file descriptor. type Writer interface { io.Writer Fd() uintptr } // SetTarget sets to output target of the default curser to the // provided cursor.Writer (wrapping io.Writer). func SetTarget(w Writer) { cursor = cursor.WithWriter(w) } // Up moves the cursor n lines up relative to the current position. func Up(n int) { cursor.Up(n) autoheight += n } // Down moves the cursor n lines down relative to the current position. func Down(n int) { cursor.Down(n) if autoheight > 0 { autoheight -= n } } // Right moves the cursor n characters to the right relative to the current position. func Right(n int) { cursor.Right(n) } // Left moves the cursor n characters to the left relative to the current position. func Left(n int) { cursor.Left(n) } // HorizontalAbsolute moves the cursor to n horizontally. // The position n is absolute to the start of the line. func HorizontalAbsolute(n int) { cursor.HorizontalAbsolute(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 they reopen the terminal. func Show() { cursor.Show() } // 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 they reopen the terminal. func Hide() { cursor.Hide() } // ClearLine clears the current line and moves the cursor to it's start position. func ClearLine() { cursor.ClearLine() } // Clear clears the current position and moves the cursor to the left. func Clear() { cursor.Clear() } // 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() { if autoheight > 0 { Down(autoheight) StartOfLine() autoheight = 0 } } // StartOfLine moves the cursor to the start of the current line. func StartOfLine() { HorizontalAbsolute(0) } // StartOfLineDown moves the cursor down by n lines, then moves to cursor to the start of the line. func StartOfLineDown(n int) { Down(n) StartOfLine() } // StartOfLineUp moves the cursor up by n lines, then moves to cursor to the start of the line. func StartOfLineUp(n int) { Up(n) StartOfLine() } // UpAndClear moves the cursor up by n lines, then clears the line. func UpAndClear(n int) { Up(n) ClearLine() } // DownAndClear moves the cursor down by n lines, then clears the line. func DownAndClear(n int) { Down(n) ClearLine() } // Move moves the cursor relative by x and y. func Move(x, y int) { if x > 0 { Right(x) } else if x < 0 { x *= -1 Left(x) } if y > 0 { Up(y) } else if y < 0 { y *= -1 Down(y) } } // ClearLinesUp clears n lines upwards from the current position and moves the cursor. func ClearLinesUp(n int) { for i := 0; i < n; i++ { UpAndClear(1) } } // ClearLinesDown clears n lines downwards from the current position and moves the cursor. func ClearLinesDown(n int) { for i := 0; i < n; i++ { DownAndClear(1) } }