desknudge

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 30 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package desknudge is the desk nudge: the clock that counts toward the interval, the files and locks it keeps on this desk, the panel it builds from the service's desk panel view, and the picks that complete a row.

The service computes the interval and the answer counts from every answer, and every rule behind the panel's rows. This package holds only the desk's own rules: the clock, kept blind to input, the picks, the files and the locks.

Index

Constants

View Source
const (
	StepToday   = "today"
	StepMinutes = "minutes"
	StepDone    = "done"
)

The steps a panel document holds.

View Source
const (
	KindNote          = "note"
	KindAnswer        = "answer"
	KindLogMinutes    = "log_minutes"
	KindAdjustMinutes = "adjust_minutes"
	// KindTick is a habit's checklist item, whose key records one set and
	// whose untick_key takes the latest set back.
	KindTick = "tick"
)

The kinds a row of the panel carries beyond what a completion completes.

View Source
const (
	KindPursuit = "pursuit"
	KindHabit   = "habit"
)

The completion kinds that the watch completes by an owner of their own. The view's completion_kind definition holds the whole set, and every other kind is an entry of a review register.

View Source
const (
	ActionComplete = "complete"
	ActionTick     = "tick"
	ActionUntick   = "untick"
	ActionCancel   = "cancel"
)

What a pick does, as its picked line names it. A cancel is a pending pick taken back.

View Source
const (
	PickPending    = "pending"
	PickCommitting = "committing"
)

The states of a pending pick before it ends. Once it ends, its state is its outcome.

View Source
const (
	PickDone    = 0
	PickFailed  = 1
	PickRefused = 2
)

The exit codes of a pick, which `doit desk-nudge panel pick` exits with.

View Source
const (
	OutcomeDone      = "done"
	OutcomeSkipped   = "skipped"
	OutcomeRefused   = "refused"
	OutcomeUncertain = "uncertain"
	OutcomeCanceled  = "canceled"
)

The outcomes of a pick. A refused pick recorded nothing, so its row is free again. An uncertain one may have landed, so its row is held until the next read. A canceled one is a pending pick taken back before its commit.

View Source
const (
	Unreachable   = "unreachable"
	LoginRefused  = "login"
	NothingToDraw = "nothing_to_draw"
	Unreadable    = "unreadable"
)

Why a firing has no suggestion, each with what the panel says instead.

View Source
const PanelSchemaVersion = 3

PanelSchemaVersion is the panel's shape, which schemas/desk-nudge-panel.schema.json describes.

View Source
const RecordSchemaVersion = 4

RecordSchemaVersion is the shape of a line in desk-nudge-events.jsonl, which schemas/desk-nudge-record.schema.json describes.

View Source
const StateVersion = 2

StateVersion is the state file's shape. It stays 2 with the Python watch's field names, so either watch reads the other's clock: an older doit's Python watch after a revert, and this watch after an upgrade.

View Source
const StatusSchemaVersion = 3

StatusSchemaVersion is the shape of `doit desk-nudge status --json`, which schemas/desk-nudge-status.schema.json describes.

Variables

View Source
var ErrAnotherWatch = errors.New("another watch is running on this desk, so this one exits")

ErrAnotherWatch is a watch already running on this desk.

View Source
var ErrHeldElsewhere = errors.New("held by another process")

ErrHeldElsewhere is a lock that another process holds.

View Source
var ErrNoPendingPick = errors.New("no pick")

ErrNoPendingPick is an id that this desk holds no pick by now: it never held one, or the pick ended and a later read let it go.

View Source
var ErrNoWatch = errors.New("no desk nudge watch is running")

ErrNoWatch is no watch answering on the socket.

View Source
var ErrNothingToAnswer = errors.New("nothing to answer")

ErrNothingToAnswer is no nudge to answer: none is ready, or the id names none that this desk fired lately.

View Source
var ErrUnreadableState = errors.New("unreadable state")

ErrUnreadableState is a state file that is not version 2's: not JSON, not an object, or another version.

View Source
var ErrWatchServesNoPicks = errors.New("a watch holds this desk and answers no pick")

ErrWatchServesNoPicks is a watch that holds this desk and answers nothing on the socket, such as an older doit's Python watch still running through an upgrade.

Functions

func Answers

func Answers() []string

Answers are the answers a nudge takes.

func IsAnswer

func IsAnswer(value string) bool

IsAnswer is whether value is an answer a nudge takes.

func SetLogger

