Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spinner.go: Add support for windows #1

Merged
merged 1 commit into from
Aug 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 0 additions & 28 deletions spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"os"
"strings"
"sync"
"syscall"
"time"
"unsafe"
)

var (
Expand Down Expand Up @@ -144,32 +142,6 @@ func (spinner *Spinner) Call(methods ...func() error) error {
return nil
}

func getTerminalWidth() int {
term, err := os.Open("/dev/tty")
if err != nil {
term = os.Stdin
}

window := struct {
Rows uint16
Columns uint16
X uint16
Y uint16
}{}

result, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
term.Fd(),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(&window)),
)
if int(result) == -1 || err != nil {
return 0
}

return int(window.Columns)
}

func getSpinnerSuffix(length int) string {
suffix := getTerminalWidth() - length
if suffix > 0 {
Expand Down
33 changes: 33 additions & 0 deletions spinner_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package spinner

import (
"os"
"syscall"
"unsafe"
)

func getTerminalWidth() int {
term, err := os.Open("/dev/tty")
if err != nil {
term = os.Stdin
}

window := struct {
Rows uint16
Columns uint16
X uint16
Y uint16
}{}

result, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
term.Fd(),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(&window)),
)
if int(result) == -1 || err != nil {
return 0
}

return int(window.Columns)
}
12 changes: 12 additions & 0 deletions spinner_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package spinner

import termbox "github.com/nsf/termbox-go"

func getTerminalWidth() int {
if err := termbox.Init(); err != nil {
panic(err)
}
width, _ := termbox.Size()
termbox.Close()
return width
}