-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.go
More file actions
130 lines (107 loc) · 2.61 KB
/
Copy pathmodel.go
File metadata and controls
130 lines (107 loc) · 2.61 KB
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
package main
import (
"fmt"
"os"
"charm.land/bubbles/v2/list"
"charm.land/bubbles/v2/textinput"
"charm.land/lipgloss/v2"
)
type states string
const (
list_state states = "list_state"
form_state states = "form_state"
path_input_state states = "path_input_state"
)
type Model struct {
// Size
width int
height int
// State
current_state states
configPath string
editingIndex int // -1 when adding new, >= 0 when editing
// List
main_list list.Model
// Form
focusedInputIndex int
inputs []textinput.Model
// Path Input
pathInput textinput.Model
}
func InitialModel() Model {
// List
main_list := list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0)
main_list.Title = "SSH CONFIG"
// Form
inputs := make([]textinput.Model, 5)
focusedStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
blurredStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
for i := range inputs {
t := textinput.New()
s := t.Styles()
s.Cursor.Color = lipgloss.Color("205")
s.Focused.Prompt = focusedStyle
s.Focused.Text = focusedStyle
s.Blurred.Prompt = blurredStyle
s.Focused.Text = focusedStyle
t.SetStyles(s)
t.SetWidth(50)
switch i {
case 0:
t.Placeholder = "Host"
t.Focus()
case 1:
t.Placeholder = "HostName"
case 2:
t.Placeholder = "User"
case 3:
t.Placeholder = "Port"
case 4:
t.Placeholder = "IdentityFile"
}
inputs[i] = t
}
// Path input
pathInput := textinput.New()
pathInput.Placeholder = "Enter path to ssh config..."
pathInput.Focus()
pathInput.SetWidth(50)
m := Model{
main_list: main_list,
current_state: list_state,
inputs: inputs,
focusedInputIndex: 0,
editingIndex: -1,
pathInput: pathInput,
}
m.configPath = GetDefaultSSHConfigPath()
if _, err := os.Stat(m.configPath); os.IsNotExist(err) {
m.current_state = path_input_state
} else {
m.LoadConfig()
}
return m
}
func (m *Model) LoadConfig() {
configs, err := ParseSSHConfig(m.configPath)
if err == nil {
var items []list.Item
for _, cfg := range configs {
items = append(items, Item{
title: cfg.Host,
desc: fmt.Sprintf("%s - %s", cfg.Hostname, cfg.User),
cfg: cfg,
})
}
m.main_list.SetItems(items)
}
}
// Item in the List
type Item struct {
title string
desc string
cfg SSHConfig
}
func (i Item) Title() string { return i.title }
func (i Item) Description() string { return i.desc }
func (i Item) FilterValue() string { return i.title }