Skip to content

Commit c81c4ce

Browse files
committed
show only installed sdk list for current system
1 parent e1d95f1 commit c81c4ce

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

internal/installer/install/common.go

+15
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ func GetSDKVersionDir(sdkName string) string {
2020
return d
2121
}
2222

23+
func IsSDKInstalledByVMR(sdkName string) bool {
24+
vd := GetSDKVersionDir(sdkName)
25+
dList, _ := os.ReadDir(vd)
26+
count := 0
27+
for _, d := range dList {
28+
if d.IsDir() {
29+
count++
30+
}
31+
}
32+
if count == 0 {
33+
os.RemoveAll(vd)
34+
}
35+
return count > 0
36+
}
37+
2338
/*
2439
============================
2540
Installation configs.

internal/tui/cmds/cmd.go

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ func (v *VmrTUI) ListSDKName() {
1818
v.SList = NewSDKSearcher()
1919
nextEvent, sdkName := v.SList.Show()
2020

21+
if nextEvent == KeyEventWhatsInstalled {
22+
// show SDKs already installed by vmr.
23+
nextEvent, sdkName = v.SList.ShowInstalledOnly()
24+
}
25+
2126
switch nextEvent {
2227
case KeyEventSeachVersionList:
2328
// search version list for selected sdkname.

internal/tui/cmds/list.go

+44
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
tea "github.com/charmbracelet/bubbletea"
55
"github.com/gvcgo/goutils/pkgs/gtea/gprint"
66
"github.com/gvcgo/version-manager/internal/download"
7+
"github.com/gvcgo/version-manager/internal/installer/install"
78
"github.com/gvcgo/version-manager/internal/terminal"
89
"github.com/gvcgo/version-manager/internal/tui/table"
910
"github.com/gvcgo/version-manager/internal/utils"
@@ -17,6 +18,7 @@ const (
1718
KeyEventRemoveLocalInstalled = "remove-installed-versions"
1819
KeyEventClearLocalCached = "clear-local-cached-files"
1920
KeyEventBacktoPreviousPage = "back-to-previous-page"
21+
KeyEventWhatsInstalled = "show-installed-sdks"
2022
)
2123

2224
/*
@@ -75,6 +77,41 @@ func (v *SDKSearcher) Show() (nextEvent, selectedItem string) {
7577
return
7678
}
7779

80+
func (v *SDKSearcher) ShowInstalledOnly() (nextEvent, selectedItem string) {
81+
ll := table.NewList()
82+
ll.SetListType(table.SDKList)
83+
v.RegisterKeyEvents(ll)
84+
85+
_, w, _ := terminal.GetTerminalSize()
86+
if w > 30 {
87+
w -= 30
88+
} else {
89+
w = 120
90+
}
91+
ll.SetHeader([]table.Column{
92+
{Title: "installed sdk", Width: 20},
93+
{Title: "homepage", Width: w},
94+
})
95+
rows := download.GetSDKSortedRows(v.SdkList)
96+
97+
installedRows := []table.Row{}
98+
for _, r := range rows {
99+
if install.IsSDKInstalledByVMR(r[0]) {
100+
installedRows = append(installedRows, r)
101+
}
102+
}
103+
if len(installedRows) == 0 {
104+
gprint.PrintWarning("no installed sdk found!")
105+
return
106+
}
107+
ll.SetRows(installedRows)
108+
ll.Run()
109+
110+
selectedItem = ll.GetSelected()
111+
nextEvent = ll.NextEvent
112+
return
113+
}
114+
78115
func (v *SDKSearcher) RegisterKeyEvents(ll *table.List) {
79116
// Open homepage.
80117
ll.SetKeyEventForTable("o", table.KeyEvent{
@@ -121,4 +158,11 @@ func (v *SDKSearcher) RegisterKeyEvents(ll *table.List) {
121158
},
122159
HelpInfo: "clear all local cached files of the selected sdk",
123160
})
161+
ll.SetKeyEventForTable("w", table.KeyEvent{
162+
Event: func(key string, l *table.List) tea.Cmd {
163+
l.NextEvent = KeyEventWhatsInstalled
164+
return tea.Quit
165+
},
166+
HelpInfo: "show the list of sdks installed by VMR on your system",
167+
})
124168
}

0 commit comments

Comments
 (0)