Skip to content

Commit 1297f97

Browse files
ndeloofglours
authored andcommitted
prefer aec library over raw ANSI sequences
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 55cded1 commit 1297f97

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

cmd/formatter/ansi.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,71 +20,70 @@ import (
2020
"fmt"
2121

2222
"github.com/acarl005/stripansi"
23+
"github.com/morikuni/aec"
2324
)
2425

2526
var disableAnsi bool
2627

27-
func ansi(code string) string {
28-
return fmt.Sprintf("\033%s", code)
29-
}
30-
3128
func saveCursor() {
3229
if disableAnsi {
3330
return
3431
}
35-
fmt.Print(ansi("7"))
32+
// see https://github.com/morikuni/aec/pull/5
33+
fmt.Print(aec.Save)
3634
}
3735

3836
func restoreCursor() {
3937
if disableAnsi {
4038
return
4139
}
42-
fmt.Print(ansi("8"))
40+
// see https://github.com/morikuni/aec/pull/5
41+
fmt.Print(aec.Restore)
4342
}
4443

4544
func showCursor() {
4645
if disableAnsi {
4746
return
4847
}
49-
fmt.Print(ansi("[?25h"))
48+
fmt.Print(aec.Show)
5049
}
5150

5251
func moveCursor(y, x int) {
5352
if disableAnsi {
5453
return
5554
}
56-
fmt.Print(ansi(fmt.Sprintf("[%d;%dH", y, x)))
55+
fmt.Print(aec.Position(uint(y), uint(x)))
5756
}
5857

5958
func carriageReturn() {
6059
if disableAnsi {
6160
return
6261
}
63-
fmt.Print(ansi(fmt.Sprintf("[%dG", 0)))
62+
fmt.Print(aec.Column(0))
6463
}
6564

6665
func clearLine() {
6766
if disableAnsi {
6867
return
6968
}
7069
// Does not move cursor from its current position
71-
fmt.Print(ansi("[2K"))
70+
fmt.Print(aec.EraseLine(aec.EraseModes.Tail))
7271
}
7372

7473
func moveCursorUp(lines int) {
7574
if disableAnsi {
7675
return
7776
}
7877
// Does not add new lines
79-
fmt.Print(ansi(fmt.Sprintf("[%dA", lines)))
78+
fmt.Print(aec.Up(uint(lines)))
8079
}
8180

8281
func moveCursorDown(lines int) {
8382
if disableAnsi {
8483
return
8584
}
8685
// Does not add new lines
87-
fmt.Print(ansi(fmt.Sprintf("[%dB", lines)))
86+
fmt.Print(aec.Down(uint(lines)))
8887
}
8988

9089
func newLine() {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ require (
3434
github.com/moby/patternmatcher v0.6.0
3535
github.com/moby/sys/atomicwriter v0.1.0
3636
github.com/moby/term v0.5.2
37-
github.com/morikuni/aec v1.0.0
37+
github.com/morikuni/aec v1.1.0
3838
github.com/opencontainers/go-digest v1.0.0
3939
github.com/opencontainers/image-spec v1.1.1
4040
github.com/otiai10/copy v1.14.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
283283
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
284284
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
285285
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
286-
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
287-
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
286+
github.com/morikuni/aec v1.1.0 h1:vBBl0pUnvi/Je71dsRrhMBtreIqNMYErSAbEeb8jrXQ=
287+
github.com/morikuni/aec v1.1.0/go.mod h1:xDRgiq/iw5l+zkao76YTKzKttOp2cwPEne25HDkJnBw=
288288
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
289289
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
290290
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=

0 commit comments

Comments
 (0)