func SetLogger(handler slog.Handler)

SetLogger points the desk nudge's log at handler.

func StderrHandler

func StderrHandler() slog.Handler

StderrHandler logs to stderr, with journald's priorities where stderr is the journal stream that $JOURNAL_STREAM names.

Types

type AnswerRow

type AnswerRow struct {
	NudgeID    string `json:"nudge_id"`
	Answer     string `json:"answer"`
	FiredAt    string `json:"fired_at"`
	AnsweredAt string `json:"answered_at"`
	Machine    string `json:"machine"`
}

AnswerRow is one answer as the service stores it.

type Answered

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

Answered is what became of one answer: taken on the desk, then stored by the service or not.

func (Answered) Outcome

func (a Answered) Outcome() (int, string)

Outcome is the exit code and the sentence for one answer, which `doit desk-nudge answer` and a panel pick both give.

type Change

type Change struct {
	Records []Record
	// Closes is the nudge this change ended.
	Closes *Nudge
	// Fire is present time reaching the interval, so a firing begins.
	Fire bool
}

Change is what one change to the state asks of the effects around it.

type Completes

type Completes struct {
	Kind   string  `json:"kind"`
	Ref    string  `json:"ref"`
	ItemID *string `json:"item_id"`
}

Completes is what picking an entry completes, as the view gives it.

type Desk

type Desk struct {
	Paths Paths
	Now   func() time.Time
	// Open is the service this desk's login names, opened afresh for each
	// request so a new login is taken at once.
	Open    func(context.Context) (*service.Client, error)
	Machine string
	Actor   string
	// Version is the doit release this binary is.
	Version string
}

Desk is this desk: its files, its clock, the service it asks, and who it is.

func ThisDesk

func ThisDesk(version string) *Desk

ThisDesk is the desk this process runs on.

func (*Desk) Answer

func (d *Desk) Answer(ctx context.Context, answer, nudgeID string) (Answered, error)

Answer takes one answer, ends the nudge it answers, and stores it, keeping it to send again where a later send could store it.

It is the one path for every answer to a nudge. With no id it answers the ready nudge. It answers ErrNothingToAnswer where none is ready and the id names none that this desk fired. An answer already kept on this desk is sent as it was kept, because that row is the one the service stores. One saved line is written per answer, by the writer that drops it from the queue or whose send the service added.

func (*Desk) LabelOf

func (d *Desk) LabelOf(answer string) string

LabelOf is an answer's words, as the view last read named them.

func (*Desk) Pause

func (d *Desk) Pause() (bool, error)

Pause holds the clock where it stands, ending a nudge in hand, and reports whether it was running.

func (*Desk) Pick

func (d *Desk) Pick(ctx context.Context, key string, minutes *int, read func()) (Panel, error)

Pick acts on one panel row by its key, and answers the next step: a done step once something is completed, or a timed pursuit's minutes. minutes logs a timed pursuit at once. read is called once a completion went through, so the parts are read again.

func (*Desk) Resume

func (d *Desk) Resume() (bool, error)

Resume carries on the count from where Pause held it, and reports whether it was paused.

func (*Desk) Show

func (d *Desk) Show() Panel

Show is today's step, read from the panel the watch keeps and the state file, with no request.

func (*Desk) Status

func (d *Desk) Status() Status

Status reads the clock from the state file, under no lock and with no request.

type EndedPick added in v0.4.0

type EndedPick struct {
	ID       string `json:"id"`
	Key      string `json:"key"`
	Text     string `json:"text"`
	Action   string `json:"action"`
	Minutes  *int   `json:"minutes"`
	CommitAt string `json:"commit_at"`
	Outcome  string `json:"outcome"`
	Message  string `json:"message"`
	EndedAt  string `json:"ended_at"`
}

EndedPick is how a pending pick came out, which the panel shows on its row. A done or uncertain one shows until a view read begins after it ended. A refused or skipped one shows until its key is picked again, or a view read begins on a later day. A canceled one is never drawn. It is kept until a read, so a second DELETE of it answers it again.

type HeldPick

type HeldPick struct {
	Key string `json:"key"`
	At  string `json:"at"`
}

HeldPick is a key picked, held until the parts are read again.

type Interval

type Interval struct {
	IntervalMinutes float64        `json:"interval_minutes"`
	AnswerCounts    map[string]int `json:"answer_counts"`
}

Interval is the service's interval and answer counts, as GET /desk-nudge and an answer's reply carry them.

type MinutesStep

type MinutesStep struct {
	Key  string `json:"key"`
	Text string `json:"text"`
}

MinutesStep is the timed pursuit that a minutes step logs. The step's rows log an amount, or reopen the step one adjustment above or below it.

type Nudge

type Nudge struct {
	NudgeID         string      `json:"nudge_id"`
	FiredAt         string      `json:"fired_at"`
	CountedMinutes  float64     `json:"counted_minutes"`
	IntervalMinutes float64     `json:"interval_minutes"`
	Suggestion      *Suggestion `json:"suggestion"`
	ReadyUntil      *string     `json:"ready_until"`
}

Nudge is the nudge in hand, from its firing until an answer, a pick or its expiry ends it.

type NudgeAnswer

type NudgeAnswer struct {
	Answer string `json:"answer"`
	Label  string `json:"label"`
	Key    string `json:"key"`
}

NudgeAnswer is one answer to the ready nudge, and the key that picks it.

type Panel

type Panel struct {
	SchemaVersion int     `json:"schema_version"`
	Step          string  `json:"step"`
	Message       *string `json:"message"`
	// ReadAt is when the watch began the read that the view comes from, on this desk's clock.
	ReadAt *string `json:"read_at"`
	// GeneratedAt is when the service computed the view.
	GeneratedAt *string      `json:"generated_at"`
	StaleSince  *string      `json:"stale_since"`
	StaleReason *StaleReason `json:"stale_reason"`
	// View is the service's desk panel view whole, with its own schema_version, or null before the first read.
	View json.RawMessage `json:"view"`
	// Rows are what the step draws, in order, so no reader holds the rule that lists them.
	Rows    []Row        `json:"rows"`
	Nudge   *ReadyNudge  `json:"nudge"`
	Picked  []HeldPick   `json:"picked"`
	Minutes *MinutesStep `json:"minutes"`
	// Picks are the pending picks, and how each one that ended came out, while the panel shows it.
	Picks []ShownPick `json:"picks"`
}

Panel is one step of the desk nudge's panel: the view as the watch last read it, what only this desk knows, and the rows the step draws. The watch keeps the today step in desk-nudge-panel.json, `panel show --json` prints it, and `panel pick --json` prints the step a pick leads to.

type PanelState

type PanelState struct {
	Picked []HeldPick  `json:"picked"`
	Ended  []EndedPick `json:"ended"`
}

PanelState holds each panel key picked, with when its completion ended. A read of the panel that begins after that lets it go. Ended holds how each pending pick ended, which the panel shows on its row.

type Paths

type Paths struct {
	// State is the clock, which the waybar pill reads.
	State string
	// StateLock is taken around each change to State.
	StateLock string
	// WatchLock is held for a watch's whole life, so a second one refuses to start.
	WatchLock string
	// Records is one line per moment the nudge records, appended and never rotated, for a log shipper.
	Records string
	// Panel is the panel as the watch last built it, which panel show reads with no watch.
	Panel string
	// Socket is where the watch answers the CLI.
	Socket string
}

Paths are the files the desk nudge keeps on this desk.

func DeskPaths

func DeskPaths() Paths

DeskPaths are this desk's, under $XDG_STATE_HOME, $XDG_CACHE_HOME and the runtime directory.

func (Paths) AskPick

func (p Paths) AskPick(ctx context.Context, key string, minutes *int) (Panel, error)

AskPick hands a pick to the watch, and answers the step it leads to, a *PickError where the pick did nothing or did not go through, or ErrNoWatch. Where nothing answers and another process holds the watch lock, it is ErrWatchServesNoPicks.

func (Paths) HoldWatchLock

func (p Paths) HoldWatchLock() (*os.File, error)

HoldWatchLock holds the watch lock until the returned file closes, or answers ErrHeldElsewhere where another watch holds it.

type PendingPick added in v0.4.0

type PendingPick struct {
	ID        string    `json:"id"`
	Key       string    `json:"key"`
	Text      string    `json:"text"`
	Action    string    `json:"action"`
	Completes Completes `json:"completes"`
	Minutes   *int      `json:"minutes"`
	PickedAt  string    `json:"picked_at"`
	CommitAt  string    `json:"commit_at"`
	// StartedAt is when its commit began. A restart reads it to tell what may already have run.
	StartedAt *string `json:"started_at"`
	// Log is a pursuit's journal entry once its on_log is about to run, which a restart sends again under the same id.
	Log map[string]any `json:"log,omitempty"`
}

