Files
nib-v1/cmd/add.go
T
lerko a6fda5d1ee feat(cli): add nib add + nib ls commands
Default command delegation: `nib "..."` routes to `nib add`. Capture
bar parses grammar, creates entities. `nib ls` lists with date
grouping, tag filter, 48h default window. Display glyphs for all
entity types.
2026-05-14 11:17:27 -04:00

71 lines
1.4 KiB
Go

package cmd
import (
"fmt"
"strings"
"github.com/lerko/nib/internal/db"
"github.com/lerko/nib/internal/display"
"github.com/lerko/nib/internal/parse"
"github.com/spf13/cobra"
)
var addCmd = &cobra.Command{
Use: "add [input]",
Short: "capture a new entity",
Args: cobra.MinimumNArgs(1),
RunE: runAdd,
}
func runAdd(_ *cobra.Command, args []string) error {
input := strings.Join(args, " ")
parsed, err := parse.Parse(input)
if err != nil {
return fmt.Errorf("parse: %w", err)
}
store, err := openStore()
if err != nil {
return err
}
defer store.Close()
e := &db.Entity{
Body: parsed.Body,
Glyph: db.Glyph(parsed.Glyph),
Tags: parsed.Tags,
}
if parsed.TimeAnchor != nil {
e.TimeAnchor = parsed.TimeAnchor
}
if parsed.CardSuffix != nil {
ct := db.CardType(*parsed.CardSuffix)
e.CardType = &ct
}
if err := store.Create(e); err != nil {
return err
}
glyph := display.DisplayGlyph(e.Glyph, e.CardType)
shortID := display.FormatID(e.ID)
var parts []string
parts = append(parts, glyph)
parts = append(parts, " "+e.Body)
if e.TimeAnchor != nil {
parts = append(parts, " @"+*e.TimeAnchor)
}
for _, tag := range e.Tags {
parts = append(parts, " #"+tag)
}
parts = append(parts, " ["+shortID+"]")
if e.CardType != nil {
parts = append(parts, fmt.Sprintf(" (%s)", *e.CardType))
}
fmt.Println(strings.Join(parts, ""))
return nil
}