WIP: progress (macOS).

This commit is contained in:
Nuno Cruces 2021-04-25 18:34:56 +01:00
parent c49aa7990b
commit 374ba8a90a
6 changed files with 93 additions and 46 deletions

View file

@ -56,16 +56,13 @@ app.includeStandardAdditions=true
app.activate() app.activate()
ObjC.import('stdlib') ObjC.import('stdlib')
ObjC.import('readline') ObjC.import('readline')
function run(args){Progress.totalUnitCount=100 try{Progress.totalUnitCount=$.getenv('total')}catch{}
Progress.completedUnitCount=0 try{Progress.description=$.getenv('description')}catch{}
Progress.description=args[0]||"Progress"
Progress.additionalDescription=args[1]||"Running..."
while(true){var s while(true){var s
try{s=$.readline('')}catch(e){if(e.errorNumber===-128)$.exit(1) try{s=$.readline('')}catch(e){if(e.errorNumber===-128)$.exit(1)
break} break}
if(s.indexOf('#')===0){Progress.additionalDescription=s.slice(1).trim() if(s.indexOf('#')===0){Progress.additionalDescription=s.slice(1)
continue} continue}
var i=parseInt(s) var i=parseInt(s)
if(Number.isSafeInteger(i)){Progress.completedUnitCount=i if(i>=0&&Progress.totalUnitCount>0){Progress.completedUnitCount=i
continue}} continue}}`
Progress.completedUnitCount=100}`

View file

@ -5,13 +5,10 @@ app.activate()
ObjC.import('stdlib') ObjC.import('stdlib')
ObjC.import('readline') ObjC.import('readline')
function run(args) { try { Progress.totalUnitCount = $.getenv('total') } catch { }
Progress.totalUnitCount = 100 try { Progress.description = $.getenv('description') } catch { }
Progress.completedUnitCount = 0
Progress.description = args[0] || "Progress"
Progress.additionalDescription = args[1] || "Running..."
while (true) { while (true) {
var s var s
try { try {
s = $.readline('') s = $.readline('')
@ -21,16 +18,13 @@ function run(args) {
} }
if (s.indexOf('#') === 0) { if (s.indexOf('#') === 0) {
Progress.additionalDescription = s.slice(1).trim() Progress.additionalDescription = s.slice(1)
continue continue
} }
var i = parseInt(s) var i = parseInt(s)
if (Number.isSafeInteger(i)) { if (i >= 0 && Progress.totalUnitCount > 0) {
Progress.completedUnitCount = i Progress.completedUnitCount = i
continue continue
} }
}
Progress.completedUnitCount = 100
} }

View file

@ -51,7 +51,7 @@ func Run(ctx context.Context, script string, data interface{}) ([]byte, error) {
return cmd.Output() return cmd.Output()
} }
func RunProgress(ctx context.Context) (m *progressMonitor, err error) { func RunProgress(ctx context.Context, env []string) (m *progressDialog, err error) {
t, err := ioutil.TempDir("", "") t, err := ioutil.TempDir("", "")
if err != nil { if err != nil {
return nil, err return nil, err
@ -93,6 +93,7 @@ func RunProgress(ctx context.Context) (m *progressMonitor, err error) {
} }
cmd = exec.Command(executable) cmd = exec.Command(executable)
cmd.Env = env
pipe, err := cmd.StdinPipe() pipe, err := cmd.StdinPipe()
if err != nil { if err != nil {
return nil, err return nil, err
@ -104,7 +105,7 @@ func RunProgress(ctx context.Context) (m *progressMonitor, err error) {
ctx = context.Background() ctx = context.Background()
} }
m = &progressMonitor{ m = &progressDialog{
done: make(chan struct{}), done: make(chan struct{}),
lines: make(chan string), lines: make(chan string),
} }
@ -139,13 +140,13 @@ func RunProgress(ctx context.Context) (m *progressMonitor, err error) {
return return
} }
type progressMonitor struct { type progressDialog struct {
err error err error
done chan struct{} done chan struct{}
lines chan string lines chan string
} }
func (m *progressMonitor) send(line string) error { func (m *progressDialog) send(line string) error {
select { select {
case m.lines <- line: case m.lines <- line:
return nil return nil
@ -154,18 +155,18 @@ func (m *progressMonitor) send(line string) error {
} }
} }
func (m *progressMonitor) Close() error { func (m *progressDialog) Close() error {
close(m.lines) close(m.lines)
<-m.done <-m.done
return m.err return m.err
} }
func (m *progressMonitor) Message(msg string) error { func (m *progressDialog) Text(text string) error {
return m.send("#" + msg) return m.send("#" + text)
} }
func (m *progressMonitor) Progress(progress int) error { func (m *progressDialog) Value(value int) error {
return m.send(strconv.Itoa(progress)) return m.send(strconv.Itoa(value))
} }
// File is internal. // File is internal.

View file

@ -1,7 +1,36 @@
package zenity package zenity
type ProgressMonitor interface { // Progress displays the progress indication dialog.
Message(string) error //
Progress(int) error // Valid options: Title, Width, Height, OKLabel, CancelLabel, ExtraButton,
// Icon, MaxValue, Pulsate, NoCancel, TimeRemaining.
func Progress(options ...Option) (ProgressDialog, error) {
return progress(applyOptions(options))
}
type ProgressDialog interface {
Text(string) error
Value(int) error
Close() error Close() error
} }
// MaxValue returns an Option to set the maximum value (macOS only).
// The default value is 100.
func MaxValue(value int) Option {
return funcOption(func(o *options) { o.maxValue = value })
}
// Pulsate returns an Option to pulsate the progress bar.
func Pulsate() Option {
return funcOption(func(o *options) { o.maxValue = -1 })
}
// NoCancel returns an Option to hide the Cancel button (Unix only).
func NoCancel() Option {
return funcOption(func(o *options) { o.noCancel = true })
}
// TimeRemaining returns an Option to estimate when progress will reach 100% (Unix only).
func TimeRemaining() Option {
return funcOption(func(o *options) { o.timeRemaining = true })
}

21
progress_darwin.go Normal file
View file

@ -0,0 +1,21 @@
package zenity
import (
"strconv"
"github.com/ncruces/zenity/internal/zenutil"
)
func progress(opts options) (ProgressDialog, error) {
var env []string
if opts.title != nil {
env = append(env, "description="+*opts.title)
}
if opts.maxValue == 0 {
opts.maxValue = 100
}
if opts.maxValue >= 0 {
env = append(env, "total="+strconv.Itoa(opts.maxValue))
}
return zenutil.RunProgress(opts.ctx, env)
}

View file

@ -57,6 +57,11 @@ type options struct {
color color.Color color color.Color
showPalette bool showPalette bool
// Progress indication options
maxValue int
noCancel bool
timeRemaining bool
// Context for timeout // Context for timeout
ctx context.Context ctx context.Context
} }