-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
410 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/charmbracelet/lipgloss" | ||
"github.com/sveltinio/prompti/progressbar" | ||
) | ||
|
||
func main() { | ||
fruits := []string{ | ||
"apple", | ||
"banana", | ||
"orange", | ||
"grapes", | ||
"mellon", | ||
"strawberry", | ||
"mango", | ||
"lemon", | ||
"apricot", | ||
"peach", | ||
"papaya", | ||
"kiwi", | ||
"pear", | ||
"guava", | ||
"almond", | ||
"coconut", | ||
"blackberry", | ||
"cherry", | ||
"grapes", | ||
} | ||
|
||
pbConfig := &progressbar.Config{ | ||
Items: fruits, | ||
OnProgressMsg: "Eating:", | ||
Styles: progressbar.Styles{ | ||
CurrentItemStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("411")), | ||
ShowLabel: true, | ||
GradientFrom: "#FF7CCB", | ||
GradientTo: "#FDFF8C", | ||
}} | ||
|
||
if _, err := progressbar.Run(pbConfig); err != nil { | ||
fmt.Println("error running program:", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/sveltinio/prompti/progressbar" | ||
) | ||
|
||
func main() { | ||
fruits := []string{ | ||
"apple", | ||
"banana", | ||
"orange", | ||
"grapes", | ||
"mellon", | ||
"strawberry", | ||
"mango", | ||
"lemon", | ||
"apricot", | ||
"peach", | ||
"papaya", | ||
"kiwi", | ||
"pear", | ||
"guava", | ||
"almond", | ||
"coconut", | ||
"blackberry", | ||
"cherry", | ||
"grapes", | ||
} | ||
|
||
pbConfig := &progressbar.Config{Items: fruits} | ||
|
||
if _, err := progressbar.Run(pbConfig); err != nil { | ||
fmt.Println("error running program:", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/sveltinio/prompti/progressbar" | ||
) | ||
|
||
func main() { | ||
fruits := []string{ | ||
"apple", | ||
"banana", | ||
"orange", | ||
"grapes", | ||
"mellon", | ||
"strawberry", | ||
"mango", | ||
"lemon", | ||
"apricot", | ||
"peach", | ||
"papaya", | ||
"kiwi", | ||
"pear", | ||
"guava", | ||
"almond", | ||
"coconut", | ||
"blackberry", | ||
"cherry", | ||
"grapes", | ||
} | ||
|
||
pbConfig := &progressbar.Config{Items: fruits, RunConcurrently: true} | ||
|
||
fmt.Println("Run commands concurrently") | ||
if _, err := progressbar.Run(pbConfig); err != nil { | ||
fmt.Println("error running program:", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package progressbar | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
// Config represents the struct to configure the tui command. | ||
type Config struct { | ||
Items []string | ||
OnProgressCmd func(string) tea.Cmd | ||
//If true, the progressbar runs commands concurrently (tea.Batch) else in order (tea.Sequence). | ||
OnProgressMsg string | ||
OnCompletesMsg string | ||
RunConcurrently bool | ||
Styles Styles | ||
} | ||
|
||
// setDefaults sets default values for Config if not present. | ||
func (cfg *Config) setDefaults() { | ||
if cfg.OnCompletesMsg == "" { | ||
cfg.OnCompletesMsg = "Done!" | ||
} | ||
|
||
cfg.OnProgressCmd = func(item string) tea.Cmd { | ||
// This is where you'd do i/o stuff to download and install packages. In | ||
// our case we're just pausing for a moment to simulate the process. | ||
d := time.Millisecond * time.Duration(rand.Intn(500)) | ||
return tea.Tick(d, func(t time.Time) tea.Msg { | ||
return IncrementMsg(item) | ||
}) | ||
} | ||
} | ||
|
||
func (cfg *Config) initialModel() model { | ||
return model{ | ||
items: cfg.Items, | ||
onProgressCmd: cfg.OnProgressCmd, | ||
onProgressMsg: cfg.OnProgressMsg, | ||
onCompletedMsg: cfg.OnCompletesMsg, | ||
runConcurrently: cfg.RunConcurrently, | ||
showLabel: cfg.Styles.ShowLabel, | ||
currentItemNameStyle: cfg.Styles.CurrentItemStyle, | ||
progress: newProgressBarModel(cfg), | ||
} | ||
} | ||
|
||
// Run provides a shell script interface for prompting a user to confirm an | ||
// action with an affirmative or negative answer. | ||
func Run(cfg *Config) (tea.Model, error) { | ||
cfg.setDefaults() | ||
return tea.NewProgram(cfg.initialModel()).Run() | ||
} |
Oops, something went wrong.