-
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.
Merge pull request #5 from MTVersionManager/usecmd
Implement the use command
- Loading branch information
Showing
9 changed files
with
284 additions
and
129 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
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,56 @@ | ||
package downloadProgress | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/progress" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
type ProgressMsg float64 | ||
type DownloadedMsg bool | ||
|
||
type Model struct { | ||
Title string | ||
progress float64 | ||
progressBar progress.Model | ||
progressChannel chan float64 | ||
} | ||
|
||
func New(progressChannel chan float64) Model { | ||
progressBar := progress.New(progress.WithDefaultGradient()) | ||
return Model{ | ||
progress: 0, | ||
progressBar: progressBar, | ||
progressChannel: progressChannel, | ||
} | ||
} | ||
|
||
func WaitForProgress(progressChannel chan float64) tea.Cmd { | ||
return func() tea.Msg { | ||
downloadProgress := <-progressChannel | ||
if downloadProgress == 1 { | ||
return DownloadedMsg(true) | ||
} | ||
return ProgressMsg(downloadProgress) | ||
} | ||
} | ||
|
||
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { | ||
var cmd tea.Cmd | ||
switch msg := msg.(type) { | ||
case ProgressMsg: | ||
m.progress = float64(msg) | ||
cmd = WaitForProgress(m.progressChannel) | ||
case DownloadedMsg: | ||
m.progress = 1 | ||
} | ||
return m, cmd | ||
} | ||
|
||
func (m Model) View() string { | ||
var s string | ||
if m.Title != "" { | ||
s += m.Title + "\n" | ||
} | ||
s += m.progressBar.ViewAs(m.progress) | ||
return s | ||
} |
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,90 @@ | ||
package install | ||
|
||
import ( | ||
"github.com/MTVersionManager/mtvm/components/downloadProgress" | ||
"github.com/MTVersionManager/mtvm/shared" | ||
"github.com/MTVersionManager/mtvmplugin" | ||
"github.com/charmbracelet/bubbles/spinner" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"log" | ||
"path/filepath" | ||
) | ||
|
||
type Model struct { | ||
progressChannel chan float64 | ||
plugin mtvmplugin.Plugin | ||
installing bool | ||
version string | ||
spinner spinner.Model | ||
pluginName string | ||
downloader downloadProgress.Model | ||
} | ||
|
||
type InstalledMsg bool | ||
|
||
func New(plugin mtvmplugin.Plugin, pluginName string, version string) Model { | ||
progressChannel := make(chan float64) | ||
downloader := downloadProgress.New(progressChannel) | ||
downloader.Title = "Downloading..." | ||
spinnerModel := spinner.New() | ||
spinnerModel.Spinner = spinner.Dot | ||
return Model{ | ||
progressChannel: progressChannel, | ||
version: version, | ||
plugin: plugin, | ||
downloader: downloader, | ||
installing: true, | ||
spinner: spinnerModel, | ||
pluginName: pluginName, | ||
} | ||
} | ||
|
||
func (m Model) Init() tea.Cmd { | ||
return tea.Batch(downloadProgress.WaitForProgress(m.progressChannel), Download(m.plugin, m.version, m.progressChannel), m.spinner.Tick) | ||
} | ||
|
||
func Download(plugin mtvmplugin.Plugin, version string, progressChannel chan float64) tea.Cmd { | ||
return func() tea.Msg { | ||
err := plugin.Download(version, progressChannel) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func Install(plugin mtvmplugin.Plugin, installDir string, pluginName string, version string) tea.Cmd { | ||
return func() tea.Msg { | ||
err := plugin.Install(filepath.Join(installDir, pluginName, version)) | ||
if err != nil { | ||
return err | ||
} | ||
return InstalledMsg(true) | ||
} | ||
} | ||
|
||
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { | ||
var cmds []tea.Cmd | ||
switch msg := msg.(type) { | ||
case error: | ||
log.Fatal(msg) | ||
case downloadProgress.DownloadedMsg: | ||
m.installing = false | ||
cmds = append(cmds, Install(m.plugin, shared.Configuration.InstallDir, m.pluginName, m.version)) | ||
case InstalledMsg: | ||
return m, tea.Quit | ||
} | ||
var cmd tea.Cmd | ||
m.spinner, cmd = m.spinner.Update(msg) | ||
cmds = append(cmds, cmd) | ||
m.downloader, cmd = m.downloader.Update(msg) | ||
cmds = append(cmds, cmd) | ||
return m, tea.Batch(cmds...) | ||
} | ||
|
||
func (m Model) View() string { | ||
if m.installing { | ||
return m.downloader.View() | ||
} | ||
return m.spinner.View() + " Installing..." | ||
} |
Oops, something went wrong.