io

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package io provides clipboard, AI watcher, file watcher, and cron scheduler types.

Public types: ClipboardMonitor, AIComment, AIWatcher, FileEvent, WatcherConfig, FileWatcher, SingleFileWatcher, CronJob, CronScheduler, CronExpr, ClipboardBridge.

Public functions: NewClipboardMonitor, ReadClipboard, WriteClipboard, DetectContentType, DetectLanguage, NewAIWatcher, ScanFile, ScanDirectory, BuildPrompt, RemoveComment, NewFileWatcher, WatchSingle, DefaultIgnorePatterns, MatchesPattern, DedupEvents, FormatEvents, NewCronScheduler, ParseCron, IsDue, NextRunTime, SummarizeClipboard, FormatForContext.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildPrompt

func BuildPrompt(comment AIComment) string

BuildPrompt constructs a prompt for the AI agent based on the detected comment.

func DefaultIgnorePatterns

func DefaultIgnorePatterns() []string

DefaultIgnorePatterns returns standard patterns to ignore during watching.

func DetectContentType

func DetectContentType(content string) string

DetectContentType classifies clipboard content into a category. Returns one of: "code", "diff", "url", "path", "error", "text".

func DetectLanguage

func DetectLanguage(code string) string

DetectLanguage performs heuristic language detection from code content. Returns the detected language name or "" if unknown.

func FormatEvents

func FormatEvents(events []FileEvent) string

FormatEvents produces a human-readable summary of file events.

func FormatForContext

func FormatForContext(content string, contentType string) string

FormatForContext wraps content appropriately for injection into agent context based on its detected content type.

func IsDue

func IsDue(job *CronJob, now time.Time) bool

IsDue returns true if the given job should run at the specified time.

func MatchesPattern

func MatchesPattern(path string, patterns []string) bool

MatchesPattern checks if a path matches any of the given glob patterns.

func NextRunTime

func NextRunTime(expr *CronExpr, after time.Time) time.Time

NextRunTime calculates the next time after `after` that matches the cron expression.

func ReadClipboard

func ReadClipboard() (string, error)

ReadClipboard reads the current system clipboard content. It uses platform-specific commands:

  • macOS: pbpaste
  • Linux: xclip -selection clipboard -o (falls back to xsel)
  • Windows: powershell Get-Clipboard

func RemoveComment

func RemoveComment(file string, line int, marker string) error

RemoveComment removes the AI comment from the specified file at the given line. It matches the marker string to ensure the correct line is removed.

func SummarizeClipboard

func SummarizeClipboard(content string, maxChars int) string

SummarizeClipboard truncates large clipboard content for display, showing the first and last few lines with an omission indicator.

func WriteClipboard

func WriteClipboard(content string) error

WriteClipboard writes the given content to the system clipboard. It uses platform-specific commands:

  • macOS: pipe to pbcopy
  • Linux: pipe to xclip -selection clipboard (falls back to xsel)
  • Windows: pipe to powershell Set-Clipboard

Types

type AIComment

type AIComment struct {
	File     string // relative file path
	Line     int    // 1-based line number
	Comment  string // the instruction text after "ai:"
	Language string // detected language (go, python, js, etc.)
	Context  string // surrounding 10 lines of code (5 before, 5 after)
	Marker   string // the full comment line for removal after completion
}

AIComment represents a detected AI instruction comment in a source file.

func ScanDirectory

func ScanDirectory(dir string, patterns []string) []AIComment

ScanDirectory walks a directory tree and scans all files matching the given patterns for AI comments.

func ScanFile

func ScanFile(path string) []AIComment

ScanFile scans a single file for AI comments and returns all found. The path should be an absolute or relative path to the file.

type AIWatcher

type AIWatcher struct {
	RootDir   string
	Patterns  []string // file glob patterns to watch (e.g., "*.go", "*.py")
	Debounce  time.Duration
	OnComment func(comment AIComment)
	// contains filtered or unexported fields
}

AIWatcher monitors a directory tree for AI instruction comments and fires a callback when new ones are detected.

func NewAIWatcher

func NewAIWatcher(rootDir string, patterns []string) *AIWatcher

NewAIWatcher creates a new AIWatcher for the given directory and file patterns. If patterns is empty, defaults to common source file patterns.

func (*AIWatcher) Start

func (w *AIWatcher) Start(ctx context.Context) error

Start begins watching the directory for AI comments. It uses filesystem polling at the configured Debounce interval. It blocks until ctx is cancelled or Stop is called.

func (*AIWatcher) Stop

func (w *AIWatcher) Stop()

Stop signals the watcher to stop polling.

type ClipboardBridge

type ClipboardBridge struct{}

ClipboardBridge enables paste-from-browser workflows. Paste code/errors from browser, hawk processes them as context.

func (*ClipboardBridge) IsCode

func (cb *ClipboardBridge) IsCode(content string) bool

IsCode heuristically detects if clipboard content is code vs prose.

func (*ClipboardBridge) ReadClipboard

func (cb *ClipboardBridge) ReadClipboard() (string, error)

ReadClipboard returns the current clipboard content.

func (*ClipboardBridge) WriteClipboard

func (cb *ClipboardBridge) WriteClipboard(content string) error

WriteClipboard sets the clipboard content.

type ClipboardMonitor

type ClipboardMonitor struct {
	Enabled      bool
	PollInterval time.Duration
	OnPaste      func(content string)
	// contains filtered or unexported fields
}

ClipboardMonitor watches the system clipboard for changes and fires a callback when new content is detected. Inspired by Aider's copypaste feature.

