detect

package
v0.18.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 7 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 detect derives a session state from its terminal output alone: desktop-notification escapes (OSC 9, OSC 777;notify, OSC 99) and BEL mean the program wants the user, output means it is working, and silence for IdleAfter means it is idle. It reports transitions only and runs no timer while nothing can change.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

type Clock interface {
	Now() time.Time
	AfterFunc(d time.Duration, f func()) Timer
}

Clock abstracts time for tests.

type Config

type Config struct {
	IdleAfter time.Duration // default 3s
	// OnState receives each state change (wire.State*) and its source.
	OnState func(state, source string)
	// OnNotify receives every notification the program emitted.
	OnNotify func(Notification)
	Clock    Clock // default: real time
}

Config configures a Detector. Callbacks run synchronously, one at a time, with the detector locked: they must not call back into the detector.

type Detector

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

Detector is the output-derived state machine of one session. States: working, idle, waiting_input, exited.

  • Any output: idle → working. waiting_input is sticky until input is written to the program (Input): a program that asked for the user usually redraws its prompt afterwards, which is not work.
  • No output for IdleAfter while working → idle (one timer, re-armed lazily: it fires at most once per IdleAfter while output flows).
  • OSC 9 / 777;notify / 99, or BEL outside a string sequence → notification + waiting_input.
  • Exited → exited, final.

func New

func New(cfg Config) *Detector

New returns a detector in state working (the program just started) with the idle timer armed.

func (*Detector) Exited

func (d *Detector) Exited()

Exited moves to the final state exited.

func (*Detector) Feed

func (d *Detector) Feed(b []byte)

Feed processes a chunk of program output. Escape sequences may be split across chunks. A chunk of DEC private mode sets and resets only (`CSI ? Pm h/l`) changes nothing on screen and is not work: Oh My Pi on Windows re-enables bracketed paste every second while it waits.

func (*Detector) Input

func (d *Detector) Input()

Input records that input was written to the program, which ends a sticky waiting_input: the next output counts as work again.

func (*Detector) SetIdleAfter

func (d *Detector) SetIdleAfter(v time.Duration)

SetIdleAfter changes the quiet period (the daemon's idle_after_ms).

func (*Detector) State

func (d *Detector) State() (state, source string)

State returns the current state and its source.

func (*Detector) Stop

func (d *Detector) Stop()

Stop releases the timer without a transition.

type Notification

type Notification struct {
	Title string
	Body  string
	Bell  bool // a bare BEL rather than an OSC notification
}

Notification is one notification the program emitted.

type Timer

type Timer interface {
	Reset(d time.Duration) bool
	Stop() bool
}

Timer is the subset of *time.Timer the detector uses.

Jump to

Keyboard shortcuts

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