Skip to content

Commit

Permalink
feat: add spinner v4 (#96)
Browse files Browse the repository at this point in the history
* wip

* fix: added CLI exit

* fix: release the terminal at the end of the run

* fix: handle error terminal end error

* fix: only show spinner in interactive mode

* ci: remove templates after matrix ends

* fix: remove ucreate main from go routine

* fix: remove the channel

* ffix: wait for spinner to finish

* ci: removes go install from matrix job

* fix: remove duplicate error handling

---------

Co-authored-by: Mohammad_Alhallaq <mohammad.alhallaq1@gmail.com>
Co-authored-by: MelkeyDev <53410236+Melkeydev@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 25, 2023
1 parent 5062f9b commit c713f8a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 6 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/generate-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.goVersion }}
- name: build templates
run: go run main.go create -n ${{ matrix.framework }} -f ${{ matrix.framework}} -d ${{ matrix.driver }}
- name: golangci-lint
Expand All @@ -40,3 +36,5 @@ jobs:
version: v1.55.2
working-directory: ${{ matrix.framework }}
args: --timeout=5m
- name: remove templates
run: rm -rf ${{ matrix.framework }}
28 changes: 26 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"log"
"os"
"strings"
"sync"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/melkeydev/go-blueprint/cmd/flags"
"github.com/melkeydev/go-blueprint/cmd/program"
"github.com/melkeydev/go-blueprint/cmd/steps"
"github.com/melkeydev/go-blueprint/cmd/ui/multiInput"
"github.com/melkeydev/go-blueprint/cmd/ui/spinner"
"github.com/melkeydev/go-blueprint/cmd/ui/textinput"
"github.com/melkeydev/go-blueprint/cmd/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -152,9 +154,26 @@ var createCmd = &cobra.Command{
cobra.CheckErr(textinput.CreateErrorInputModel(err).Err())
}
project.AbsolutePath = currentWorkingDir

// This calls the templates
spinner := tea.NewProgram(spinner.InitialModelNew())
// add synchronization to wait for spinner to finish
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
// only run the spinner if the command is interactive
if isInteractive {
if _, err := spinner.Run(); err != nil {
cobra.CheckErr(err)
}
}
}()
err = project.CreateMainFile()
// once the create is done, stop the spinner
if isInteractive {
spinner.Quit()
}
// wait for the spinner to finish
wg.Wait()
if err != nil {
log.Printf("Problem creating files for project. %v", err)
cobra.CheckErr(textinput.CreateErrorInputModel(err).Err())
Expand All @@ -167,6 +186,11 @@ var createCmd = &cobra.Command{
nonInteractiveCommand := utils.NonInteractiveCommand(cmd.Use, cmd.Flags())
fmt.Println(tipMsgStyle.Render("Tip: Repeat the equivalent Blueprint with the following non-interactive command:"))
fmt.Println(tipMsgStyle.Italic(false).Render(fmt.Sprintf("• %s\n", nonInteractiveCommand)))
err = tprogram.ReleaseTerminal()
if err != nil {
log.Printf("Could not release terminal: %v", err)
cobra.CheckErr(err)
}
}
},
}
Expand Down
62 changes: 62 additions & 0 deletions cmd/ui/spinner/spinner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package spinner

import (
"fmt"

"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

type errMsg error

type model struct {
spinner spinner.Model
quitting bool
err error
}

func InitialModelNew() model {
s := spinner.New()
s.Spinner = spinner.Line
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
return model{spinner: s}
}

func (m model) Init() tea.Cmd {
return m.spinner.Tick
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "esc", "ctrl+c":
m.quitting = true
return m, tea.Quit
default:
return m, nil
}

case errMsg:
m.err = msg
return m, nil

default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
}

func (m model) View() string {

if m.err != nil {
return m.err.Error()
}
str := fmt.Sprintf("\n\n %s Preparing...\n\n", m.spinner.View())
if m.quitting {
return str + "\n"
}
return str
}
1 change: 1 addition & 0 deletions cmd/ui/textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
titleStyle = lipgloss.NewStyle().Background(lipgloss.Color("#01FAC6")).Foreground(lipgloss.Color("#030303")).Bold(true).Padding(0, 1, 0)
errorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF8700")).Bold(true).Padding(0, 0, 0)
)

type (
errMsg error
)
Expand Down

0 comments on commit c713f8a

Please sign in to comment.