Skip to content

Commit

Permalink
move terminal logic to term package
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Nov 10, 2021
1 parent e873cfb commit 838eea7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
7 changes: 4 additions & 3 deletions cli/ui/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"

"github.com/fatih/color"
"github.com/mantil-io/mantil/cli/ui/term"
"github.com/mattn/go-colorable"
)

Expand All @@ -16,7 +17,7 @@ type Progress struct {
elements []Element
done chan struct{}
loopDone chan struct{}
writer *writer
writer *term.Writer
printFunc func(w io.Writer, format string, v ...interface{})
isTerminal bool
closer sync.Once
Expand All @@ -27,7 +28,7 @@ func New(prefix string, printFunc func(w io.Writer, format string, v ...interfac
prefix: prefix,
done: make(chan struct{}),
loopDone: make(chan struct{}),
writer: newWriter(colorable.NewColorableStdout()),
writer: term.NewWriter(colorable.NewColorableStdout()),
printFunc: printFunc,
isTerminal: isTerminal(),
}
Expand Down Expand Up @@ -91,7 +92,7 @@ func (p *Progress) print() {
out += ", done."
}
p.printFunc(p.writer, out)
p.writer.flush()
p.writer.Flush()
}

func (p *Progress) isDone() bool {
Expand Down
20 changes: 10 additions & 10 deletions cli/ui/progress/writer.go → cli/ui/term/writer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package progress
package term

import (
"bytes"
Expand All @@ -8,7 +8,7 @@ import (
"golang.org/x/term"
)

type writer struct {
type Writer struct {
out io.Writer
buffer *bytes.Buffer
prevLineCount int
Expand All @@ -17,19 +17,19 @@ type writer struct {
terminalHeight int
}

func newWriter(out io.Writer) *writer {
func NewWriter(out io.Writer) *Writer {
if out == nil {
out = os.Stdout
}
w := &writer{
w := &Writer{
out: out,
buffer: bytes.NewBuffer(nil),
}
w.initTerminal()
return w
}

func (w *writer) initTerminal() {
func (w *Writer) initTerminal() {
fd := int(os.Stdout.Fd())
w.isTerminal = term.IsTerminal(fd)
width, height, _ := term.GetSize(fd)
Expand All @@ -38,16 +38,16 @@ func (w *writer) initTerminal() {
}

// Write to internal buffer, won't be visible until flush() is called
func (w *writer) Write(p []byte) (int, error) {
func (w *Writer) Write(p []byte) (int, error) {
n, err := w.buffer.Write(p)
if err != nil {
return 0, err
}
return n, nil
}

// flush all buffered changes and reset internal buffer
func (w *writer) flush() error {
// Flush all buffered changes and reset internal buffer
func (w *Writer) Flush() error {
// add an additional newline so that we can move the cursor up
// without shifting the whole output on every flush
w.buffer.WriteByte('\n')
Expand All @@ -61,7 +61,7 @@ func (w *writer) flush() error {
return nil
}

func (w *writer) clearLines(n int) error {
func (w *Writer) clearLines(n int) error {
for i := 0; i < n; i++ {
if err := w.clearLine(); err != nil {
return err
Expand All @@ -70,7 +70,7 @@ func (w *writer) clearLines(n int) error {
return nil
}

func (w *writer) lineCount() int {
func (w *Writer) lineCount() int {
w.initTerminal()
if !w.isTerminal {
return 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//go:build !windows

package progress
package term

func (w *writer) clearLine() error {
func (w *Writer) clearLine() error {
_, err := w.out.Write([]byte("\033[1A\033[2K\r"))
return err
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build windows

package progress
package term

import (
"os"
Expand Down Expand Up @@ -39,7 +39,7 @@ type consoleScreenBufferInfo struct {
maximumWindowSize coord
}

func (wr *writer) clearLine() error {
func (wr *Writer) clearLine() error {
fd := int(os.Stdout.Fd())
h := syscall.Handle(fd)
var csbi consoleScreenBufferInfo
Expand Down

0 comments on commit 838eea7

Please sign in to comment.