@@ -152,36 +152,44 @@ func rerun(f func() (string, error)) {
152
152
if flagWatch {
153
153
print ("\033 [H\033 [2J" ) // clear the screen
154
154
155
- prevLines := 0
156
- nextLines := 0
155
+ var prevStrSlice []string
157
156
158
157
for true {
159
- str , err := f ()
158
+ nextStr , err := f ()
160
159
if err != nil {
161
160
fmt .Println ()
162
161
errors .Exit (err )
163
162
}
164
163
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 " )
169
167
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 -- {
171
180
fmt .Printf ("\033 [%dA\033 [2K" , 1 ) // move the cursor up and clear the line
172
- prevLines --
173
181
}
174
182
175
- for i := 0 ; i < prevLines ; i ++ {
183
+ for i := 0 ; i < prevNumLines ; i ++ {
176
184
fmt .Printf ("\033 [%dA" , 1 ) // move the cursor up
177
185
}
178
186
179
- prevLines = nextLines
180
-
181
- for _ , strLine := range strSlice {
187
+ for _ , strLine := range nextStrSlice {
182
188
fmt .Printf ("\033 [2K%s\n " , strLine ) // clear the line and print the new line
183
189
}
184
190
191
+ prevStrSlice = nextStrSlice
192
+
185
193
time .Sleep (time .Second )
186
194
}
187
195
} else {
0 commit comments