Skip to content

Commit

Permalink
show only installed sdk list for current system
Browse files Browse the repository at this point in the history
  • Loading branch information
moqsien committed Jun 4, 2024
1 parent e1d95f1 commit c81c4ce
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/installer/install/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ func GetSDKVersionDir(sdkName string) string {
return d
}

func IsSDKInstalledByVMR(sdkName string) bool {
vd := GetSDKVersionDir(sdkName)
dList, _ := os.ReadDir(vd)
count := 0
for _, d := range dList {
if d.IsDir() {
count++
}
}
if count == 0 {
os.RemoveAll(vd)
}
return count > 0
}

/*
============================
Installation configs.
Expand Down
5 changes: 5 additions & 0 deletions internal/tui/cmds/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func (v *VmrTUI) ListSDKName() {
v.SList = NewSDKSearcher()
nextEvent, sdkName := v.SList.Show()

if nextEvent == KeyEventWhatsInstalled {
// show SDKs already installed by vmr.
nextEvent, sdkName = v.SList.ShowInstalledOnly()
}

switch nextEvent {
case KeyEventSeachVersionList:
// search version list for selected sdkname.
Expand Down
44 changes: 44 additions & 0 deletions internal/tui/cmds/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
tea "github.com/charmbracelet/bubbletea"
"github.com/gvcgo/goutils/pkgs/gtea/gprint"
"github.com/gvcgo/version-manager/internal/download"
"github.com/gvcgo/version-manager/internal/installer/install"
"github.com/gvcgo/version-manager/internal/terminal"
"github.com/gvcgo/version-manager/internal/tui/table"
"github.com/gvcgo/version-manager/internal/utils"
Expand All @@ -17,6 +18,7 @@ const (
KeyEventRemoveLocalInstalled = "remove-installed-versions"
KeyEventClearLocalCached = "clear-local-cached-files"
KeyEventBacktoPreviousPage = "back-to-previous-page"
KeyEventWhatsInstalled = "show-installed-sdks"
)

/*
Expand Down Expand Up @@ -75,6 +77,41 @@ func (v *SDKSearcher) Show() (nextEvent, selectedItem string) {
return
}

func (v *SDKSearcher) ShowInstalledOnly() (nextEvent, selectedItem string) {
ll := table.NewList()
ll.SetListType(table.SDKList)
v.RegisterKeyEvents(ll)

_, w, _ := terminal.GetTerminalSize()
if w > 30 {
w -= 30
} else {
w = 120
}
ll.SetHeader([]table.Column{
{Title: "installed sdk", Width: 20},
{Title: "homepage", Width: w},
})
rows := download.GetSDKSortedRows(v.SdkList)

installedRows := []table.Row{}
for _, r := range rows {
if install.IsSDKInstalledByVMR(r[0]) {
installedRows = append(installedRows, r)
}
}
if len(installedRows) == 0 {
gprint.PrintWarning("no installed sdk found!")
return
}
ll.SetRows(installedRows)
ll.Run()

selectedItem = ll.GetSelected()
nextEvent = ll.NextEvent
return
}

func (v *SDKSearcher) RegisterKeyEvents(ll *table.List) {
// Open homepage.
ll.SetKeyEventForTable("o", table.KeyEvent{
Expand Down Expand Up @@ -121,4 +158,11 @@ func (v *SDKSearcher) RegisterKeyEvents(ll *table.List) {
},
HelpInfo: "clear all local cached files of the selected sdk",
})
ll.SetKeyEventForTable("w", table.KeyEvent{
Event: func(key string, l *table.List) tea.Cmd {
l.NextEvent = KeyEventWhatsInstalled
return tea.Quit
},
HelpInfo: "show the list of sdks installed by VMR on your system",
})
}

0 comments on commit c81c4ce

Please sign in to comment.