journal

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const SchemaVersion = 1

Variables

View Source
var (
	// ErrEmptyNote is returned when attempting to add an empty note
	ErrEmptyNote = errors.New("cannot add empty note")

	// ErrNoEntries is returned when no entries are found in operations requiring entries
	ErrNoEntries = errors.New("no entries found")

	// ErrInvalidEntry is returned when an entry has invalid data
	ErrInvalidEntry = errors.New("invalid entry")

	// ErrEntryNotFound is returned when a specific entry cannot be found
	ErrEntryNotFound = errors.New("entry not found")

	// ErrInvalidTimeFormat is returned when time parsing fails
	ErrInvalidTimeFormat = errors.New("invalid time format")

	// ErrInvalidBreak is returned when break operations fail
	ErrInvalidBreak = errors.New("invalid break operation")

	// ErrJournalIO is returned when journal file operations fail
	ErrJournalIO = errors.New("journal file operation failed")

	// ErrValidation is returned when validation fails
	ErrValidation = errors.New("validation failed")
)

Error types for better error categorization and handling

Functions

func BreakError added in v0.9.0

func BreakError(reason string) error

BreakError creates an error for break operations

func CalculateTotalTime

func CalculateTotalTime(entries []JournalEntry) (time.Duration, error)

CalculateTotalTime calculates the total time duration for a slice of JournalEntry. It iterates over each entry in the slice and checks if the end time of the entry is after the start time. If the end time is not after the start time, it returns an error indicating that the entry is invalid. Otherwise, it adds the difference between the end time and the start time to the total duration. After iterating over all entries, it returns the total duration and nil. If there are no entries in the slice, it returns a duration of 0 and nil.

func EmptyNoteError added in v0.9.0

func EmptyNoteError() error

EmptyNoteError creates an error for empty note operations

func EntryNotFoundError added in v0.9.0

func EntryNotFoundError(id string) error

EntryNotFoundError creates an error for missing entries

func FormatNoteWithTags added in v0.12.0

func FormatNoteWithTags(content string, tags []string) string

FormatNoteWithTags formats a note with its tags for display. If the note has no tags, returns just the content. If the note has tags, appends them in the format " #tag1 #tag2"

func InvalidEntryError added in v0.9.0

func InvalidEntryError(id string, reason string) error

InvalidEntryError creates an error for invalid entries

func JournalIOError added in v0.9.0

func JournalIOError(operation string, err error) error

JournalIOError creates an error for file operations

func NoEntriesError added in v0.9.0

func NoEntriesError(operation string) error

NoEntriesError creates an error for operations requiring entries

func ParseNoteTags added in v0.12.0

func ParseNoteTags(content string) (string, []string)

ParseNoteTags extracts hashtags from note content and returns cleaned content and tags. Tags are identified by the pattern #tagname and are removed from the content. Multiple consecutive spaces are collapsed to single spaces.

Example:

content: "Meeting completed #progress #team-sync"
returns: ("Meeting completed", ["progress", "team-sync"])

func SaveEntries

func SaveEntries(journalEntries []JournalEntry, filename string) error

SaveEntries encodes the given journal entries into JSON format and writes them to a file with the specified filename.

The function will return an error if the encoding process fails or if the file cannot be created or written to.

The function takes two parameters: - journalEntries: a slice of JournalEntry objects to be saved. - filename: a string representing the name of the file to write to.

Example:

entries := []JournalEntry{...}
err := SaveEntries(entries, "journal.json")
if err != nil {
    log.Fatal(err)
}

func TimeFormatError added in v0.9.0

func TimeFormatError(input string, err error) error

TimeFormatError creates an error for time parsing issues

func ValidateConfigDuration added in v0.9.0

func ValidateConfigDuration(durationStr string, fieldName string) (time.Duration, error)

ValidateConfigDuration validates and parses a duration string from config

func ValidateTimeFormat added in v0.9.0

func ValidateTimeFormat(timeStr string) (time.Time, error)

ValidateTimeFormat validates and parses a time string in HH:MM format

func ValidationError added in v0.9.0

func ValidationError(field string, reason string) error

ValidationError creates an error for validation failures

Types

type Break added in v0.5.0

type Break struct {
	StartTime time.Time `json:"start_time"`
	EndTime   time.Time `json:"end_time"`
	Reason    string    `json:"reason"`
}

func (*Break) Duration added in v0.7.0

func (b *Break) Duration() time.Duration

type Journal added in v0.5.0

type Journal struct {
	Version int            `json:"version"` // schema version for the loaded journal
	Entries []JournalEntry `json:"entries"` // journal entries
}

type JournalEntry

type JournalEntry struct {
	ID        string    `json:"id"`
	StartTime time.Time `json:"start_time"`
	EndTime   time.Time `json:"end_time"`
	Notes     []Note    `json:"notes,omitempty"`
	Breaks    []Break   `json:"breaks,omitempty"`
}

