habit

package module
v0.0.0-...-9ca1a20 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 9 Imported by: 0

README

Habit Tracker

A CLI tool written in Go that allows you to track the performance of your habits. Add good habits and see how long you can keep the daily streak going for them!

Usage

go install github.com/aculclasure/habit/cmd/habit@latest
  • Add a new habit:

    habit programming
    
    Congratulations on starting your new habit 'programming'! Don't forget to do it again.
    
  • Update an existing habit within 24 hours to continue your daily streak:

    habit programming
    
    Nice work: you've done the habit 'programming' for 5 days in a row now.
    

    If you break your daily streak, you'll start over for that habit:

    habit programming
    
    You last did the habit 'programming' 2 days ago, so you're starting a new streak today. Good luck!
    
  • Get a summary of all tracked habits:

    habit
    
    It's been 3 days since you did 'programming'. Stay positive and get back on it!
    You are currently on a 4-day streak for 'strength-training'. Keep it going!
    

Description

Full project description and instructions link.

Documentation

Overview

Package habit implements a habit tracker and accompanying CLI.

Index

Constants

This section is empty.

Variables

View Source
var Now = time.Now

Now provides a seam to allow the time.Now() function to be overriden for testing.

Functions

func Main

func Main() int

Main is the driver for the CLI. It reads command-line arguments and allows a new Habit to be added, an existing Habit to be updated, or a summary of all stored Habits to be printed. It returns an exit code where 0 means the command was successful and anything other than 0 means the command failed.

func OpenStore

func OpenStore(path string) (*store, error)

OpenStore opens the store file at the given path and returns a store initialized with the key-value data contained in the file. An error is returned if there is a problem opening the store file or decoding its data.

func WithOutput

func WithOutput(output io.Writer) option

WithOutput accepts an io.Writer and returns an option that wires the io.Writer to a Tracker.

func WithStore

func WithStore(store *store) option

WithStore accepts a store and returns an option that wires the store to a Tracker.

Types

type Habit

type Habit struct {
	// Name is the name of the habit.
	Name string
	// CurrentStreak is the number of days in a row this habit has
	// been performed.
	CurrentStreak int
	// LastDone is the timestamp when the habit was last done.
	LastDone time.Time
}

A Habit represents a habit that can be tracked.

type Tracker

type Tracker struct {
	// contains filtered or unexported fields
}

A Tracker provides habit-tracking and summarization logic.

func NewTracker

func NewTracker(opts ...option) (*Tracker, error)

NewTracker accepts an optional list of options and returns a Tracker initialized with these options. If no options are provided, the Tracker stores its data to a local file "habit.store" and writes to stdout. An error is returned if there is a problem opening the data store or if any of the opts returns an error.

func (Tracker) PrintSummary

func (t Tracker) PrintSummary()

PrintSummary writes a summary of tracked Habits to the given Tracker's output.

func (*Tracker) Track

func (t *Tracker) Track(hbtName string) error

Track adds a new Habit to the store or updates an already-existing Habit in the store. An error is returned if an update is attempted on a Habit with a timestamp in the future or if the store cannot be saved after adding/updating a Habit.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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