From 6b8ba76018f859013d6d8d643adce36385dff5e5 Mon Sep 17 00:00:00 2001 From: Travis Shears Date: Sun, 5 Oct 2025 14:22:11 +0200 Subject: [PATCH] get updating posts working --- gemlog/core.go | 10 ++-- gemlog/db.go | 38 +++++++++++- internal/ui/app.go | 2 +- internal/ui/entry.go | 21 ++----- internal/ui/entryList.go | 7 ++- internal/ui/write.go | 123 +++++++++++++++++++++++++++++---------- 6 files changed, 148 insertions(+), 53 deletions(-) diff --git a/gemlog/core.go b/gemlog/core.go index 4b369c6..07f3d10 100644 --- a/gemlog/core.go +++ b/gemlog/core.go @@ -9,11 +9,11 @@ import ( // GemlogEntry represents a single gemlog post entry type GemlogEntry struct { - Title string `json:"title"` - Slug string `json:"slug"` - Date time.Time `json:"date"` - Tags []string `json:"tags"` - Gemtxt string `json:"gemtxt"` + Title string `json:"title"` + Slug string `json:"slug"` + Date time.Time `json:"date"` + // Tags []string `json:"tags"` + Gemtxt string `json:"gemtxt"` } type GemlogListEntry struct { diff --git a/gemlog/db.go b/gemlog/db.go index 3d21ce4..e3b0efa 100644 --- a/gemlog/db.go +++ b/gemlog/db.go @@ -119,7 +119,7 @@ func ReadGemlogEntry(config *Config, id string) (GemlogEntry, error) { Slug: rawData.Slug, Date: rawData.Date, Gemtxt: rawData.GemText, - Tags: make([]string, 0), + // Tags: make([]string, 0), }, nil } @@ -151,6 +151,42 @@ func DeleteGemlogEntry(config *Config, id string, rev string) error { return nil } +func UpdateGemlogEntry(config *Config, entry *GemlogEntry, id string, rev string) error { + url := fmt.Sprintf("%s:%d/gemlog/%s?rev=%s", config.CouchDB.Host, config.CouchDB.Port, id, rev) + + // Marshal the entry struct to JSON + jsonData, err := json.Marshal(entry) + if err != nil { + return fmt.Errorf("failed to marshal entry: %w", err) + } + + req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonData)) + if err != nil { + return fmt.Errorf("failed to create request: %w", err) + } + + req.Header.Add("authorization", genBasicAuthHeader(config.CouchDB.User, config.CouchDB.Password)) + req.Header.Add("content-type", "application/json") + slog.Info("Sending request to update gemlog entry", "url", url, "data", string(jsonData)) + + res, err := http.DefaultClient.Do(req) + if err != nil { + return fmt.Errorf("failed to send request: %w", err) + } + defer res.Body.Close() + + body, err := io.ReadAll(res.Body) + if err != nil { + return fmt.Errorf("failed to read response body: %w", err) + } + + if res.StatusCode < 200 || res.StatusCode >= 300 { + return fmt.Errorf("unexpected status code %d: %s", res.StatusCode, string(body)) + } + + return nil +} + func SaveGemlogEntry(config *Config, entry *GemlogEntry) error { url := fmt.Sprintf("%s:%d/gemlog/", config.CouchDB.Host, config.CouchDB.Port) diff --git a/internal/ui/app.go b/internal/ui/app.go index a50d124..0887f3c 100644 --- a/internal/ui/app.go +++ b/internal/ui/app.go @@ -118,7 +118,7 @@ func (m model) View() string { return s } -var enableLogs bool = false +var enableLogs bool = true func Run() { if enableLogs { diff --git a/internal/ui/entry.go b/internal/ui/entry.go index 79d2a65..7179869 100644 --- a/internal/ui/entry.go +++ b/internal/ui/entry.go @@ -20,15 +20,6 @@ func initialEntryPageModel() EntryPageModel { return EntryPageModel{} } -func transformEntryToContent(entry gemlog.GemlogEntry) string { - content := entry.Slug - content += "\n\n" - content += entry.Gemtxt - content += "\n\n-------------------------\n" - content += "\n\nPress h or left arrow to go back" - return content -} - func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPageModel, tea.Cmd) { var ( cmd tea.Cmd @@ -37,7 +28,7 @@ func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPag switch msg := msg.(type) { case GemLogLoaded: m.entry = msg.Log - m.viewport.SetContent(transformEntryToContent(m.entry)) + m.viewport.SetContent(m.entry.Gemtxt) case tea.KeyMsg: if !active { return m, nil @@ -50,7 +41,7 @@ func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPag return m, cmd } case tea.WindowSizeMsg: - headerHeight := lipgloss.Height(m.headerView()) + headerHeight := lipgloss.Height(m.headerView("loading slug...")) footerHeight := lipgloss.Height(m.footerView()) verticalMarginHeight := headerHeight + footerHeight @@ -63,7 +54,7 @@ func (m EntryPageModel) Update(msg tea.Msg, active bool, ctx *context) (EntryPag m.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight) m.viewport.YPosition = headerHeight - m.viewport.SetContent(transformEntryToContent(m.entry)) + m.viewport.SetContent(m.entry.Gemtxt) m.ready = true } else { m.viewport.Width = msg.Width @@ -83,7 +74,7 @@ func (m EntryPageModel) View() string { return "\n Initializing..." } - return fmt.Sprintf("%s\n%s\n%s", m.headerView(), m.viewport.View(), m.footerView()) + return fmt.Sprintf("%s\n%s\n%s", m.headerView(m.entry.Slug), m.viewport.View(), m.footerView()) } var ( @@ -100,8 +91,8 @@ var ( }() ) -func (m EntryPageModel) headerView() string { - title := titleStyle.Render("Mr. Pager") +func (m EntryPageModel) headerView(slug string) string { + title := titleStyle.Render(slug) line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(title))) return lipgloss.JoinHorizontal(lipgloss.Center, title, line) } diff --git a/internal/ui/entryList.go b/internal/ui/entryList.go index ce6ba33..5be6dfc 100644 --- a/internal/ui/entryList.go +++ b/internal/ui/entryList.go @@ -52,7 +52,12 @@ func (m EntryListPageModel) Update(msg tea.Msg, active bool, ctx *context) (Entr id := m.entries[m.cursor].ID rev := m.entries[m.cursor].Rev switch m.actionToTake { - // TODO: handle edit + case Edit: + editCmd := EditPostCMD(ctx.config, id, rev) + navCmd := func() tea.Msg { + return SwitchPages{Page: ActionList} + } + return m, tea.Sequence(editCmd, navCmd) case Read: loadCmd := LoadGemlogCMD(ctx.config, id) navCmd := func() tea.Msg { diff --git a/internal/ui/write.go b/internal/ui/write.go index 1f31516..15ba95e 100644 --- a/internal/ui/write.go +++ b/internal/ui/write.go @@ -12,8 +12,84 @@ import ( "gopkg.in/yaml.v3" ) +type Frontmatter struct { + Title string `yaml:"title"` + Date string `yaml:"date"` + Slug string `yaml:"slug"` + // Tags []string `yaml:"tags"` +} + // TODO: add edit command -// func EditPostCMD(config *gemlog.Config) tea.Cmd {} +func EditPostCMD(config *gemlog.Config, id string, rev string) tea.Cmd { + return func() tea.Msg { + gemlogEntry, err := gemlog.ReadGemlogEntry(config, id) + if err != nil { + return ErrorMsg{fmt.Errorf("failed to read gemlog entry: %w", err)} + } + + // Create a temporary file + tmpFile, err := os.CreateTemp("/tmp", "gemlog-*.md") + if err != nil { + return ErrorMsg{fmt.Errorf("failed to create temporary file: %w", err)} + } + + frontmatter := Frontmatter{ + Title: gemlogEntry.Title, + Date: gemlogEntry.Date.Format("2006-01-02"), + Slug: gemlogEntry.Slug, + // Tags: []string{}, // or nil if empty is ok + } + yamlData, err := yaml.Marshal(&frontmatter) + if err != nil { + return ErrorMsg{fmt.Errorf("failed to marshal frontmatter: %w", err)} + } + + initialContent := append(yamlData, []byte("---\n"+gemlogEntry.Gemtxt)...) + + if _, err := tmpFile.Write(initialContent); err != nil { + tmpFile.Close() + os.Remove(tmpFile.Name()) + return ErrorMsg{fmt.Errorf("failed to write initial content: %w", err)} + } + tmpFile.Close() + + // Get the editor from environment variable, default to vim + editor := os.Getenv("EDITOR") + if editor == "" { + editor = "vim" + } + + // Create the command to open the editor with the temp file + c := exec.Command(editor, tmpFile.Name()) //nolint:gosec + + // Return tea.ExecProcess which will suspend the TUI and run the editor + return tea.ExecProcess(c, func(err error) tea.Msg { + defer os.Remove(tmpFile.Name()) // Clean up the temp file + + if err != nil { + return ErrorMsg{fmt.Errorf("editor command failed: %w", err)} + } + + // Read the contents of the file after editing + content, readErr := os.ReadFile(tmpFile.Name()) + if readErr != nil { + return ErrorMsg{fmt.Errorf("failed to read file contents: %w", readErr)} + } + + gemlogEntry, err := parsePost(string(content)) + if err != nil { + return ErrorMsg{fmt.Errorf("failed to parse post: %w", err)} + } + if err := gemlog.UpdateGemlogEntry(config, &gemlogEntry, id, rev); err != nil { + return ErrorMsg{fmt.Errorf("failed to save gemlog entry: %w", err)} + } + + // Return success with the content + return Notification(fmt.Sprintf("Post updated: \ngemini://travisshears.com/gemlog/%s\n\n", gemlogEntry.Slug)) + })() + } + +} func WritePostCMD(config *gemlog.Config) tea.Cmd { return func() tea.Msg { @@ -23,15 +99,7 @@ func WritePostCMD(config *gemlog.Config) tea.Cmd { return ErrorMsg{fmt.Errorf("failed to create temporary file: %w", err)} } - // Load initial content from template file - initialContent, err := os.ReadFile("templates/default_post.gmi") - if err != nil { - tmpFile.Close() - os.Remove(tmpFile.Name()) - return ErrorMsg{fmt.Errorf("failed to read template file: %w", err)} - } - - if _, err := tmpFile.Write(initialContent); err != nil { + if _, err := tmpFile.Write([]byte(defaultTemplate)); err != nil { tmpFile.Close() os.Remove(tmpFile.Name()) return ErrorMsg{fmt.Errorf("failed to write initial content: %w", err)} @@ -99,12 +167,7 @@ func parsePost(post string) (gemlog.GemlogEntry, error) { // Parse frontmatter YAML frontmatterYAML := strings.Join(frontmatterLines, "\n") - var frontmatter struct { - Title string `yaml:"title"` - Date string `yaml:"date"` - Slug string `yaml:"slug"` - Tags string `yaml:"tags"` - } + var frontmatter Frontmatter if err := yaml.Unmarshal([]byte(frontmatterYAML), &frontmatter); err != nil { return gemlog.GemlogEntry{}, fmt.Errorf("failed to parse frontmatter: %w", err) @@ -117,25 +180,25 @@ func parsePost(post string) (gemlog.GemlogEntry, error) { } // Parse tags (comma-separated) - var tags []string - if frontmatter.Tags != "" { - tagParts := strings.Split(frontmatter.Tags, ",") - for _, tag := range tagParts { - trimmed := strings.TrimSpace(tag) - if trimmed != "" { - tags = append(tags, trimmed) - } - } - } + // var tags []string + // if frontmatter.Tags != "" { + // tagParts := strings.Split(frontmatter.Tags, ",") + // for _, tag := range tagParts { + // trimmed := strings.TrimSpace(tag) + // if trimmed != "" { + // tags = append(tags, trimmed) + // } + // } + // } // Join body lines body := strings.Join(bodyLines, "\n") return gemlog.GemlogEntry{ - Title: frontmatter.Title, - Date: date, - Slug: frontmatter.Slug, - Tags: tags, + Title: frontmatter.Title, + Date: date, + Slug: frontmatter.Slug, + // Tags: tags, Gemtxt: body, }, nil }