Skip to content

Commit 691c08f

Browse files
committed
add spinner for long works and concurrent stdout/stderr
1 parent 57508ea commit 691c08f

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

main.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"github.com/kardianos/osext"
88
"github.com/mattn/go-shellwords"
9+
"github.com/tj/go-spin"
910
"io"
1011
"io/ioutil"
1112
"os"
@@ -46,6 +47,12 @@ func PrintlnVerbose(a ...interface{}) {
4647
}
4748
}
4849

50+
func PrintVerbose(a ...interface{}) {
51+
if *verbose {
52+
fmt.Print(a...)
53+
}
54+
}
55+
4956
func main_load() {
5057

5158
if *dfu_path == "" {
@@ -198,12 +205,8 @@ func main_load() {
198205
executablePath, _ := osext.ExecutableFolder()
199206
firmwarePath := executablePath + "/firmwares/" + *core + "/"
200207

201-
// Save verbose flag
202-
verbose_user := *verbose
203-
204208
if needUpdateBLE || *force == true {
205209

206-
*verbose = true
207210
// flash current BLE firmware to partition 8
208211
dfu_ble_flash_command := []string{dfu, dfu_flags, "-D", firmwarePath + ble_firmware, "--alt", "8"}
209212

@@ -219,7 +222,6 @@ func main_load() {
219222

220223
if needUpdateRTOS || *force == true {
221224

222-
*verbose = true
223225
// flash current RTOS firmware to partition 2
224226
dfu_rtos_flash_command := []string{dfu, dfu_flags, "-D", firmwarePath + rtos_firmware, "--alt", "2"}
225227

@@ -233,9 +235,6 @@ func main_load() {
233235
}
234236
}
235237

236-
// Restore verbose flag
237-
*verbose = verbose_user
238-
239238
// Finally flash the sketch
240239

241240
if *bin_file_name == "" {
@@ -373,24 +372,42 @@ func launchCommandAndWaitForOutput(command []string, stringToSearch string, prin
373372
tellCommandNotToSpawnShell(oscmd)
374373
stdout, _ := oscmd.StdoutPipe()
375374
stderr, _ := oscmd.StderrPipe()
376-
multi := io.MultiReader(stderr, stdout)
375+
multi := io.MultiReader(stdout, stderr)
376+
377+
s := spin.New()
378+
s.Set(spin.Spin1)
379+
showSpinner := false
380+
381+
if print_output {
382+
if *verbose {
383+
oscmd.Stdout = os.Stdout
384+
oscmd.Stderr = os.Stderr
385+
} else {
386+
showSpinner = true
387+
}
388+
}
377389
err := oscmd.Start()
378390
in := bufio.NewScanner(multi)
379-
in.Split(bufio.ScanLines)
391+
in.Split(bufio.ScanRunes)
380392
found := false
381393
out := ""
382394
for in.Scan() {
383-
if print_output {
384-
PrintlnVerbose(in.Text())
395+
396+
if showSpinner {
397+
fmt.Printf("\r %s", s.Next())
385398
}
386-
out += in.Text() + "\n"
399+
400+
out += in.Text()
387401
if stringToSearch != "" {
388-
if strings.Contains(in.Text(), stringToSearch) {
402+
if strings.Contains(out, stringToSearch) {
389403
found = true
390404
}
391405
}
392406
}
393407
err = oscmd.Wait()
408+
if showSpinner {
409+
fmt.Println("")
410+
}
394411
return err, found, out
395412
}
396413

0 commit comments

Comments
 (0)