-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (114 loc) · 3.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Demo code for the Grid primitive.
package main
import (
"strings"
"os/exec"
"github.com/rivo/tview"
"log"
"os"
// "fmt"
"github.com/gdamore/tcell/v2"
)
func redrawmain(main *tview.List ,menu *tview.List ){
// get the venvs
main.Clear()
menu.Clear()
files,err := os.ReadDir("/home/mehdi/.config/lazyvenv/")
if err != nil{
log.Fatal(err)
}
//add the items to the list
c := 'a'
c2 := 'a'
for _,file := range files {
menu.AddItem(file.Name(), "",c , func(){
c2 = 'a'
a,_ := menu.GetItemText(menu.GetCurrentItem())
// inputField.SetText(a)
dirname , err := os.UserHomeDir()
out, err := exec.Command(dirname+"/.config/lazyvenv/"+a+"/bin/pip","freeze").Output()
if err != nil {
log.Fatal(err)
}
main.Clear()
for _,pack := range strings.Split(string(out),"\n") {
main.AddItem(pack, "",c2 ,nil)
c2++
}
})
c++
}
}
func main() {
// init the app (usefull for closing it lol)
app := tview.NewApplication()
main := tview.NewList() //the input field (to add or remove packages from the selected venv)
menu := tview.NewList()
//inputs :
inputField := tview.NewInputField().
SetLabel("add package: ")
inputField.SetDoneFunc(func(key tcell.Key) {
a,_ := menu.GetItemText(menu.GetCurrentItem())
dirname , err := os.UserHomeDir()
_, err = exec.Command(dirname+"/.config/lazyvenv/"+a+"/bin/pip","install" , inputField.GetText()).Output()
if err != nil {
log.Fatal(err)
}
// print(out)
redrawmain(main,menu)
})
venvInput := tview.NewInputField().
SetLabel("add venv: ")
venvInput.SetDoneFunc(func(key tcell.Key) {
a := venvInput.GetText()
// dirname , err := os.UserHomeDir()
_, err := exec.Command("./venv.sh",a).Output()
if err != nil {
log.Fatal(err)
}
redrawmain(main,menu)
})
// // title & helper
// title = tview.TextView("LazyVenv")
// helper = tview.TextView("mouse -> select items / C-c -> exit / d -> delete venv/package ")
redrawmain(main,menu)
main.SetBackgroundColor(tcell.ColorDefault).SetTitle("packages")
menu.SetBackgroundColor(tcell.ColorDefault).SetTitle("venvs")
inputField.SetBackgroundColor(tcell.ColorDefault)
venvInput.SetBackgroundColor(tcell.ColorDefault)
// title.SetBackgroundColor(tcell.ColorDefault)
// help.SetBackgroundColor(tcell.ColorDefault)
//add the inputField to the grid
grid := tview.NewGrid().
SetRows( 0, 1).
SetColumns(17,-1).
SetBorders(true).
AddItem(inputField, 1, 1, 1, 1, 0, 0, false).
AddItem(venvInput, 1, 0, 1, 1, 0, 0, false)
// add the venv list & the the packages list to the menu
// Layout for screens wider than 100 cells.
grid.AddItem(menu, 0, 0, 1, 1, 0, 100, false).
AddItem(main, 0, 1, 1, 1, 3, 90, true)
grid.SetBackgroundColor(tcell.ColorDefault)
// Key presses :
main.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Rune(){
case 'd':
print("deleted an item")
}
return event
})
menu.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Rune(){
case 'd':
print("deleted a venv")
case 'c':
print("cloning a venv")
}
return event
})
// start the app
if err := app.SetRoot(grid, true).EnableMouse(true).Run(); err != nil {
panic(err)
}
}