github.com/serversong/goreporter@v0.0.0-20200325104552-3cfaf44fd178/engine/processbar/processbar.go (about) 1 // Copyright 2017 The GoReporter Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package processbar 15 16 import ( 17 "log" 18 "os" 19 "runtime" 20 "sync/atomic" 21 "time" 22 23 "github.com/reconquest/barely" 24 "github.com/reconquest/loreley" 25 ) 26 27 // LinterProcessBar provides a function to display the current progress of 28 // generating your project, you can predict the time to generate your report. 29 // And it will communicate with engine through chans(lintersProcessChans and 30 // lintersFinishedSignal). 31 func LinterProcessBar(lintersProcessChans chan int64, lintersFinishedSignal chan string) { 32 // Set process bar corlor,for a better experience. 33 format, err := loreley.CompileWithReset( 34 ` {bg 2}{fg 15}{bold} {.Mode} `+ 35 `{bg 253}{fg 0} `+ 36 `{if .Updated}{fg 70}{end}{.Done}{fg 0}/{.Total} `, 37 nil, 38 ) 39 if err != nil { 40 log.Fatal(err) 41 } 42 // Initialize some of the parameters, set some of the properties of our progress bar. 43 var ( 44 bar = barely.NewStatusBar(format.Template) 45 46 status = &struct { 47 Mode string 48 Total int 49 Done int64 50 51 Updated int64 52 }{ 53 Mode: "GOREPORTER", 54 Total: 100, 55 } 56 ) 57 58 bar.SetStatus(status) 59 processCount := int64(100) 60 61 if runtime.GOOS == "windows" { 62 PROCESSNORENDER: 63 for { 64 select { 65 case process := <-lintersProcessChans: 66 atomic.AddInt64(&status.Done, process) 67 atomic.AddInt64(&status.Updated, process) 68 processCount = processCount - process 69 if processCount <= int64(0) { 70 break PROCESSNORENDER 71 } 72 case signal := <-lintersFinishedSignal: 73 log.Println(signal) 74 } 75 } 76 } else { 77 bar.Render(os.Stderr) 78 PROCESSRENDER: 79 for { 80 select { 81 case process := <-lintersProcessChans: 82 atomic.AddInt64(&status.Done, process) 83 atomic.AddInt64(&status.Updated, process) 84 bar.Render(os.Stderr) 85 processCount = processCount - process 86 if processCount <= int64(0) { 87 break PROCESSRENDER 88 } 89 case signal := <-lintersFinishedSignal: 90 log.Println(signal) 91 bar.Render(os.Stderr) 92 case <-time.After(1 * time.Second): 93 bar.Render(os.Stderr) 94 } 95 } 96 } 97 98 bar.Clear(os.Stderr) 99 return 100 }