func FetchEntriesByMonthDate

func FetchEntriesByMonthDate(journalEntries []JournalEntry, filterDate time.Time) ([]JournalEntry, error)

FetchEntriesByMonthDate filters a slice of JournalEntry objects and returns a new slice containing only the entries from the given date's month. The function uses the Year() and Month() functions from time standard library

The function takes a slice of JournalEntry objects and iterates over each entry. It checks the start time of each entry and compares it with the current month. If the entry belongs to the current month, it is added to the new slice.

If no entries are passed, or no entries belong to the current month, the function returns an error along with an empty slice.

Example:

entries := []JournalEntry{...}
currentMonthEntries, err := FetchEntriesByMonthDate(entries)
if err != nil {
    log.Fatal(err)
}
fmt.Println(currentMonthEntries) // Prints the entries for the current week

func FetchEntriesByWeekDate

func FetchEntriesByWeekDate(journalEntries []JournalEntry, currentDate time.Time) ([]JournalEntry, error)

FetchEntriesByWeekDate filters a slice of JournalEntry objects and returns a new slice containing only the entries from the given date's week. The function uses the ISO week date system, where weeks start on a Monday and the first week of the year is the one that includes at least four days of the new year.

The function takes a slice of JournalEntry objects and iterates over each entry. It checks the start time of each entry and compares it with the current week. If the entry belongs to the current week, it is added to the new slice.

If no entries are passed, or no entries belong to the current week, the function returns an error along with an empty slice.

Example:

entries := []JournalEntry{...}
currentWeekEntries, err := FetchEntriesByWeekDate(entries)
if err != nil {
    log.Fatal(err)
}
fmt.Println(currentWeekEntries) // Prints the entries for the current week

func FetchEntryByID

func FetchEntryByID(id string, entries []JournalEntry) (*JournalEntry, int)

FetchEntryByID searches for a JournalEntry in the provided slice of entries by its ID. It returns a pointer to the found JournalEntry and its index in the slice If the entry is not found, it returns nil and -1.

func FindCurrentDayEntry added in v0.9.0

func FindCurrentDayEntry(entries []JournalEntry) (*JournalEntry, int, error)

FindCurrentDayEntry finds the entry for the current day

func LoadEntries

func LoadEntries(filename string) ([]JournalEntry, error)

LoadEntries reads the JSON file with the given filename, unmarshals its contents into a slice of JournalEntry, and returns the slice. If the file does not exists, LoadEntries creates the file and returns an empty slice. If the file cannot be created, LoadEntries returns an error.

func NewJournalEntry

func NewJournalEntry() *JournalEntry

func (*JournalEntry) AddNote

func (j *JournalEntry) AddNote(note Note) error

func (*JournalEntry) EndDay

func (j *JournalEntry) EndDay()

func (*JournalEntry) String

func (j *JournalEntry) String() string

func (*JournalEntry) TotalWorkTime added in v0.7.0

func (j *JournalEntry) TotalWorkTime() time.Duration

type JournalError added in v0.9.0

type JournalError struct {
	Type    error
	Message string
	Context map[string]interface{}
	Err     error
}

JournalError represents a structured error with context

func (*JournalError) Error added in v0.9.0

func (e *JournalError) Error() string

func (*JournalError) Is added in v0.9.0

func (e *JournalError) Is(target error) bool

Is allows error comparison

func (*JournalError) Unwrap added in v0.9.0

func (e *JournalError) Unwrap() error

func (*JournalError) WithContext added in v0.9.0

func (e *JournalError) WithContext(key string, value interface{}) *JournalError

WithContext adds context to the error

type Note

type Note struct {
	Contents string   `json:"Contents"`       // Note contents
	Tags     []string `json:"Tags,omitempty"` // Tags for this particular note
}

func (*Note) ParseContent added in v0.12.0

func (n *Note) ParseContent()

ParseContent extracts hashtags from the note content and updates both the Contents and Tags fields. This allows users to write notes with inline tags like "Meeting done #progress #team" and have them automatically parsed into separate fields.

func (*Note) String

func (n *Note) String() string

type ValidationResult added in v0.9.0

type ValidationResult struct {
	IsValid bool
	Error   error
}

ValidationResult represents the result of a validation operation

func ValidateBreak added in v0.9.0

func ValidateBreak(br Break) ValidationResult

ValidateBreak validates a break entry

func ValidateEntry added in v0.9.0

func ValidateEntry(entry *JournalEntry) ValidationResult

ValidateEntry validates a journal entry

func ValidateNote added in v0.9.0

func ValidateNote(note Note) ValidationResult

ValidateNote validates a note's content and tags

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL