habit

package module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 14 Imported by: 0

README

Habit tracker

Go Reference Go Go Report Card GitHub go.mod Go version GitHub GitHub release (latest SemVer) Platforms

This is a Go program that helps with establishing new, and tracking existing habits.

Description

When we're trying to establish a new habit (studying, running, rowing for example), it can be difficult to maintain focus and motivation. Studies suggest that it takes many weeks of regularly performing a new habit before it becomes natural and automatic. So, if you can motivate yourself to do the new habit every day for a month or two, that'll get you well on the way.

One thing that can help is to track your performance of the habit. Suppose you decide that you're going to spend at least 15 minutes every day studying or writing Go programs or going for a morning jog. You could draw 30 boxes on a piece of paper, one for each of the next 30 days, and check off each box as you do that day's practice.

This simple idea can be surprisingly effective, because we don't like to break a streak. If you've successfully done the habit every day for 29 days, there's a strong incentive not to break that run of success. Life has a way of coming at you, and you might well need that extra motivation at some point. Not today, not tomorrow, but soon: probably just around the time the novelty wears off.

The project is a Go package and accompanying command-line tool called habit that will help users track and establish new habits, by reporting their current streak.

Using habit

For example, if you decide you want to build the habit of jogging every day, you might tell the habit tool about it like this:

habit jog

Good luck with your new habit 'jog'! Don't forget to do it again
tomorrow.

If you want to track multiple daily habits you tell the habit tool to track your new activity, for example:

habit study

Good luck with your new habit 'study'! Don't forget to do it again
tomorrow.

As the days go by, you might record each daily practice like this:

habit jog

Nice work: you've done the habit 'jog' for 18 days in a row now.
Keep it up!

If you happen to miss a couple of days, that's all right:

habit jog

You last did the habit 'jog' 3 days ago, so you're starting a new
streak today. Good luck!

If you just want to check how you're doing, you could run:

habit

You're currently on a 1-day streak for 'jog'. Stick to it!
You're currently on a 1-day streak for 'study'. Stick to it!

or, if you keep streeks not broken:

You're currently on a 4-day streak for 'jog'. Stick to it!
You're currently on a 17-day streak for 'study'. Stick to it!

Maybe the news won't be quite so good:

You're currently on a 1-day streak for 'hike'. Stick to it!
It's been 10 days since you did 'jog'. It's ok, life happens. Get back on that horse today!
It's been 17 days since you did 'study'. It's ok, life happens. Get back on that horse today!

Installation

Storing data

habit persists data in a file storage. If you want to configure habit where to locate the file store, export the ENV variable $XDG_DATA_HOME. If the env var is not exported habit will create file store in user's $HOME directory.

Using go install

go install github.com/qba73/habit/cmd@latest

Verify installation:

habit
You are not tracking any habit yet.

Start tracking a habit:

habit jog
Good luck with your new habit 'jog'. Don't forget to do it tomorrow.

Check tracked habits:

habit
You're currently on a 1-day streak for 'jog'. Stick to it!

Building from source

Clone this repository to your local machine:

git clone git@github.com:qba73/habit.git
cd habit

Build habit binary:

go build -o habit ./cmd/main.go

Run habit:

./habit

Development

Use following make targets for developemnt and testing:

$ make

Usage:
  help                      Show help message
  dox                       Run tests with gotestdox
  test                      Run tests
  vet                       Run go vet
  check                     Run staticcheck analyzer
  cover                     Run unit tests and generate test coverage report
  tidy                      Run go mod tidy

Credits

This is an educational Go project intended for students at the Bitfield Institute of Technology.

Documentation

Overview

Package habit is a Go library that provides building blocks for habit tracking applications.

Index

Constants

This section is empty.

Variables

View Source
var Now = time.Now

Functions

func Check

func Check(s Store) string

Check takes a store and reports about all tracked habits.

func DayDiff

func DayDiff(start, stop time.Time) int

DayDiff takes two time obj and returns time delta in days.

func Main

func Main() int

func Record added in v0.1.1

func Record(s Store, habitName string) (string, error)

Record takes store and habitName and records habit activity. It creates a new habit if habit with provided name does not exist.

func RoundDateToDay

func RoundDateToDay(t time.Time) time.Time

RoundDateToDay truncates time to 24h periods (days).

Types

type FileStore

type FileStore struct {
	Path string

	Data map[string]Habit
	// contains filtered or unexported fields
}

FileStore implements Store interface.

func NewFileStore

func NewFileStore(path string) (*FileStore, error)

NewFileStore takes a path and returns a file store. It returns an error if it can't access the file.

func (*FileStore) Add

func (f *FileStore) Add(habit Habit)

Add takes a habit and adds it to the store.

Add does not persis data in the store. After calling Add(), call Save() to persist data.

func (*FileStore) Get

func (f *FileStore) Get(habitName string) (Habit, bool)

Get takes name and returns the habit. If the habit does not exist in the store it returns false.

func (*FileStore) GetAll

func (f *FileStore) GetAll() []Habit

GetAll returns tracked habits sorted by name.

func (*FileStore) Log added in v0.1.1

func (f *FileStore) Log(habitName string) (string, error)

Log takes a string representing habit's name and logs the habit. If habit with given name does not exist, Log creates it and starts tracking.

func (*FileStore) Save

func (f *FileStore) Save() error

Save saves content of the store.

type Habit

type Habit struct {
	Name   string    `json:"name"`
	Date   time.Time `json:"date"`   // Date it's a date when habit activity was last recorded
	Streak int       `json:"streak"` // Streak represents number of consecutive days when habit was recorded.
}

Habit holds state of a tracked habit.

func New

func New(name string) (Habit, error)

New takes a name and returns a new habit. It returns an error if name is empty.

func (*Habit) Check

func (h *Habit) Check() (int, string)

Check verifies if the streak is broken.

Returned value represents number of days since the habit was logged last time.

func (*Habit) Record added in v0.1.1

func (h *Habit) Record() (int, string)

Record records activity to the existing streak or starts a new streak if the streak is broken. It returns streak length and a corresponding message.

func (*Habit) Start

func (h *Habit) Start() string

Start starts a new streak.

type Store

type Store interface {
	Log(name string) (string, error)
	GetAll() []Habit
	Save() error
}

Store is the interface that wraps methods for storing and retrieving habits.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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