Skip to content

Commit

Permalink
cleanup Show Progress interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mostlikelee committed Nov 19, 2024
1 parent 061f3c5 commit ea2a5d0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
12 changes: 3 additions & 9 deletions orbit/pkg/dialog/dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type Dialog interface {
// ShowInfo displays a dialog that displays information. It returns an error if the dialog
// could not be displayed.
ShowInfo(ctx context.Context, opts InfoOptions) error
// Progress displays a dialog that shows progress. It returns a channel that can be used to
// end the dialog.
ShowProgress(ctx context.Context, opts ProgressOptions) chan struct{}
// Progress displays a dialog that shows progress. It waits until the
// context is cancelled.
ShowProgress(ctx context.Context, opts ProgressOptions) error
}

// EntryOptions represents options for a dialog that accepts end user input.
Expand Down Expand Up @@ -63,10 +63,4 @@ type ProgressOptions struct {

// Text sets the text of the dialog.
Text string

// Pulsate sets the progress bar to pulsate.
Pulsate bool

// NoCancel sets the dialog to grey out the cancel button.
NoCancel bool
}
23 changes: 15 additions & 8 deletions orbit/pkg/zenity/zenity.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,15 @@ func (z *Zenity) ShowInfo(ctx context.Context, opts dialog.InfoOptions) error {
return nil
}

// ShowProgress displays a progress dialog. It returns a channel that can be used to
// end the dialog.
// ShowProgress starts a Zenity progress dialog with the given options.
// This function is designed to block until the provided context is canceled.
// It is intended to be used within a separate goroutine to avoid blocking
// the main execution flow.
//
// If the context is already canceled, the function will return immediately.
//
// Use this function for cases where a progress dialog is needed to run
// alongside other operations, with explicit cancellation or termination.
func (z *Zenity) ShowProgress(ctx context.Context, opts dialog.ProgressOptions) error {
args := []string{"--progress"}
if opts.Title != "" {
Expand All @@ -94,12 +101,12 @@ func (z *Zenity) ShowProgress(ctx context.Context, opts dialog.ProgressOptions)
if opts.Text != "" {
args = append(args, fmt.Sprintf("--text=%s", opts.Text))
}
if opts.Pulsate {
args = append(args, "--pulsate")
}
if opts.NoCancel {
args = append(args, "--no-cancel")
}

// --pulsate shows a pulsating progress bar
args = append(args, "--pulsate")

// --no-cancel disables the cancel button
args = append(args, "--no-cancel")

err := z.cmdWithWait(ctx, args...)
if err != nil {
Expand Down
12 changes: 1 addition & 11 deletions orbit/pkg/zenity/zenity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,7 @@ func TestProgressArgs(t *testing.T) {
Title: "A Title",
Text: "Some text",
},
expectedArgs: []string{"--progress", "--title=A Title", "--text=Some text"},
},
{
name: "All Options",
opts: dialog.ProgressOptions{
Title: "Another Title",
Text: "Some more text",
Pulsate: true,
NoCancel: true,
},
expectedArgs: []string{"--progress", "--title=Another Title", "--text=Some more text", "--pulsate", "--no-cancel"},
expectedArgs: []string{"--progress", "--title=A Title", "--text=Some text", "--pulsate", "--no-cancel"},
},
}

Expand Down
10 changes: 4 additions & 6 deletions tools/dialog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ func main() {
panic(err)
}

ctx, cancel := context.WithCancel(context.Background())
ctx, cancelProgress := context.WithCancel(context.Background())

go func() {
err := prompt.ShowProgress(ctx, dialog.ProgressOptions{
Title: "Zenity Test Progress Title",
Text: "Zenity Test Progress Text",
Pulsate: true,
NoCancel: true,
Title: "Zenity Test Progress Title",
Text: "Zenity Test Progress Text",
})
if err != nil {
fmt.Println("Err ShowProgress")
Expand All @@ -43,7 +41,7 @@ func main() {
}()

time.Sleep(2 * time.Second)
cancel()
cancelProgress()

err = prompt.ShowInfo(ctx, dialog.InfoOptions{
Title: "Zenity Test Info Title",
Expand Down

0 comments on commit ea2a5d0

Please sign in to comment.