52160345bf
Complete implementation of note management CLI with all core features: Commands: - add: Create new notes in $EDITOR with auto-generated filenames - list: Display all notes with titles, paths, and tags - search: Full-text search via ripgrep, tag-based filtering - tags: List all tags with occurrence counts - edit: Fuzzy search and edit notes by title - rm: Move notes to trash with confirmation prompt Features: - Automatic depository structure initialization (.jade/trash/) - Configurable tag prefix (default '+') - Parse title from first # heading (filename fallback) - Extract tags anywhere in content - Parse both [[wiki-links]] and [markdown](links) - Trash system with timestamps to prevent conflicts Technical: - Global config at ~/.config/jade/config.yml - Per-depository settings support - Ripgrep integration for fast search - $EDITOR integration for note editing - Comprehensive README with usage examples
87 lines
1.9 KiB
Go
87 lines
1.9 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.jnss.me/joakim/jadedepo/internal/engine"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
editCmd = &cobra.Command{
|
|
Use: "edit [title]",
|
|
Short: "Edit a note by searching for its title",
|
|
Args: cobra.MinimumNArgs(1),
|
|
Run: editNote,
|
|
}
|
|
)
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(editCmd)
|
|
}
|
|
|
|
func editNote(cmd *cobra.Command, args []string) {
|
|
jd, err := engine.GetInstance()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Search for note by title
|
|
searchTitle := strings.Join(args, " ")
|
|
matches, err := jd.FindNoteByTitle(searchTitle)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error searching for note: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if len(matches) == 0 {
|
|
fmt.Fprintf(os.Stderr, "No notes found matching '%s'\n", searchTitle)
|
|
os.Exit(1)
|
|
}
|
|
|
|
var noteToEdit *engine.Note
|
|
|
|
if len(matches) == 1 {
|
|
noteToEdit = matches[0]
|
|
} else {
|
|
// Multiple matches - show options
|
|
fmt.Printf("Multiple notes found matching '%s':\n\n", searchTitle)
|
|
for i, note := range matches {
|
|
fmt.Printf(" %d. %s (%s)\n", i+1, note.Title, note.Path)
|
|
}
|
|
fmt.Print("\nSelect note number (1-", len(matches), "): ")
|
|
|
|
var selection int
|
|
_, err := fmt.Scanf("%d", &selection)
|
|
if err != nil || selection < 1 || selection > len(matches) {
|
|
fmt.Fprintf(os.Stderr, "Invalid selection\n")
|
|
os.Exit(1)
|
|
}
|
|
noteToEdit = matches[selection-1]
|
|
}
|
|
|
|
// Open note in editor
|
|
notePath := filepath.Join(jd.Config.DepoPath, noteToEdit.Path)
|
|
editor := os.Getenv("EDITOR")
|
|
if editor == "" {
|
|
editor = "vi" // fallback
|
|
}
|
|
|
|
editorCmd := exec.Command(editor, notePath)
|
|
editorCmd.Stdin = os.Stdin
|
|
editorCmd.Stdout = os.Stdout
|
|
editorCmd.Stderr = os.Stderr
|
|
|
|
if err := editorCmd.Run(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error opening editor: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("Edited note: %s\n", noteToEdit.Title)
|
|
}
|