|
| 1 | +package edit |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/abdfnx/bubbles/list" |
| 8 | + "github.com/charmbracelet/lipgloss" |
| 9 | + "github.com/scmn-dev/secman/constants" |
| 10 | + "github.com/scmn-dev/secman/pkg/options" |
| 11 | + tea "github.com/charmbracelet/bubbletea" |
| 12 | + "github.com/scmn-dev/secman/internal/shared" |
| 13 | + "github.com/scmn-dev/secman/pkg/context/edit/editor" |
| 14 | +) |
| 15 | + |
| 16 | +type model struct { |
| 17 | + styles shared.Styles |
| 18 | + list list.Model |
| 19 | + pwType string |
| 20 | + password string |
| 21 | +} |
| 22 | + |
| 23 | +func (m model) Init() tea.Cmd { |
| 24 | + return nil |
| 25 | +} |
| 26 | + |
| 27 | +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 28 | + switch msg := msg.(type) { |
| 29 | + case tea.WindowSizeMsg: |
| 30 | + m.list.SetWidth(msg.Width) |
| 31 | + return m, nil |
| 32 | + |
| 33 | + case tea.KeyMsg: |
| 34 | + switch keypress := msg.String(); keypress { |
| 35 | + case "ctrl+c": |
| 36 | + return m, tea.Quit |
| 37 | + |
| 38 | + case "enter": |
| 39 | + i, ok := m.list.SelectedItem().(shared.Item) |
| 40 | + |
| 41 | + if ok { |
| 42 | + if err := tea.NewProgram(editor.Editor(m.pwType, string(i), m.password)).Start(); err != nil { |
| 43 | + fmt.Printf("could not start editor: %s\n", err) |
| 44 | + os.Exit(1) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return m, tea.Quit |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + var cmd tea.Cmd |
| 53 | + |
| 54 | + m.list, cmd = m.list.Update(msg) |
| 55 | + |
| 56 | + return m, cmd |
| 57 | +} |
| 58 | + |
| 59 | +func (m model) View() string { |
| 60 | + return lipgloss.NewStyle().PaddingLeft(2).SetString(constants.Logo("Secman Editor")).String() + "\n" + m.list.View() |
| 61 | +} |
| 62 | + |
| 63 | +func Edit(o *options.PasswordsOptions) { |
| 64 | + items := []list.Item{} |
| 65 | + st := shared.DefaultStyles() |
| 66 | + var p = fmt.Sprintf("\"%s\"", o.Password) |
| 67 | + |
| 68 | + if o.Logins { |
| 69 | + items = append(items, shared.Item("Title"), shared.Item("Username"), shared.Item("Password"), shared.Item("URL"), shared.Item("Extra")) |
| 70 | + } else if o.CreditCards { |
| 71 | + items = append(items, shared.Item("Card Name"), shared.Item("Card Holder Name"), shared.Item("Card Type"), shared.Item("Number"), shared.Item("Expiry Date"), shared.Item("Verification Number")) |
| 72 | + } else if o.Emails { |
| 73 | + items = append(items, shared.Item("Email Address"), shared.Item("Password")) |
| 74 | + } else if o.Notes { |
| 75 | + items = append(items, shared.Item("Title"), shared.Item("Note")) |
| 76 | + } else if o.Servers { |
| 77 | + items = append(items, shared.Item("Title"), shared.Item("URL"), shared.Item("Username"), shared.Item("Password"), shared.Item("Hosting Username"), shared.Item("Hosting Password"), shared.Item("Admin Username"), shared.Item("Admin Password"), shared.Item("Extra")) |
| 78 | + } |
| 79 | + |
| 80 | + l := list.New(items, shared.ItemDelegate{}, constants.LIST_WIDTH, constants.LIST_HEIGHT) |
| 81 | + l.Title = "\nChoose which password to edit, or press `q` to quit." |
| 82 | + l.SetShowStatusBar(false) |
| 83 | + l.SetFilteringEnabled(false) |
| 84 | + l.Styles.Title = st.BaseStyle |
| 85 | + l.Styles.PaginationStyle = st.Pagination |
| 86 | + |
| 87 | + m := model{ |
| 88 | + styles: st, |
| 89 | + list: l, |
| 90 | + pwType: shared.PasswordType(o), |
| 91 | + password: p, |
| 92 | + } |
| 93 | + |
| 94 | + if err := tea.NewProgram(m).Start(); err != nil { |
| 95 | + fmt.Println("Error running program:", err) |
| 96 | + os.Exit(1) |
| 97 | + } |
| 98 | +} |
0 commit comments