PendingPick is one click waiting for its commit_at, or committing.

type Phase

type Phase string

Phase is where the clock stands. Only counting adds time.

const (
	Counting Phase = "counting"
	Firing   Phase = "firing"
	Ready    Phase = "ready"
	Paused   Phase = "paused"
	NoDesk   Phase = "no_desk"
)

type PickError

type PickError struct {
	ExitCode int    `json:"exit_code"`
	Message  string `json:"message"`
}

PickError is a pick that did nothing or did not go through. Its notification is already shown, and its picked line written.

func (*PickError) Error

func (p *PickError) Error() string

type Pill

type Pill struct {
	Text       string  `json:"text"`
	Class      Phase   `json:"class"`
	StaleAfter *string `json:"stale_after"`
}

Pill is what the waybar pill reads from the state file.

type QueuedAnswer

type QueuedAnswer struct {
	Row  AnswerRow `json:"row"`
	Late bool      `json:"late"`
}

QueuedAnswer is an answer the service did not store, which the watch sends again.

type ReadyNudge

type ReadyNudge struct {
	NudgeID    string        `json:"nudge_id"`
	FiredAt    string        `json:"fired_at"`
	ReadyUntil *string       `json:"ready_until"`
	Answers    []NudgeAnswer `json:"answers"`
}

ReadyNudge is the nudge that is ready, with the key that answers it for each answer.

type RecentNudge

type RecentNudge struct {
	NudgeID string `json:"nudge_id"`
	FiredAt string `json:"fired_at"`
}

RecentNudge is a nudge that ended lately, which a late answer can still name.

type Record

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

Record is one line of the records file. Its fields keep the order they were given, so ts, event and the schema version lead every line.

func (Record) Event

func (r Record) Event() string

Event is the line's event.

func (Record) MarshalJSON

func (r Record) MarshalJSON() ([]byte, error)

type Row

type Row struct {
	Key       *string `json:"key"`
	Kind      string  `json:"kind"`
	Text      string  `json:"text"`
	Group     *string `json:"group"`
	Minutes   *int    `json:"minutes"`
	Suggested bool    `json:"suggested"`
	// Ticks and Target are a checklist item's sets today, as the view was read, and its daily target.
	Ticks  *int `json:"ticks"`
	Target *int `json:"target"`
	// UntickKey takes a checklist item's latest set back.
	UntickKey *string `json:"untick_key"`
	// Picks are the pending picks of the row's keys, and how each one that ended came out.
	Picks []ShownPick `json:"picks"`
}

Row is one line of a step as a terminal or a board draws it.

type ShownPick added in v0.4.0

type ShownPick struct {
	ID       string  `json:"id"`
	Key      string  `json:"key"`
	Text     string  `json:"text"`
	Action   string  `json:"action"`
	State    string  `json:"state"`
	Minutes  *int    `json:"minutes"`
	CommitAt string  `json:"commit_at"`
	Message  *string `json:"message"`
	EndedAt  *string `json:"ended_at"`
}

ShownPick is a pending pick as the panel, the status and the socket show it. Its state is pending, committing, or the outcome it ended with.

type StaleReason

type StaleReason struct {
	Error  string `json:"error"`
	Detail string `json:"detail"`
}

StaleReason is why the latest read of the view failed.

type State

type State struct {
	Version         int            `json:"version"`
	Phase           Phase          `json:"phase"`
	CountedSeconds  float64        `json:"counted_seconds"`
	LastTick        *string        `json:"last_tick"`
	IntervalMinutes float64        `json:"interval_minutes"`
	AnswerCounts    map[string]int `json:"answer_counts"`
	Nudge           *Nudge         `json:"nudge"`
	QueuedAnswers   []QueuedAnswer `json:"queued_answers"`
	RecentNudges    []RecentNudge  `json:"recent_nudges"`
	Panel           PanelState     `json:"panel"`
	Pill            Pill           `json:"pill"`
	// PendingPicks are the page's clicks that have not ended, in click order.
	PendingPicks []PendingPick `json:"pending_picks"`
}

State is the state file, $XDG_STATE_HOME/doit/desk-nudge.json.

type Status

