-
Notifications
You must be signed in to change notification settings - Fork 2
/
tui.go
166 lines (143 loc) · 4.23 KB
/
tui.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"database/sql"
"errors"
"fmt"
"os"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/hrishikesh/hash/database"
)
const (
loginPage = iota
allPasswordsPage
newPasswordPage
viewPasswordPage
updatePasswordPage
welcomePage
)
var (
lBfaintTStyle = lipgloss.NewStyle().Faint(true).BorderStyle(lipgloss.NormalBorder()).BorderLeft(true).BorderLeftForeground(lipgloss.Color("#7D56F4")).Margin(1, 0).Padding(0, 1)
boldTextStyle = lipgloss.NewStyle().Bold(true)
boldFaintTextStyle = boldTextStyle.Copy().Faint(true)
faintTextStyle = lipgloss.NewStyle().Faint(true)
headlineStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#ff006e")).Bold(true)
boldtextWBackground = lipgloss.NewStyle().Bold(true).Background(lipgloss.Color("#8338ec")).Padding(0, 1)
btnStyle = boldTextStyle.Copy().Foreground(lipgloss.Color("#FFFFFF")).Padding(0, 1).Border(lipgloss.NormalBorder())
activeBtnStyle = btnStyle.Copy().Foreground(lipgloss.Color("#8338ec")).BorderForeground(lipgloss.Color("#8338ec"))
listItemStyle = lipgloss.NewStyle().Padding(0, 1).Faint(true)
listItemHeaderStyle = listItemStyle.Copy().Bold(true).UnsetFaint()
listItemSelectedStyle = listItemStyle.Copy().BorderStyle(lipgloss.NormalBorder()).BorderLeft(true).BorderLeftForeground(lipgloss.Color("#7D56F4"))
listItemSelectedHeaderStyle = listItemSelectedStyle.Copy().Bold(true).UnsetFaint()
)
type Model struct {
pageIndex int
salt []byte
masterPasswordFocusIndex int
masterPassword textinput.Model
secretKey [32]byte
focusIndex int
txtInputs []textinput.Model
allPassword []database.PWItem
passwordIndex int
err error
}
type saltFound []byte
func initialModel() Model {
m := Model{
pageIndex: loginPage,
focusIndex: 0,
txtInputs: make([]textinput.Model, 3),
err: errors.New(""),
passwordIndex: 0,
}
// setting master password text filed
masterPasswordTxtField := textinput.New()
masterPasswordTxtField.CharLimit = 200
masterPasswordTxtField.Width = 50
masterPasswordTxtField.Placeholder = "Master password"
masterPasswordTxtField.EchoMode = textinput.EchoPassword
masterPasswordTxtField.EchoCharacter = '*'
m.masterPassword = masterPasswordTxtField
for i := range m.txtInputs {
t := textinput.New()
t.CharLimit = 156
t.Width = 30
switch i {
case 0:
t.Placeholder = "Name"
t.Focus()
case 1:
t.Placeholder = "Email"
case 2:
t.Placeholder = "Password"
t.EchoMode = textinput.EchoPassword
t.EchoCharacter = '*'
}
m.txtInputs[i] = t
}
// Getting all the password
var err error
m.allPassword, err = database.ReadAllPasswords()
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
m.err = errors.New("such an empty. no password found")
} else {
m.err = err
}
}
return m
}
func (m Model) Init() tea.Cmd {
return readSaltFileTUI
}
func (m Model) View() string {
switch m.pageIndex {
case loginPage:
return loginUI(m)
case allPasswordsPage:
return passwordListUI(m)
case newPasswordPage:
return newPasswordUI(m)
case viewPasswordPage:
return viewPasswordUI(m)
case updatePasswordPage:
return updatePasswordUI(m)
case welcomePage:
return welcomeUI(m)
}
return fmt.Sprintln("No page selected")
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
}
case saltFound:
m.salt = []byte(msg)
case error:
if errors.Is(msg, os.ErrNotExist) {
m.err = errors.New("secret key not found")
m.pageIndex = welcomePage
return m, nil
}
}
switch m.pageIndex {
case loginPage:
return onLoginUpdate(msg, m)
case allPasswordsPage:
return onPaswordListUpdate(msg, m)
case viewPasswordPage:
return onviewPasswordUpdate(msg, m)
case newPasswordPage:
return onNewPasswordUpdate(msg, m)
case updatePasswordPage:
return onPasswordUpdate(msg, m)
case welcomePage:
return onWelcomeUpdate(msg, m)
}
return m, nil
}