Creating waiting/busy indicator for executed process

前端 未结 2 948
一生所求
一生所求 2021-01-19 15:26

i\'ve program which execute child process like

cmd := exec.Command(\"npm\", \"install\")
log.Printf(\"Running command and waiting for it to finish...\")
err         


        
2条回答
  •  Happy的楠姐
    2021-01-19 15:51

    Another way is to turn icza's answer around. Since the npm command is a long-running execution, it would probably be better to use a goroutine for it instead of the ticker (or both as a goroutine), but it's a matter of preference.

    Like this:

    func npmInstall(done chan struct{}) {
        cmd := exec.Command("npm", "install")
        log.Printf("Running command and waiting for it to finish...")
    
        err := cmd.Run()
        if err != nil {
            log.Printf("\nCommand finished with error: %v", err)
        }
        close(done)
    }
    
    func main() {
        done := make(chan struct{})
        go npmInstall(done)
    
        ticker := time.NewTicker(3 * time.Second)
        defer ticker.Stop()
        for {
            select {
            case <-ticker.C:
                fmt.Print(".")
            case <-done:
                fmt.Println()
                return
            }
        }
    }
    

提交回复
热议问题