Module: Ace::Assign::Atoms::TreeFormatter

Defined in:
lib/ace/assign/atoms/tree_formatter.rb

Overview

Pure function: renders assignment hierarchy as an indented tree string.

Takes a flat list of AssignmentInfo objects with parent fields and renders them as a visual tree with Unicode box-drawing characters.

Examples:

TreeFormatter.format(assignments)
# =>  task-148-implement
#     +-- onboard (completed)
#     +-- work-on-task (running)
#     |   +-- onboard (completed)
#     |   +-- implement (in_progress)
#     |   \-- verify-tests (pending)
#     \-- review-pr (pending)

Constant Summary collapse

STATE_LABELS =

State display labels matching list command

{
  pending: "pending",
  in_progress: "in_progress",
  running: "running",
  paused: "paused",
  completed: "completed",
  failed: "failed",
  empty: "empty"
}.freeze

Class Method Summary collapse

Class Method Details

.format(assignments) ⇒ String

Format a flat list of assignment info objects as a tree.

Parameters:

Returns:

  • (String)

    Formatted tree string



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ace/assign/atoms/tree_formatter.rb', line 36

def self.format(assignments)
  return "No assignments found." if assignments.empty?

  # Build ID index first (pass 1), then attach children (pass 2)
  by_id = {}
  assignments.each { |info| by_id[info.id] = info }

  children_of = Hash.new { |h, k| h[k] = [] }
  assignments.each do |info|
    parent_id = extract_parent_id(info)
    if parent_id && by_id.key?(parent_id)
      children_of[parent_id] << info
    end
  end

  # Find roots: assignments whose parent is nil or not in the set
  roots = assignments.reject do |info|
    parent_id = extract_parent_id(info)
    parent_id && by_id.key?(parent_id)
  end

  lines = []
  roots.each do |root|
    render_node(root, children_of, lines, prefix: "", is_last: true, is_root: true)
  end

  lines.join("\n")
end