tidy

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package tidy provides shared helpers for context file maintenance: task block parsing, empty section removal, string truncation, and archive writing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseTaskBlocks

func ParseTaskBlocks(lines []string) []entity.TaskBlock

ParseTaskBlocks parses content into task blocks, identifying completed tasks with their nested content.

A task block consists of:

  • A parent task line at the top level (no indentation, e.g., "- [x] Task title")
  • All following lines that are more indented than the parent

Only top-level tasks (indent=0) are considered for archiving. Nested subtasks are part of their parent block and are not collected as independent blocks.

A block is archivable if: - The parent task is checked [x] - No nested lines contain unchecked tasks [ ]

Parameters:

  • lines: Slice of lines from the tasks file

Returns:

  • []entity.TaskBlock: All completed top-level task blocks found (outside the Completed section)

func RemoveBlocksFromLines

func RemoveBlocksFromLines(lines []string, blocks []entity.TaskBlock) []string

RemoveBlocksFromLines removes the specified blocks from the lines slice.

Blocks must be sorted by StartIndex in ascending order.

Parameters:

  • lines: Original lines from the file
  • blocks: Task blocks to remove (must be sorted by StartIndex)

Returns:

  • []string: New lines with blocks removed

func RemoveEmptySections

func RemoveEmptySections(content string) (string, int)

RemoveEmptySections removes Markdown sections that contain no content.

A section is considered empty if it has a "## " header followed only by blank lines until the next section or end of the file.

Parameters:

  • content: Markdown content to process

Returns:

  • string: Content with empty sections removed
  • int: Number of sections removed

func TruncateString

func TruncateString(s string, maxLen int) string

TruncateString shortens a string to maxLen, adding "..." if truncated.

Parameters:

  • s: String to truncate
  • maxLen: Maximum length including the "..." suffix

Returns:

  • string: Original string if within limit, otherwise truncated with "..."

func WriteArchive

func WriteArchive(prefix, heading, content string) (string, error)

WriteArchive writes content to a dated archive file in .context/archive/.

Creates the archive directory if needed. If a file for today already exists, the new content is appended. Otherwise, a new file is created with a header.

Parameters:

  • prefix: File name prefix (e.g., "tasks", "decisions", "learnings")
  • heading: Markdown heading for new archive files (e.g., config.HeadingArchivedTasks)
  • content: The content to archive

Returns the path to the written archive file.

Types

type CompactResult

type CompactResult struct {
	// TasksMoved lists the parent text of each task moved to Completed.
	TasksMoved []string
	// TasksSkipped lists parent text of completed tasks with pending children.
	TasksSkipped []string
	// TasksFileUpdate is non-nil when TASKS.md content changed.
	TasksFileUpdate *fileUpdate
	// ArchivableBlocks are blocks eligible for archival.
	ArchivableBlocks []entity.TaskBlock
	// SectionsCleaned lists files where empty sections were removed.
	SectionsCleaned []sectionClean
	// SectionFileUpdates holds new content for files with sections removed.
	SectionFileUpdates []fileUpdate
}

CompactResult holds the outcome of a CompactContext call.

Callers decide how to report results (CLI prints, MCP returns JSON-RPC responses) and how to write files (os.WriteFile, etc.).

func CompactContext

func CompactContext(ctx *entity.Context) *CompactResult

CompactContext analyzes a loaded context for compactable items.

This is pure logic with no I/O. It parses task blocks, moves completed tasks to the Completed section, and removes empty sections from all context files. Callers are responsible for writing the returned file updates to disk.

Parameters:

  • ctx: loaded context

Returns:

  • *compactResult: what changed and what to write

func (*CompactResult) TotalChanges

func (r *CompactResult) TotalChanges() int

TotalChanges returns the number of items compacted.

Jump to

Keyboard shortcuts

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