|
| 1 | +require "io/console" |
| 2 | + |
| 3 | +module Generators |
| 4 | + module Avo |
| 5 | + # A small keyboard-driven panel for `rails g avo:skills`. |
| 6 | + # |
| 7 | + # Two groups with different semantics: scope is a radio (this app OR the |
| 8 | + # machine), targets are checkboxes (any combination of scan directories). |
| 9 | + # Written against io/console from stdlib rather than a prompt gem, because |
| 10 | + # avo is a dependency of other people's apps and a TUI is not worth adding |
| 11 | + # one for. |
| 12 | + # |
| 13 | + # Falls back to defaults whenever there is no interactive terminal, so |
| 14 | + # `-q`, CI, and piped invocations behave predictably. Callers pass explicit |
| 15 | + # flags to skip the panel entirely. |
| 16 | + class SkillsInstallPanel |
| 17 | + SCOPES = [ |
| 18 | + {key: "local", label: "This app", hint: "committed, so the whole team gets it"}, |
| 19 | + {key: "global", label: "This machine", hint: "one install, every Avo project"} |
| 20 | + ].freeze |
| 21 | + |
| 22 | + TARGETS = [ |
| 23 | + {key: "claude", label: ".claude/skills", hint: "Claude Code"}, |
| 24 | + {key: "agents", label: ".agents/skills", hint: "Codex, Gemini CLI, Goose, Amp, OpenCode"}, |
| 25 | + {key: "cursor", label: ".cursor/skills", hint: "Cursor"} |
| 26 | + ].freeze |
| 27 | + |
| 28 | + Result = Struct.new(:scope, :targets, :cancelled) do |
| 29 | + def cancelled? = !!cancelled |
| 30 | + end |
| 31 | + |
| 32 | + def self.interactive? |
| 33 | + $stdin.tty? && $stdout.tty? |
| 34 | + end |
| 35 | + |
| 36 | + def initialize(input: $stdin, output: $stdout) |
| 37 | + @input = input |
| 38 | + @output = output |
| 39 | + @scope = 0 |
| 40 | + @targets = TARGETS.map { |t| [t[:key], true] }.to_h |
| 41 | + @cursor = 0 |
| 42 | + @rows_drawn = 0 |
| 43 | + end |
| 44 | + |
| 45 | + # Rows are the two scopes then the three targets, so one cursor walks |
| 46 | + # both groups and the whole panel is arrow-navigable. |
| 47 | + def rows |
| 48 | + SCOPES.length + TARGETS.length |
| 49 | + end |
| 50 | + |
| 51 | + def run |
| 52 | + render |
| 53 | + loop do |
| 54 | + case read_key |
| 55 | + when :up then move(-1) |
| 56 | + when :down then move(1) |
| 57 | + when :space then toggle |
| 58 | + when :enter then return finish |
| 59 | + when :cancel then return cancel |
| 60 | + end |
| 61 | + render |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + private |
| 66 | + |
| 67 | + attr_reader :input, :output |
| 68 | + |
| 69 | + def move(delta) |
| 70 | + @cursor = (@cursor + delta) % rows |
| 71 | + end |
| 72 | + |
| 73 | + def toggle |
| 74 | + if @cursor < SCOPES.length |
| 75 | + @scope = @cursor |
| 76 | + else |
| 77 | + key = TARGETS[@cursor - SCOPES.length][:key] |
| 78 | + # Refuse to empty the set: an install with no target writes nothing |
| 79 | + # and looks like a silent no-op. |
| 80 | + @targets[key] = !@targets[key] unless @targets[key] && selected_targets.length == 1 |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + def selected_targets |
| 85 | + TARGETS.map { |t| t[:key] }.select { |k| @targets[k] } |
| 86 | + end |
| 87 | + |
| 88 | + def finish |
| 89 | + clear |
| 90 | + Result.new(SCOPES[@scope][:key], selected_targets, false) |
| 91 | + end |
| 92 | + |
| 93 | + def cancel |
| 94 | + clear |
| 95 | + Result.new(nil, [], true) |
| 96 | + end |
| 97 | + |
| 98 | + def read_key |
| 99 | + char = input.getch |
| 100 | + case char |
| 101 | + when "\r", "\n" then :enter |
| 102 | + when " " then :space |
| 103 | + when "", "q", "\e" then escape_or_cancel(char) |
| 104 | + else :none |
| 105 | + end |
| 106 | + rescue Interrupt |
| 107 | + :cancel |
| 108 | + end |
| 109 | + |
| 110 | + # An arrow key arrives as ESC [ A. A bare ESC means cancel, so peek at |
| 111 | + # what follows before deciding. |
| 112 | + def escape_or_cancel(char) |
| 113 | + return :cancel unless char == "\e" |
| 114 | + return :cancel unless input.respond_to?(:read_nonblock) |
| 115 | + |
| 116 | + begin |
| 117 | + seq = input.read_nonblock(2) |
| 118 | + rescue IO::WaitReadable, EOFError, Errno::EAGAIN |
| 119 | + return :cancel |
| 120 | + end |
| 121 | + |
| 122 | + case seq |
| 123 | + when "[A" then :up |
| 124 | + when "[B" then :down |
| 125 | + else :none |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + def clear |
| 130 | + output.print("\e[#{@rows_drawn}A\e[J") if @rows_drawn.positive? |
| 131 | + @rows_drawn = 0 |
| 132 | + end |
| 133 | + |
| 134 | + def render |
| 135 | + clear |
| 136 | + lines = [] |
| 137 | + lines << "" |
| 138 | + lines << " Install the Avo skills loader" |
| 139 | + lines << "" |
| 140 | + lines << " #{dim("Where")}" |
| 141 | + SCOPES.each_with_index do |scope, i| |
| 142 | + lines << row(i, (@scope == i) ? "(•)" : "( )", scope[:label], scope[:hint]) |
| 143 | + end |
| 144 | + lines << "" |
| 145 | + lines << " #{dim("Which agents")}" |
| 146 | + TARGETS.each_with_index do |target, i| |
| 147 | + lines << row(SCOPES.length + i, @targets[target[:key]] ? "[x]" : "[ ]", target[:label], target[:hint]) |
| 148 | + end |
| 149 | + lines << "" |
| 150 | + lines << " #{dim("↑↓ move space select enter install q cancel")}" |
| 151 | + lines << "" |
| 152 | + |
| 153 | + output.print(lines.join("\n") + "\n") |
| 154 | + @rows_drawn = lines.length |
| 155 | + end |
| 156 | + |
| 157 | + def row(index, marker, label, hint) |
| 158 | + active = @cursor == index |
| 159 | + pointer = active ? "›" : " " |
| 160 | + text = "#{pointer} #{marker} #{label.ljust(16)} #{dim(hint)}" |
| 161 | + active ? " \e[1m#{text}\e[0m" : " #{text}" |
| 162 | + end |
| 163 | + |
| 164 | + def dim(text) |
| 165 | + "\e[2m#{text}\e[0m" |
| 166 | + end |
| 167 | + end |
| 168 | + end |
| 169 | +end |
0 commit comments