Skip to content

Commit

Permalink
feat: progressbar component added
Browse files Browse the repository at this point in the history
  • Loading branch information
indaco committed Nov 16, 2022
1 parent f23a151 commit 095ac91
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 14 deletions.
51 changes: 37 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ It also provides ready to use validation functions for some common use cases:

### Default

[Source Code](_examples/input/default/main.go)

<img src="https://statics.sveltin.io/github/prompti/input/input-default.gif" alt="Input example">

### Default value and validation
Expand All @@ -43,61 +45,82 @@ It also provides ready to use validation functions for some common use cases:

### Custom styles

[Source Code](_examples/input/custom-styles/main.go)

<img src="https://statics.sveltin.io/github/prompti/input/input-styled.gif" alt="Input example with custom styles">

#### Examples
### Other Examples

[Default](_examples/input/default/main.go) | [Email](_examples/input/email/main.go) | [Password](_examples/input/password/main.go) | [Styled](_examples/input/custom-styles/main.go)
- [Email](_examples/input/email/main.go)
- [Password](_examples/input/password/main.go)

## Choose

`choose` is a customizable component for browsing a set of items.

### Default

[Source Code](_examples/choose/default/main.go)

<img src="https://statics.sveltin.io/github/prompti/choose/choose-default.gif" alt="Choose example">

### Custom styles

<img src="https://statics.sveltin.io/github/prompti/choose/choose-styled.gif" alt="Choose example with custom styles">

#### Examples
[Source Code](_examples/choose/custom-styles/main.go)

[Default](_examples/choose/default/main.go) | [Styled](_examples/choose/custom-styles/main.go)
<img src="https://statics.sveltin.io/github/prompti/choose/choose-styled.gif" alt="Choose example with custom styles">

## Confirm

`confirm` is a customizable component to confirm an anction.

### Default

[Source Code](_examples/confirm/default/main.go)

<img src="https://statics.sveltin.io/github/prompti/confirm/confirm-default.gif" alt="Confirm example">

### Custom styles

<img src="https://statics.sveltin.io/github/prompti/confirm/confirm-styled.gif" alt="Confirm example with custom styles">
[Source Code](_examples/confirm/custom-styles/main.go)

#### Examples

[Default](_examples/confirm/default/main.go) | [Styled](_examples/confirm/custom-styles/main.go)
<img src="https://statics.sveltin.io/github/prompti/confirm/confirm-styled.gif" alt="Confirm example with custom styles">

## Toggle

`toggle` is a customizable component to confirm an anction. It works like `confirm` but it renders the options inline and not in a box.

### Default

[Source Code](_examples/toggle/default/main.go)

<img src="https://statics.sveltin.io/github/prompti/toggle/toggle-default.gif" alt="Toggle example">

### Custom styles

[Source Code](_examples/toggle/custom-styles/main.go)

<img src="https://statics.sveltin.io/github/prompti/toggle/toggle-styled.gif" alt="Toggle example with custom styles">

#### Examples
## ProgressBar

`progressbar` is a customizable component for progress meter.

### Default

[Default](_examples/toggle/default/main.go) | [Styled](_examples/toggle/custom-styles/main.go)
[Source Code](_examples/progressbar/default/main.go)

<img src="https://statics.sveltin.io/github/prompti/progressbar/progressbar-default.gif" alt="ProgressBar example">

### Styled

[Source Code](_examples/progressbar/custom-styles/main.go)

<img src="https://statics.sveltin.io/github/prompti/progressbar/progressbar-styled.gif" alt="ProgressBar example">

## :free: License

prompti is free and open-source software licensed under the MIT License.

***
Made with [Charm](https://charm.sh).

<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge-unrounded.jpg" width="400"></a>
48 changes: 48 additions & 0 deletions _examples/progressbar/custom-styles/main.go
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)
}
}
39 changes: 39 additions & 0 deletions _examples/progressbar/default/main.go
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)
}
}
40 changes: 40 additions & 0 deletions _examples/progressbar/run-batch/main.go
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)
}
}
55 changes: 55 additions & 0 deletions progressbar/command.go
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()
}
Loading

0 comments on commit 095ac91

Please sign in to comment.