func NewClipboardMonitor

func NewClipboardMonitor() *ClipboardMonitor

NewClipboardMonitor creates a new ClipboardMonitor with sensible defaults.

func (*ClipboardMonitor) Start

func (cm *ClipboardMonitor) Start(ctx context.Context) error

Start begins polling the clipboard at PollInterval. When content changes and passes validation (>20 chars, <50000 chars), the OnPaste callback is fired. The monitor runs until Stop() is called or the context is cancelled.

func (*ClipboardMonitor) Stop

func (cm *ClipboardMonitor) Stop()

Stop halts the clipboard polling loop.

type CronExpr

type CronExpr struct {
	Minute     []int
	Hour       []int
	DayOfMonth []int
	Month      []int
	DayOfWeek  []int
}

CronExpr holds parsed cron expression fields as integer slices.

func ParseCron

func ParseCron(expression string) (*CronExpr, error)

ParseCron parses a standard 5-field cron expression. Fields: minute hour day-of-month month day-of-week Supports: * (any), */N (step), N-M (range), N,M,O (list)

type CronJob

type CronJob struct {
	ID         string
	Name       string
	Schedule   string
	Command    string
	Enabled    bool
	LastRun    *time.Time
	NextRun    *time.Time
	RunCount   int
	LastResult string
	LastError  string
	CreatedAt  time.Time
}

CronJob represents a scheduled recurring task.

type CronScheduler

type CronScheduler struct {
	Jobs    map[string]*CronJob
	Running bool
	// contains filtered or unexported fields
}

CronScheduler manages scheduled cron jobs and executes them when due.

func NewCronScheduler

func NewCronScheduler() *CronScheduler

NewCronScheduler creates an initialized CronScheduler.

func (*CronScheduler) AddJob

func (cs *CronScheduler) AddJob(name, schedule, command string) (*CronJob, error)

AddJob parses the schedule expression and registers a new job.

func (*CronScheduler) FormatJobs

func (cs *CronScheduler) FormatJobs() string

FormatJobs returns a human-readable representation of all scheduled jobs.

func (*CronScheduler) ListJobs

func (cs *CronScheduler) ListJobs() []*CronJob

ListJobs returns all jobs as a slice.

func (*CronScheduler) PauseJob

func (cs *CronScheduler) PauseJob(id string) error

PauseJob disables a job so it won't be executed.

func (*CronScheduler) RemoveJob

func (cs *CronScheduler) RemoveJob(id string) error

RemoveJob deletes a job by ID.

func (*CronScheduler) ResumeJob

func (cs *CronScheduler) ResumeJob(id string) error

ResumeJob re-enables a paused job and recalculates its next run time.

func (*CronScheduler) Start

func (cs *CronScheduler) Start(ctx context.Context, execFn func(string) (string, error))

Start launches a background goroutine that checks for due jobs every minute.

func (*CronScheduler) Stop

func (cs *CronScheduler) Stop()

Stop halts the scheduler.

type FileEvent

type FileEvent struct {
	Path string
	Type string // "create", "modify", "delete", "rename"
	Time time.Time
	Size int64
}

FileEvent represents a single file system change event.

func DedupEvents

func DedupEvents(events []FileEvent) []FileEvent

DedupEvents removes duplicate events for the same path within a batch, keeping only the latest event for each path.

type FileWatcher

type FileWatcher struct {
	RootDir        string
	Patterns       []string
	IgnorePatterns []string
	Debounce       time.Duration
	BatchWindow    time.Duration
	OnChange       func([]FileEvent)
	// contains filtered or unexported fields
}

FileWatcher monitors a directory tree for file changes using polling. It supports glob-based inclusion/exclusion patterns, debouncing of rapid changes, and batching of events within a configurable window.

func NewFileWatcher

func NewFileWatcher(rootDir string, config WatcherConfig) *FileWatcher

NewFileWatcher creates a FileWatcher with the given root directory and config. Defaults: Debounce 500ms, BatchWindow 100ms, PollInterval 1s.

func (*FileWatcher) ShouldIgnore

func (fw *FileWatcher) ShouldIgnore(path string) bool

ShouldIgnore checks whether a path matches any of the watcher's ignore patterns.

func (*FileWatcher) Start

func (fw *FileWatcher) Start(ctx context.Context) error

Start begins the polling-based file watcher. It walks the directory at each PollInterval, detects creates/modifies/deletes, batches events within BatchWindow, and debounces rapid changes (only fires OnChange after Debounce duration of quiet). It blocks until ctx is cancelled or Stop is called.

func (*FileWatcher) Stop

func (fw *FileWatcher) Stop()

Stop signals the watcher to stop polling.

type SingleFileWatcher

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

SingleFileWatcher provides a simpler API for watching a single file.

func WatchSingle

func WatchSingle(path string, onChange func()) *SingleFileWatcher

WatchSingle creates a SingleFileWatcher for monitoring a single file.

func (*SingleFileWatcher) Start

func (sw *SingleFileWatcher) Start(ctx context.Context) error

Start begins polling the single file for changes. Blocks until ctx is cancelled or Stop is called.

func (*SingleFileWatcher) Stop

func (sw *SingleFileWatcher) Stop()

Stop signals the single file watcher to stop.

type WatcherConfig

type WatcherConfig struct {
	Patterns       []string
	IgnorePatterns []string
	Debounce       time.Duration
	BatchWindow    time.Duration
	PollInterval   time.Duration
}

WatcherConfig holds configuration for a FileWatcher.

Jump to

Keyboard shortcuts

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