type Status struct {
	SchemaVersion     int            `json:"schema_version"`
	Phase             Phase          `json:"phase"`
	WatchRunning      bool           `json:"watch_running"`
	CountedMinutes    float64        `json:"counted_minutes"`
	IntervalMinutes   float64        `json:"interval_minutes"`
	MinutesLeft       *int           `json:"minutes_left"`
	NudgeID           *string        `json:"nudge_id"`
	FiredAt           *string        `json:"fired_at"`
	ReadyUntil        *string        `json:"ready_until"`
	Suggestion        *Suggestion    `json:"suggestion"`
	AnswerCounts      map[string]int `json:"answer_counts"`
	QueuedAnswerCount int            `json:"queued_answer_count"`
	Pill              Pill           `json:"pill"`
	// PendingPicks are the clicks waiting for their commit_at, or committing.
	PendingPicks []ShownPick `json:"pending_picks"`
}

Status is where the clock stands, read from this desk's state file with no request.

func (Status) Print

func (s Status) Print(out io.Writer, style func(text, color string) string, label func(answer string) string)

Print writes the status as a terminal reads it.

type Suggestion

type Suggestion struct {
	Pursuit string
	ItemID  *string
	Label   string
	Error   string
	Message string
	Detail  string
}

Suggestion is what a firing suggested: the pursuit the draw offered first, or why none was drawn. Each marshals as its own shape, which the record and status schemas describe.

func (Suggestion) MarshalJSON

func (s Suggestion) MarshalJSON() ([]byte, error)

func (*Suggestion) UnmarshalJSON

func (s *Suggestion) UnmarshalJSON(data []byte) error

type View

type View struct {
	SchemaVersion    int             `json:"schema_version"`
	GeneratedAt      string          `json:"generated_at"`
	Suggestion       json.RawMessage `json:"suggestion"`
	SuggestionReason *string         `json:"suggestion_reason"`
	Answers          json.RawMessage `json:"answers"`
	Totals           json.RawMessage `json:"totals"`
	Pursuits         json.RawMessage `json:"pursuits"`
	Habits           json.RawMessage `json:"habits"`
	Lanes            json.RawMessage `json:"lanes"`
	Problems         json.RawMessage `json:"problems"`
	// contains filtered or unexported fields
}

View is the service's desk panel view, as schemas/desk-panel-view.schema.json describes it. It names the parts the watch reads, and keeps the document as the service answered it, so a part added to the service's answer reaches the panel with no change here.

func (*View) UnmarshalJSON

func (v *View) UnmarshalJSON(text []byte) error

UnmarshalJSON reads the parts and keeps the document whole.

type ViewVersionError

type ViewVersionError struct{ Version int }

ViewVersionError is a desk panel view at a version that this doit does not read.

func (*ViewVersionError) Error

func (e *ViewVersionError) Error() string

type Watch

type Watch struct {

	// Executable is the binary whose replacement ends the watch, so the unit starts the new one.
	Executable string
	// contains filtered or unexported fields
}

Watch counts the time since the last nudge ended and fires the nudge at the interval. Each request and program runs off the tick, in a goroutine of its own.

func NewWatch

func NewWatch(desk *Desk) *Watch

NewWatch is a watch over desk.

func (*Watch) CancelPending added in v0.4.0

func (w *Watch) CancelPending(id string) (ShownPick, error)

CancelPending takes back the pending pick id before it commits. A pick already taken back answers canceled again, so a resent DELETE reads as the first one did.

func (*Watch) CreatePending added in v0.4.0

func (w *Watch) CreatePending(key string, minutes *int) (ShownPick, error)

CreatePending takes one click on the page: a pending pick that commits pendingFor from now. A click on a key whose pick still waits takes that pick back instead, and answers it canceled. The watch serializes the two, so a second click that arrives before the first one's answer still takes it back.

func (*Watch) Pick

func (w *Watch) Pick(ctx context.Context, key string, minutes *int) (Panel, error)

Pick runs one pick for the socket, and asks for the parts to be read again once it went through.

func (*Watch) Run

func (w *Watch) Run(ctx context.Context) error

Run holds the watch lock, serves the socket, and ticks until ctx ends or the binary is replaced. Either way it drains what is under way, then returns nil.

func (*Watch) Start

func (w *Watch) Start(ctx context.Context)

Start writes started, then finishes a nudge that a stopped watch left firing, and takes up the pending picks it left. A ready nudge stays on the pill, unchimed.

func (*Watch) Tick

func (w *Watch) Tick(ctx context.Context)

Tick counts, expires, and starts whatever the tick calls for off the loop.

func (*Watch) Wait

func (w *Watch) Wait()

Wait waits for every request and program under way.

Jump to

Keyboard shortcuts

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