Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
15 changes: 15 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

type Config struct {
Project string
Message string
}

var (
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
33 changes: 33 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
Loading