zenity/internal/zenutil/run_progress.go

51 lines
811 B
Go
Raw Normal View History

2021-04-25 19:36:15 -04:00
// +build !windows,!js
package zenutil
import (
"strconv"
)
type progressDialog struct {
err error
done chan struct{}
lines chan string
percent bool
max int
}
2021-04-29 11:05:28 -04:00
func (d *progressDialog) send(line string) error {
2021-04-25 19:36:15 -04:00
select {
2021-04-29 11:05:28 -04:00
case d.lines <- line:
2021-04-25 19:36:15 -04:00
return nil
2021-04-29 11:05:28 -04:00
case <-d.done:
return d.err
2021-04-25 19:36:15 -04:00
}
}
2021-04-29 11:05:28 -04:00
func (d *progressDialog) Close() error {
close(d.lines)
<-d.done
return d.err
2021-04-25 19:36:15 -04:00
}
2021-04-29 11:05:28 -04:00
func (d *progressDialog) Text(text string) error {
return d.send("#" + text)
2021-04-25 19:36:15 -04:00
}
2021-04-29 11:05:28 -04:00
func (d *progressDialog) Value(value int) error {
if d.percent {
return d.send(strconv.FormatFloat(100*float64(value)/float64(d.max), 'f', -1, 64))
2021-04-25 19:36:15 -04:00
} else {
2021-04-29 11:05:28 -04:00
return d.send(strconv.Itoa(value))
2021-04-25 19:36:15 -04:00
}
}
2021-04-29 11:05:28 -04:00
func (d *progressDialog) MaxValue() int {
return d.max
2021-04-25 19:36:15 -04:00
}
2021-04-29 11:05:28 -04:00
func (d *progressDialog) Done() <-chan struct{} {
return d.done
2021-04-25 19:36:15 -04:00
}