-
Notifications
You must be signed in to change notification settings - Fork 2
/
welcome.go
102 lines (81 loc) · 2.42 KB
/
welcome.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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
func welcomeUI(m Model) string {
var sb strings.Builder
sb.WriteString(headlineStyle.Render("Welcome to Hash. A password manager for everyone"))
sb.WriteString("\n")
sb.WriteString("Enter a master password to generate your secret key 🔑\n")
sb.WriteString(lBfaintTStyle.Render("A Secret key will be generated based on your master password and stored on your device.\nCombination of master password and secret key is used to decrypt your passwords.\nPlease remember your master password. There is no way to reset the master password."))
sb.WriteString("\n")
sb.WriteString(m.masterPassword.View())
sb.WriteString("\n\n")
if m.masterPasswordFocusIndex == 1 {
sb.WriteString(activeBtnStyle.Render("Submit"))
} else {
sb.WriteString(btnStyle.Render("Submit"))
}
sb.WriteString("\n")
sb.WriteString(faintTextStyle.Render(m.err.Error()))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("%s %s", boldFaintTextStyle.Render("ctrl+ →"), faintTextStyle.Render("login ")))
sb.WriteString(fmt.Sprintf("%s %s", boldFaintTextStyle.Render("・ ctrl+c/esc"), faintTextStyle.Render("exit")))
return sb.String()
}
func onWelcomeUpdate(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyDown:
if m.masterPasswordFocusIndex >= 1 {
m.masterPasswordFocusIndex = 0
} else {
m.masterPasswordFocusIndex++
}
case tea.KeyUp:
if m.masterPasswordFocusIndex == 0 {
m.masterPasswordFocusIndex = 1
} else {
m.masterPasswordFocusIndex--
}
case tea.KeyEnter:
salt, err := generateSalt([]byte(m.masterPassword.Value()))
if err != nil {
m.err = err
return m, nil
}
err = os.WriteFile(filepath.Join(configDir, "salt.txt"), byteToHex(salt), 0644)
if err != nil {
m.err = err
return m, nil
}
m.salt = salt
m.err = errors.New("secret key generated. press ctrl+ → to login page")
case tea.KeyCtrlRight:
m.pageIndex = loginPage
return m, nil
}
case error:
if errors.Is(msg, os.ErrExist) {
m.err = errors.New("secret key exist")
m.pageIndex = loginPage
return m, nil
}
m.err = msg
return m, nil
}
if m.masterPasswordFocusIndex == 0 {
m.masterPassword.Focus()
} else {
m.masterPassword.Blur()
}
tm, tc := m.masterPassword.Update(msg)
m.masterPassword = tm
return m, tc
}