Skip to content

Commit

Permalink
dotsv2: remove jitters
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjohn committed Sep 8, 2023
1 parent 0d1ee6a commit 26f5f9e
Show file tree
Hide file tree
Showing 3 changed files with 1,086 additions and 1,065 deletions.
18 changes: 14 additions & 4 deletions internal/dotwriter/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@ func (w *Writer) Flush() error {
return nil
}
defer w.hideCursor()()
w.clearLines(w.lineCount)
w.lineCount = bytes.Count(w.buf.Bytes(), []byte{'\n'})
_, err := w.out.Write(w.buf.Bytes())
w.up(w.lineCount)

lines := bytes.Split(w.buf.Bytes(), []byte{'\n'})
w.lineCount = len(lines) - 1
for i, line := range lines {
w.out.Write(line)
w.clearRest()
if i != len(lines)-1 {
w.out.Write([]byte{'\n'})
} else {

}
}
w.buf.Reset()
return err
return nil
}

// Write saves buf to a buffer
Expand Down
11 changes: 11 additions & 0 deletions internal/dotwriter/writer_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ func (w *Writer) clearLines(count int) {
_, _ = fmt.Fprint(w.out, strings.Repeat(clear, count))
}

func (w *Writer) up(count int) {
if count == 0 {
return
}
_, _ = fmt.Fprint(w.out, fmt.Sprintf("%c[%dA", ESC, count))
}

func (w *Writer) clearRest() {
_, _ = fmt.Fprint(w.out, fmt.Sprintf("%c[0K", ESC))
}

// hideCursor hides the cursor and returns a function to restore the cursor back.
func (w *Writer) hideCursor() func() {
_, _ = fmt.Fprint(w.out, hide)
Expand Down
Loading

0 comments on commit 26f5f9e

Please sign in to comment.