Class: Tasku::Output::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/tasku/output/terminal.rb

Constant Summary collapse

PRIORITY_HEX =
{
  "none"   => "#6B7280",
  "low"    => "#06B6D4",
  "medium" => "#EAB308",
  "high"   => "#EF4444",
  "urgent" => "#E879F9"
}.freeze
STATUS_HEX =
{
  "backlog"     => "#6B7280",
  "todo"        => "#3B82F6",
  "in_progress" => "#EAB308",
  "done"        => "#22C55E",
  "cancelled"   => "#EF4444",
  "archived"    => "#6B7280"
}.freeze
PRIORITY_STYLES =
{
  "none"   => { color: :dim,    symbol: " " },
  "low"    => { color: :cyan,   symbol: "" },
  "medium" => { color: :yellow, symbol: "" },
  "high"   => { color: :red,    symbol: "" },
  "urgent" => { color: :bright_magenta, symbol: "" }
}.freeze
STATUS_STYLES =
{
  "backlog"     => { color: :dim,    symbol: "" },
  "todo"        => { color: :blue,   symbol: "" },
  "in_progress" => { color: :yellow, symbol: "" },
  "done"        => { color: :green,  symbol: "" },
  "cancelled"   => { color: :red,    symbol: "" },
  "archived"    => { color: :dim,    symbol: "" }
}.freeze
COL_SEP =
"  "

Instance Method Summary collapse

Constructor Details

#initializeTerminal

Returns a new instance of Terminal.



44
45
46
47
# File 'lib/tasku/output/terminal.rb', line 44

def initialize
  @pastel = Pastel.new
  @term_width = [terminal_width, 80].min
end

Instance Method Details

#render_added(task) ⇒ Object



99
100
101
# File 'lib/tasku/output/terminal.rb', line 99

def render_added(task)
  puts @pastel.green("  ✓ Task ##{task.id} created") + "#{@pastel.bold(task.name)}"
end

#render_deleted(task) ⇒ Object



107
108
109
# File 'lib/tasku/output/terminal.rb', line 107

def render_deleted(task)
  puts @pastel.red("  ✗ Task ##{task.id} deleted") + "#{@pastel.bold(task.name)}"
end

#render_list(tasks, colour_map: {}, spacing: "compact", bar: {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tasku/output/terminal.rb', line 49

def render_list(tasks, colour_map: {}, spacing: "compact", bar: {})
  if tasks.empty?
    puts @pastel.yellow("  No tasks found.")
    return
  end

  rows = tasks.map { |t| build_columns(t, colour_map, bar) }
  col_widths = compute_widths(rows)
  total = col_widths.sum + (COL_SEP.length * (col_widths.length - 1))

  puts ""
  puts @pastel.dim("  #{"" * total}")
  rows.each do |cols|
    line = cols.each_with_index.map { |c, i| c.to_s.ljust(col_widths[i]) }.join(COL_SEP)
    puts "  #{line}"
    puts "" if spacing == "spacious"
  end
  puts @pastel.dim("  #{"" * total}")
  puts @pastel.dim("  #{tasks.length} task(s) found")
end

#render_show(task, colour_map: {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tasku/output/terminal.rb', line 70

def render_show(task, colour_map: {})
  puts ""
  puts @pastel.bold("  Task ##{task.id}")
  puts @pastel.dim("  #{"" * 60}")

  fields = [
    ["Name",       @pastel.bold(task.name)],
    ["Description", task.description ? @pastel.dim(task.description) : @pastel.dim("")],
    ["Project",    project_str(task.project, colour_map)],
    ["Category",   task.category || @pastel.dim("")],
    ["Priority",   priority_tag(task.priority)],
    ["Status",     status_tag(task.status)],
    ["Start Day",  task.start_day ? task.start_day.to_s : @pastel.dim("")],
    ["Due Day",    due_cell(task)],
    ["Code",       task.code || @pastel.dim("")],
    ["Model",      task.model_name || @pastel.dim("")],
    ["Tags",       task.tag_list.empty? ? @pastel.dim("") : task.tag_list.join(", ")],
    ["Est. Hours", task.estimated_hours ? task.estimated_hours.to_s : @pastel.dim("")],
    ["Created",    task.created_at&.strftime("%Y-%m-%d %H:%M") || @pastel.dim("")],
    ["Updated",    task.updated_at&.strftime("%Y-%m-%d %H:%M") || @pastel.dim("")]
  ]

  max_label = fields.map { |l, _| l.length }.max
  fields.each do |label, value|
    puts "  #{@pastel.dim(label.ljust(max_label))}  #{value}"
  end
  puts ""
end

#render_stats(tasks, colour_map: {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/tasku/output/terminal.rb', line 111

def render_stats(tasks, colour_map: {})
  puts ""
  puts @pastel.bold("  Task Statistics")
  puts @pastel.dim("  #{"" * 60}")

  total = tasks.length
  by_status = tasks.group_by(&:status).transform_values(&:length)
  by_priority = tasks.group_by(&:priority).transform_values(&:length)
  by_project = tasks.group_by(&:project).transform_values(&:length)
  overdue = tasks.count(&:overdue?)
  project_count = tasks.map(&:project).compact.uniq.length

  puts "  #{@pastel.dim("Total tasks:".ljust(20))} #{total}"
  puts "  #{@pastel.dim("Overdue:".ljust(20))} #{overdue.positive? ? @pastel.red(overdue.to_s) : @pastel.green("0")}"
  puts "  #{@pastel.dim("Projects:".ljust(20))} #{project_count}"
  puts ""

  puts "  #{@pastel.dim("By Status:")}"
  Tasku::Task::VALID_STATUSES.each do |s|
    count = by_status[s] || 0
    next if count.zero?

    puts "    #{status_tag(s)} #{@pastel.send(STATUS_STYLES.dig(s, :color) || :dim, count.to_s.rjust(3))}"
  end

  puts ""
  puts "  #{@pastel.dim("By Priority:")}"
  Tasku::Task::VALID_PRIORITIES.each do |p|
    count = by_priority[p] || 0
    next if count.zero?

    puts "    #{priority_tag(p)} #{@pastel.send(PRIORITY_STYLES.dig(p, :color) || :dim, count.to_s.rjust(3))}"
  end

  puts ""
  puts "  #{@pastel.dim("By Project:")}"
  by_project.sort_by { |_, count| -count }.each do |name, count|
    label = name || @pastel.dim("(none)")
    coloured_name = name && colour_map[name] ? hex_colour_str(name, colour_map[name]) : label
    padding = " " * [20 - strip_ansi(coloured_name).length, 0].max
    puts "    #{coloured_name}#{padding} #{@pastel.dim(count.to_s.rjust(3))}"
  end
  puts ""
end

#render_updated(task) ⇒ Object



103
104
105
# File 'lib/tasku/output/terminal.rb', line 103

def render_updated(task)
  puts @pastel.green("  ✓ Task ##{task.id} updated") + "#{@pastel.bold(task.name)}"
end