From 27e21389bb9c2a1a1438c22f00a8832f804d2c16 Mon Sep 17 00:00:00 2001 From: Nate Meyer <672246+notnmeyer@users.noreply.github.com> Date: Sat, 20 Jun 2026 19:46:38 -0700 Subject: [PATCH] -m for one-liner updates --- README.md | 2 ++ cmd/root.go | 15 +++++++++++++++ cmd/root_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/README.md b/README.md index 72b418b..e964fde 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ To view today's log, run `daylog show`. To interact with a past or future log supply a date (`daylog show -- 2023/01/07`), or a more casual realtive reference, "tomorrow", "yesterday", "1 day ago", etc. +You can append a quick one-line entry without opening an editor with `-m`, `daylog -m "ate a burrito"` (this appends `- ate a burrito` to today's log). It accepts the usual date references too, `daylog -m "ate a burrito" -- yesterday`. + You can pipe updates as well, `echo "- ate a burrito" | daylog`. For other commands and options see, `daylog --help`. diff --git a/cmd/root.go b/cmd/root.go index 06fdab4..5d930bf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,6 +16,7 @@ import ( type Config struct { Project string + Message string } var ( @@ -35,6 +36,10 @@ var rootCmd = &cobra.Command{ `, Run: runCommand(func(cmd *cobra.Command, dl *daylog.DayLog) error { + if config.Message != "" { + return dl.Append(formatMessage(config.Message)) + } + piped, err := stdinIsPiped() if err != nil { return err @@ -71,6 +76,16 @@ func init() { // global flags rootCmd.PersistentFlags().StringVarP(&config.Project, "project", "p", "default", "The daylog project to use") rootCmd.PersistentFlags().Bool("prev", false, "Operate on the most recent log that isn't today's") + + rootCmd.Flags().StringVarP(&config.Message, "message", "m", "", "Append a one-line entry to the log instead of opening an editor") +} + +func formatMessage(msg string) string { + msg = strings.TrimSpace(msg) + if strings.HasPrefix(msg, "- ") { + return msg + } + return "- " + msg } func runCommand(fn func(cmd *cobra.Command, dl *daylog.DayLog) error) func(cmd *cobra.Command, args []string) { diff --git a/cmd/root_test.go b/cmd/root_test.go index a8e4391..0707d58 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -44,3 +44,36 @@ func TestFormatStdinContent(t *testing.T) { }) } } + +func TestFormatMessage(t *testing.T) { + tests := []struct { + name string + input string + expected string + }{ + { + name: "plain message gets a list marker", + input: "ate a burrito", + expected: "- ate a burrito", + }, + { + name: "surrounding whitespace is trimmed", + input: " ate a burrito ", + expected: "- ate a burrito", + }, + { + name: "existing list marker is preserved", + input: "- ate a burrito", + expected: "- ate a burrito", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := formatMessage(tt.input) + if result != tt.expected { + t.Errorf("formatMessage(%q) = %q, want %q", tt.input, result, tt.expected) + } + }) + } +}