-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ee269d
commit dff66f4
Showing
4 changed files
with
244 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/charmbracelet/bubbles/cursor" | ||
"github.com/charmbracelet/bubbles/textinput" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
var ( | ||
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205")) | ||
blurredStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240")) | ||
cursorStyle = focusedStyle.Copy() | ||
noStyle = lipgloss.NewStyle() | ||
helpStyle = blurredStyle.Copy() | ||
cursorModeHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244")) | ||
|
||
focusedButton = focusedStyle.Copy().Render("[ Submit ]") | ||
blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit")) | ||
) | ||
|
||
type FormModel struct { | ||
focusIndex int | ||
form []textinput.Model | ||
cursorMode cursor.Mode | ||
} | ||
|
||
func NewFormModel() FormModel { | ||
t := textinput.New() | ||
t.Placeholder = "Url" | ||
t.Focus() | ||
|
||
m := FormModel{ | ||
form: []textinput.Model{t}, | ||
} | ||
|
||
return m | ||
} | ||
|
||
func (m FormModel) Init() tea.Cmd { | ||
return textinput.Blink | ||
} | ||
|
||
func UpdateForm(m FormModel, msg tea.Msg) (FormModel, tea.Cmd) { | ||
cmd := m.updateInputs(msg) | ||
|
||
return m, cmd | ||
} | ||
|
||
func (m *FormModel) updateInputs(msg tea.Msg) tea.Cmd { | ||
cmds := make([]tea.Cmd, len(m.form)) | ||
|
||
for i := range m.form { | ||
m.form[i], cmds[i] = m.form[i].Update(msg) | ||
} | ||
|
||
return tea.Batch(cmds...) | ||
} | ||
|
||
func (m FormModel) View() string { | ||
var b strings.Builder | ||
inputStyle := lipgloss.NewStyle(). | ||
Background(lipgloss.Color("62")). | ||
Foreground(lipgloss.Color("230")). | ||
Padding(0, 1) | ||
b.WriteString(" " + inputStyle.Render("Bookmark")) | ||
b.WriteString("\n\n") | ||
|
||
for i := range m.form { | ||
b.WriteString(" " + m.form[i].View()) | ||
} | ||
|
||
button := &blurredButton | ||
fmt.Fprintf(&b, "\n\n %s\n\n", *button) | ||
|
||
return b.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
|
||
"github.com/charmbracelet/bubbles/key" | ||
"github.com/charmbracelet/bubbles/list" | ||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
"github.com/edgarlatorre/bookmark/internal/repositories" | ||
) | ||
|
||
var docStyle = lipgloss.NewStyle().Margin(1, 2) | ||
|
||
type keymap struct { | ||
Create key.Binding | ||
} | ||
|
||
var Keymap = keymap{ | ||
Create: key.NewBinding( | ||
key.WithKeys("n"), | ||
key.WithHelp("n", "New url"), | ||
), | ||
} | ||
|
||
type item struct { | ||
title, url string | ||
} | ||
|
||
type ListModel struct { | ||
model list.Model | ||
} | ||
|
||
func (i item) Title() string { return i.title } | ||
func (i item) Description() string { return i.url } | ||
func (i item) FilterValue() string { return i.title } | ||
|
||
func NewListModel() list.Model { | ||
urls, err := repositories.Read("urls.json") | ||
m := list.Model{} | ||
|
||
if err != nil { | ||
fmt.Println("Error to read json file:", err) | ||
|
||
return m | ||
} | ||
|
||
items := make([]list.Item, len(urls)) | ||
|
||
for i, u := range urls { | ||
items[i] = item{title: u.Name, url: u.Url} | ||
} | ||
|
||
m = list.New(items, list.NewDefaultDelegate(), 0, 0) | ||
m.Title = "Bookmark" | ||
m.AdditionalShortHelpKeys = func() []key.Binding { | ||
return []key.Binding{ | ||
Keymap.Create, | ||
} | ||
} | ||
|
||
return m | ||
} | ||
|
||
func UpdateList(m list.Model, msg tea.Msg) (list.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "ctrl+c", "q": | ||
return m, tea.Quit | ||
case "c": | ||
return m, tea.Quit | ||
case "enter", " ": | ||
if item, ok := m.SelectedItem().(item); ok { | ||
cmd := exec.Command("open", item.Description()) | ||
_, err := cmd.Output() | ||
|
||
if err != nil { | ||
fmt.Println(err.Error()) | ||
} | ||
} else { | ||
fmt.Println("Not found") | ||
} | ||
} | ||
case tea.WindowSizeMsg: | ||
h, v := docStyle.GetFrameSize() | ||
m.SetSize(msg.Width-h, msg.Height-v) | ||
} | ||
|
||
var cmd tea.Cmd | ||
m, cmd = m.Update(msg) | ||
|
||
return m, cmd | ||
} | ||
|
||
func (m ListModel) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m ListModel) View() string { | ||
return m.model.View() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package models | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/edgarlatorre/bookmark/internal/models" | ||
) | ||
|
||
func TestFormModelView(t *testing.T) { | ||
m := models.NewFormModel() | ||
content := m.View() | ||
|
||
if !strings.Contains(content, "Url") { | ||
t.Fatalf(`View() does not contain Url, %s, error`, content) | ||
} | ||
} |