Compare commits
3 commits
eb2191045e
...
b680a2f5d7
| Author | SHA1 | Date | |
|---|---|---|---|
| b680a2f5d7 | |||
| 4d1f3f2f3e | |||
| f74f50c148 |
5 changed files with 200 additions and 81 deletions
85
actionsList.go
Normal file
85
actionsList.go
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gemini_site/gemlog"
|
||||||
|
|
||||||
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Action string
|
||||||
|
|
||||||
|
const (
|
||||||
|
Write Action = "write"
|
||||||
|
Read Action = "read"
|
||||||
|
Edit Action = "edit"
|
||||||
|
Delete Action = "delete"
|
||||||
|
)
|
||||||
|
|
||||||
|
var actions = []Action{Write, Read, Edit, Delete}
|
||||||
|
|
||||||
|
type ActionListPageModel struct {
|
||||||
|
cursor int
|
||||||
|
}
|
||||||
|
|
||||||
|
func initialActionListPageModel() ActionListPageModel {
|
||||||
|
return ActionListPageModel{
|
||||||
|
cursor: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m ActionListPageModel) InitActionListPage() tea.Cmd {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m ActionListPageModel) Update(msg tea.Msg, ctx *context) (ActionListPageModel, tea.Cmd) {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case tea.KeyMsg:
|
||||||
|
switch msg.String() {
|
||||||
|
case "up", "k":
|
||||||
|
if m.cursor > 0 {
|
||||||
|
m.cursor--
|
||||||
|
}
|
||||||
|
case "down", "j":
|
||||||
|
if m.cursor < len(actions)-1 {
|
||||||
|
m.cursor++
|
||||||
|
}
|
||||||
|
case "enter", " ":
|
||||||
|
action := actions[m.cursor]
|
||||||
|
switch action {
|
||||||
|
case Write:
|
||||||
|
return m, gemlog.WritePostCMD(ctx.config)
|
||||||
|
case Read:
|
||||||
|
switchPageCmd := func() tea.Msg {
|
||||||
|
return SwitchPages{Page: EntryList}
|
||||||
|
}
|
||||||
|
loadGemLogsCmd := gemlog.LoadGemlogCMD(ctx.config)
|
||||||
|
return m, tea.Batch(switchPageCmd, loadGemLogsCmd)
|
||||||
|
// case Edit:
|
||||||
|
// m.ui.page = EntryList
|
||||||
|
// m.ui.entryListPage.cursor = 0
|
||||||
|
// m.ui.entryListPage.actionToTake = Edit
|
||||||
|
// return m, gemlog.LoadGemlogCMD(ctx.config)
|
||||||
|
// case Delete:
|
||||||
|
// m.ui.page = EntryList
|
||||||
|
// m.ui.entryListPage.cursor = 0
|
||||||
|
// m.ui.entryListPage.actionToTake = Delete
|
||||||
|
// return m, gemlog.LoadGemlogCMD(m.context.config)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m ActionListPageModel) View() string {
|
||||||
|
s := "Welcome to gemlog cli!\n\nWhat post action would you like to take?\n\n"
|
||||||
|
for i, action := range actions {
|
||||||
|
cursor := " "
|
||||||
|
if m.cursor == i {
|
||||||
|
cursor = ">"
|
||||||
|
}
|
||||||
|
s += fmt.Sprintf("%s %s\n", cursor, action)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
80
entryList.go
Normal file
80
entryList.go
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gemini_site/gemlog"
|
||||||
|
|
||||||
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EntryListPageModel struct {
|
||||||
|
entries []gemlog.GemlogListEntry
|
||||||
|
actionToTake Action
|
||||||
|
cursor int
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitialEntryListPageModel() EntryListPageModel {
|
||||||
|
return EntryListPageModel{
|
||||||
|
cursor: 0,
|
||||||
|
actionToTake: Read,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m EntryListPageModel) InitEntryListPage() tea.Cmd {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m EntryListPageModel) Update(msg tea.Msg, ctx *context) (EntryListPageModel, tea.Cmd) {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case gemlog.GemLogsLoaded:
|
||||||
|
m.entries = msg.Logs
|
||||||
|
return m, nil
|
||||||
|
case tea.KeyMsg:
|
||||||
|
switch msg.String() {
|
||||||
|
case "up", "k":
|
||||||
|
if m.cursor > 0 {
|
||||||
|
m.cursor--
|
||||||
|
}
|
||||||
|
case "down", "j":
|
||||||
|
if m.cursor < len(actions)-1 {
|
||||||
|
m.cursor++
|
||||||
|
}
|
||||||
|
// case "enter", " ":
|
||||||
|
|
||||||
|
// action := actions[m.cursor]
|
||||||
|
// switch action {
|
||||||
|
// case Write:
|
||||||
|
// return m, gemlog.WritePostCMD(ctx.config)
|
||||||
|
// case Read:
|
||||||
|
// m.ui.page = EntryList
|
||||||
|
// m.ui.entryListPage.cursor = 0
|
||||||
|
// m.ui.entryListPage.actionToTake = Read
|
||||||
|
// return m, gemlog.LoadGemlogCMD(ctx.config)
|
||||||
|
// case Edit:
|
||||||
|
// m.ui.page = EntryList
|
||||||
|
// m.ui.entryListPage.cursor = 0
|
||||||
|
// m.ui.entryListPage.actionToTake = Edit
|
||||||
|
// return m, gemlog.LoadGemlogCMD(ctx.config)
|
||||||
|
// case Delete:
|
||||||
|
// m.ui.page = EntryList
|
||||||
|
// m.ui.entryListPage.cursor = 0
|
||||||
|
// m.ui.entryListPage.actionToTake = Delete
|
||||||
|
// return m, gemlog.LoadGemlogCMD(m.context.config)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m EntryListPageModel) View() string {
|
||||||
|
s := fmt.Sprintf("Which entry would you like to %s\n\n", m.actionToTake)
|
||||||
|
for i, entry := range m.entries {
|
||||||
|
cursor := " "
|
||||||
|
if m.cursor == i {
|
||||||
|
cursor = ">"
|
||||||
|
}
|
||||||
|
s += fmt.Sprintf("%s %s : %s\n", cursor, entry.Date, entry.Slug)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
@ -17,7 +17,6 @@ func genBasicAuthHeader(user, password string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func listGemLogs(config *Config) ([]GemlogListEntry, error) {
|
func listGemLogs(config *Config) ([]GemlogListEntry, error) {
|
||||||
slog.Info("Listing gemlogs from couchdb")
|
|
||||||
url := fmt.Sprintf("%s:%d/gemlog/_design/gemlog-cli/_view/list", config.CouchDB.Host, config.CouchDB.Port)
|
url := fmt.Sprintf("%s:%d/gemlog/_design/gemlog-cli/_view/list", config.CouchDB.Host, config.CouchDB.Port)
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,15 @@
|
||||||
package gemlog
|
package gemlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
|
||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadGemlogCMD(config *Config) tea.Cmd {
|
func LoadGemlogCMD(config *Config) tea.Cmd {
|
||||||
return func() tea.Msg {
|
return func() tea.Msg {
|
||||||
logs, err := listGemLogs(config)
|
logs, err := listGemLogs(config)
|
||||||
slog.Info("Loaded gemlogs", "count", len(logs))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrorMsg{err}
|
return ErrorMsg{err}
|
||||||
}
|
}
|
||||||
return GemLogsLoaded{logs}
|
return GemLogsLoaded{Logs: logs}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
110
main.go
110
main.go
|
|
@ -9,41 +9,23 @@ import (
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Action string
|
|
||||||
|
|
||||||
const (
|
|
||||||
Write Action = "write"
|
|
||||||
Read Action = "read"
|
|
||||||
Edit Action = "edit"
|
|
||||||
Delete Action = "delete"
|
|
||||||
)
|
|
||||||
|
|
||||||
var actions = []Action{Write, Read, Edit, Delete}
|
|
||||||
|
|
||||||
func TODOCmd() tea.Msg {
|
|
||||||
return gemlog.Notification("This action has not been implemented yet. Try another.")
|
|
||||||
}
|
|
||||||
|
|
||||||
type Page string
|
type Page string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ActionList Page = "actionList"
|
ActionList Page = "actionList"
|
||||||
EntryList Page = "entryList"
|
EntryList Page = "entryList"
|
||||||
Entry Page = "entry"
|
// Entry Page = "entry"
|
||||||
)
|
)
|
||||||
|
|
||||||
type entryListPageModel struct {
|
type SwitchPages struct{ Page Page }
|
||||||
entries []gemlog.GemlogListEntry
|
|
||||||
action Action
|
|
||||||
}
|
|
||||||
|
|
||||||
type uiState struct {
|
type uiState struct {
|
||||||
notification string
|
notification string
|
||||||
cursor int
|
|
||||||
errorTxt string
|
errorTxt string
|
||||||
|
|
||||||
page Page
|
page Page
|
||||||
entryListPage entryListPageModel
|
entryListPage EntryListPageModel
|
||||||
|
actionListPage ActionListPageModel
|
||||||
}
|
}
|
||||||
|
|
||||||
type context struct {
|
type context struct {
|
||||||
|
|
@ -59,9 +41,11 @@ func initialModel(config *gemlog.Config) model {
|
||||||
return model{
|
return model{
|
||||||
ui: uiState{
|
ui: uiState{
|
||||||
page: ActionList,
|
page: ActionList,
|
||||||
entryListPage: entryListPageModel{
|
// entryListPage: entryListPageModel{
|
||||||
entries: []gemlog.GemlogListEntry{},
|
// cursor: 0,
|
||||||
},
|
// entries: []gemlog.GemlogListEntry{},
|
||||||
|
// },
|
||||||
|
actionListPage: initialActionListPageModel(),
|
||||||
},
|
},
|
||||||
context: &context{
|
context: &context{
|
||||||
config: config,
|
config: config,
|
||||||
|
|
@ -70,47 +54,37 @@ func initialModel(config *gemlog.Config) model {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd {
|
func (m model) Init() tea.Cmd {
|
||||||
return tea.SetWindowTitle("Gemlog CLI")
|
cmds := make([]tea.Cmd, 0)
|
||||||
|
cmds = append(cmds, tea.SetWindowTitle("Gemlog CLI"))
|
||||||
|
// TODO: add init commands from other pages
|
||||||
|
return tea.Batch(cmds...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
|
cmds := make([]tea.Cmd, 0)
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
|
case SwitchPages:
|
||||||
|
m.ui.page = msg.Page
|
||||||
case gemlog.ErrorMsg:
|
case gemlog.ErrorMsg:
|
||||||
m.ui.errorTxt = fmt.Sprintf("%s\n\n", fmt.Errorf("%s", msg))
|
m.ui.errorTxt = fmt.Sprintf("%s\n\n", fmt.Errorf("%s", msg))
|
||||||
case gemlog.Notification:
|
case gemlog.Notification:
|
||||||
m.ui.notification = fmt.Sprintf("%s\n\n", string(msg))
|
m.ui.notification = fmt.Sprintf("%s\n\n", string(msg))
|
||||||
case gemlog.GemLogsLoaded:
|
|
||||||
m.ui.entryListPage.entries = msg.Logs
|
|
||||||
m.ui.cursor = 0
|
|
||||||
case tea.KeyMsg:
|
case tea.KeyMsg:
|
||||||
switch msg.String() {
|
switch msg.String() {
|
||||||
case "ctrl+c", "q":
|
case "ctrl+c", "q":
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
case "up", "k":
|
|
||||||
if m.ui.cursor > 0 {
|
|
||||||
m.ui.cursor--
|
|
||||||
}
|
|
||||||
case "down", "j":
|
|
||||||
if m.ui.cursor < len(actions)-1 {
|
|
||||||
m.ui.cursor++
|
|
||||||
}
|
|
||||||
case "enter", " ":
|
|
||||||
action := actions[m.ui.cursor]
|
|
||||||
switch action {
|
|
||||||
case Write:
|
|
||||||
return m, gemlog.WritePostCMD(m.context.config)
|
|
||||||
case Read:
|
|
||||||
m.ui.page = EntryList
|
|
||||||
return m, gemlog.LoadGemlogCMD(m.context.config)
|
|
||||||
case Edit:
|
|
||||||
return m, TODOCmd
|
|
||||||
case Delete:
|
|
||||||
return m, TODOCmd
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, nil
|
actionListM, cmd := m.ui.actionListPage.Update(msg, m.context)
|
||||||
|
m.ui.actionListPage = actionListM
|
||||||
|
cmds = append(cmds, cmd)
|
||||||
|
|
||||||
|
entryListM, cmd := m.ui.entryListPage.Update(msg, m.context)
|
||||||
|
m.ui.entryListPage = entryListM
|
||||||
|
cmds = append(cmds, cmd)
|
||||||
|
|
||||||
|
return m, tea.Batch(cmds...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m model) View() string {
|
func (m model) View() string {
|
||||||
|
|
@ -120,30 +94,12 @@ func (m model) View() string {
|
||||||
s := ""
|
s := ""
|
||||||
if m.ui.notification != "" {
|
if m.ui.notification != "" {
|
||||||
s += m.ui.notification
|
s += m.ui.notification
|
||||||
} else {
|
|
||||||
s += "Welcome to gemlog cli!\n\nWhat post action would you like to take?\n\n"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.ui.page == ActionList {
|
if m.ui.page == ActionList {
|
||||||
for i, action := range actions {
|
s += m.ui.actionListPage.View()
|
||||||
cursor := " "
|
} else if m.ui.page == EntryList {
|
||||||
if m.ui.cursor == i {
|
s += m.ui.entryListPage.View()
|
||||||
cursor = ">"
|
|
||||||
}
|
|
||||||
s += fmt.Sprintf("%s %s\n", cursor, action)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.ui.page == EntryList {
|
|
||||||
slog.Info("rendering entry list", "count", len(m.ui.entryListPage.entries))
|
|
||||||
for i, entry := range m.ui.entryListPage.entries {
|
|
||||||
slog.Info("entry", "value", entry)
|
|
||||||
cursor := " "
|
|
||||||
if m.ui.cursor == i {
|
|
||||||
cursor = ">"
|
|
||||||
}
|
|
||||||
s += fmt.Sprintf("%s %s : %s\n", cursor, entry.Date, entry.Slug)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
s += "\nPress q to quit.\n"
|
s += "\nPress q to quit.\n"
|
||||||
|
|
@ -160,13 +116,15 @@ func main() {
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
slog.Info("Starting gemlog cli")
|
slog.Info("Starting gemlog cli")
|
||||||
config, err := gemlog.LoadConfig()
|
config, err := gemlog.LoadConfig()
|
||||||
// gemlog.CheckDBConnection(config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error loading config: %v", err)
|
fmt.Printf("Error loading config: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
err = gemlog.CheckDBConnection(config)
|
||||||
// TODO: check if we can reach db before starting program
|
if err != nil {
|
||||||
|
fmt.Printf("Error checking db connection: %v", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
p := tea.NewProgram(initialModel(config))
|
p := tea.NewProgram(initialModel(config))
|
||||||
if _, err := p.Run(); err != nil {
|
if _, err := p.Run(); err != nil {
|
||||||
fmt.Printf("Alas, there's been an error: %v", err)
|
fmt.Printf("Alas, there's been an error: %v", err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue