Skip to content

Commit b4f7257

Browse files
committed
Support line wrapping with --watch CLI flag
1 parent 917d703 commit b4f7257

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

cli/cmd/root.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,36 +152,44 @@ func rerun(f func() (string, error)) {
152152
if flagWatch {
153153
print("\033[H\033[2J") // clear the screen
154154

155-
prevLines := 0
156-
nextLines := 0
155+
var prevStrSlice []string
157156

158157
for true {
159-
str, err := f()
158+
nextStr, err := f()
160159
if err != nil {
161160
fmt.Println()
162161
errors.Exit(err)
163162
}
164163

165-
str = watchHeader() + "\n" + str
166-
str = strings.TrimRight(str, "\n") + "\n" // ensure a single new line at the end
167-
strSlice := strings.Split(str, "\n")
168-
nextLines = len(strSlice)
164+
nextStr = watchHeader() + "\n" + nextStr
165+
nextStr = strings.TrimRight(nextStr, "\n") + "\n" // ensure a single new line at the end
166+
nextStrSlice := strings.Split(nextStr, "\n")
169167

170-
for prevLines > nextLines {
168+
terminalWidth := getTerminalWidth()
169+
170+
nextNumLines := 0
171+
for _, strLine := range nextStrSlice {
172+
nextNumLines += (len(strLine)-1)/terminalWidth + 1
173+
}
174+
prevNumLines := 0
175+
for _, strLine := range prevStrSlice {
176+
prevNumLines += (len(strLine)-1)/terminalWidth + 1
177+
}
178+
179+
for i := prevNumLines; i > nextNumLines; i-- {
171180
fmt.Printf("\033[%dA\033[2K", 1) // move the cursor up and clear the line
172-
prevLines--
173181
}
174182

175-
for i := 0; i < prevLines; i++ {
183+
for i := 0; i < prevNumLines; i++ {
176184
fmt.Printf("\033[%dA", 1) // move the cursor up
177185
}
178186

179-
prevLines = nextLines
180-
181-
for _, strLine := range strSlice {
187+
for _, strLine := range nextStrSlice {
182188
fmt.Printf("\033[2K%s\n", strLine) // clear the line and print the new line
183189
}
184190

191+
prevStrSlice = nextStrSlice
192+
185193
time.Sleep(time.Second)
186194
}
187195
} else {

0 commit comments

Comments
 (0)