| 1 | package Claude::Agent::CLI; | ||
| 2 | |||
| 3 | use 5.020; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use utf8; | ||
| 7 | use open ':std', ':encoding(UTF-8)'; | ||
| 8 | |||
| 9 | use Term::ANSIColor qw(:constants colored); | ||
| 10 | use Term::ReadLine; | ||
| 11 | use Term::Choose qw(choose); | ||
| 12 | use Term::ProgressSpinner; | ||
| 13 | use Exporter 'import'; | ||
| 14 | |||
| 15 | our @EXPORT_OK = qw( | ||
| 16 | with_spinner start_spinner stop_spinner | ||
| 17 | prompt ask_yn menu select_option choose_from choose_multiple | ||
| 18 | header divider status | ||
| 19 | clear_line move_up | ||
| 20 | ); | ||
| 21 | |||
| 22 | our %EXPORT_TAGS = ( | ||
| 23 | all => \@EXPORT_OK, | ||
| 24 | spinner => [qw(with_spinner start_spinner stop_spinner)], | ||
| 25 | prompt => [qw(prompt ask_yn menu select_option choose_from choose_multiple)], | ||
| 26 | display => [qw(header divider status)], | ||
| 27 | term => [qw(clear_line move_up)], | ||
| 28 | ); | ||
| 29 | |||
| 30 | my $_term; | ||
| 31 | |||
| 32 | sub _term { | ||
| 33 | $_term //= Term::ReadLine->new('Claude::Agent'); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub with_spinner { | ||
| 37 | my ($message, $code) = @_; | ||
| 38 | |||
| 39 | my $ps = Term::ProgressSpinner->new(); | ||
| 40 | $ps->message("{spinner} $message"); | ||
| 41 | $ps->start(100); | ||
| 42 | |||
| 43 | my $result = $code->(); | ||
| 44 | |||
| 45 | $ps->finish(); | ||
| 46 | status('success', $message); | ||
| 47 | |||
| 48 | return $result; | ||
| 49 | } | ||
| 50 | |||
| 51 | my $_active_spinner; | ||
| 52 | |||
| 53 | sub start_spinner { | ||
| 54 | my ($message, $loop, %opts) = @_; | ||
| 55 | |||
| 56 | my $interval = delete $opts{interval} // 0.1; | ||
| 57 | |||
| 58 | my %spinner_opts = ( | ||
| 59 | spinner => $opts{spinner} // 'dots', | ||
| 60 | spinner_color => $opts{spinner_color} // 'cyan', | ||
| 61 | message => $opts{message} // "{spinner} $message", | ||
| 62 | ); | ||
| 63 | |||
| 64 | for my $key (qw(terminal_line terminal_height output)) { | ||
| 65 | $spinner_opts{$key} = $opts{$key} if defined $opts{$key}; | ||
| 66 | } | ||
| 67 | |||
| 68 | $_active_spinner = Term::ProgressSpinner->new(%spinner_opts); | ||
| 69 | |||
| 70 | if ($loop) { | ||
| 71 | $_active_spinner->start_async($loop, interval => $interval); | ||
| 72 | } else { | ||
| 73 | $_active_spinner->start(1); | ||
| 74 | $_active_spinner->draw($_active_spinner); | ||
| 75 | } | ||
| 76 | |||
| 77 | return $_active_spinner; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub stop_spinner { | ||
| 81 | my ($spinner, $success_message) = @_; | ||
| 82 | |||
| 83 | $spinner //= $_active_spinner; | ||
| 84 | return unless $spinner; | ||
| 85 | |||
| 86 | if ($spinner->{_async_timer}) { | ||
| 87 | $spinner->stop_async($success_message); | ||
| 88 | } else { | ||
| 89 | $spinner->finish(); | ||
| 90 | status('success', $success_message) if $success_message; | ||
| 91 | } | ||
| 92 | |||
| 93 | $_active_spinner = undef; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub prompt { | ||
| 97 | my ($message, $default) = @_; | ||
| 98 | my $prompt_str = $message; | ||
| 99 | $prompt_str .= " [$default]" if defined $default && $default ne ''; | ||
| 100 | $prompt_str .= ": "; | ||
| 101 | |||
| 102 | my $answer = _term->readline($prompt_str); | ||
| 103 | $answer //= ''; | ||
| 104 | $answer =~ s/^\s+|\s+$//g; | ||
| 105 | |||
| 106 | return ($answer ne '' ? $answer : $default) // ''; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub ask_yn { | ||
| 110 | my ($message, $default) = @_; | ||
| 111 | $default //= 'y'; | ||
| 112 | |||
| 113 | my @options = $default =~ /^y/i | ||
| 114 | ? ('Yes', 'No') | ||
| 115 | : ('No', 'Yes'); | ||
| 116 | |||
| 117 | my $choice = choose( | ||
| 118 | \@options, | ||
| 119 | { | ||
| 120 | prompt => "$message ", | ||
| 121 | layout => 1, | ||
| 122 | } | ||
| 123 | ); | ||
| 124 | |||
| 125 | return unless defined $choice; | ||
| 126 | return $choice eq 'Yes'; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub menu { | ||
| 130 | my ($title, $options) = @_; | ||
| 131 | |||
| 132 | my @labels = map { "[$_->{key}] $_->{label}" } @$options; | ||
| 133 | |||
| 134 | my $choice = choose( | ||
| 135 | \@labels, | ||
| 136 | { | ||
| 137 | prompt => "$title:", | ||
| 138 | layout => 2, | ||
| 139 | } | ||
| 140 | ); | ||
| 141 | |||
| 142 | return unless defined $choice; | ||
| 143 | |||
| 144 | if ($choice =~ /^\[(\w+)\]/) { | ||
| 145 | return $1; | ||
| 146 | } | ||
| 147 | return $options->[0]{key}; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub select_option { | ||
| 151 | my ($options, %args) = @_; | ||
| 152 | |||
| 153 | my @display; | ||
| 154 | for my $i (0 .. $#$options) { | ||
| 155 | my $preview = substr($options->[$i], 0, 60); | ||
| 156 | $preview .= "..." if length($options->[$i]) > 60; | ||
| 157 | $preview =~ s/\n/ /g; | ||
| 158 | push @display, "[$i] $preview"; | ||
| 159 | } | ||
| 160 | |||
| 161 | if ($args{allow_custom}) { | ||
| 162 | push @display, "[c] Custom"; | ||
| 163 | } | ||
| 164 | |||
| 165 | my $choice = choose( | ||
| 166 | \@display, | ||
| 167 | { | ||
| 168 | prompt => $args{prompt} // 'Select:', | ||
| 169 | layout => 2, | ||
| 170 | } | ||
| 171 | ); | ||
| 172 | |||
| 173 | return unless defined $choice; | ||
| 174 | |||
| 175 | return undef if $choice =~ /^\[c\]/; | ||
| 176 | |||
| 177 | if ($choice =~ /^\[(\d+)\]/) { | ||
| 178 | return $options->[$1]; | ||
| 179 | } | ||
| 180 | return $options->[0]; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub choose_from { | ||
| 184 | my ($options, %args) = @_; | ||
| 185 | my $prompt_text = $args{prompt} // 'Select an option:'; | ||
| 186 | |||
| 187 | header($prompt_text) if $args{show_header}; | ||
| 188 | |||
| 189 | return choose( | ||
| 190 | $options, | ||
| 191 | { | ||
| 192 | prompt => $args{inline_prompt} // '', | ||
| 193 | layout => $args{layout} // 1, | ||
| 194 | index => $args{return_index} // 0, | ||
| 195 | mouse => $args{mouse} // 0, | ||
| 196 | search => 1, | ||
| 197 | } | ||
| 198 | ); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub choose_multiple { | ||
| 202 | my ($options, %args) = @_; | ||
| 203 | my @selected = choose( | ||
| 204 | $options, | ||
| 205 | { | ||
| 206 | prompt => $args{prompt} // 'Select items (Space to mark, Enter to confirm):', | ||
| 207 | layout => 2, | ||
| 208 | mark => $args{preselected} // [], | ||
| 209 | } | ||
| 210 | ); | ||
| 211 | return @selected; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub header { | ||
| 215 | my ($text) = @_; | ||
| 216 | my $line = "=" x 60; | ||
| 217 | print colored(['bold', 'blue'], $line), "\n"; | ||
| 218 | print colored(['bold', 'white'], " $text"), "\n"; | ||
| 219 | print colored(['bold', 'blue'], $line), "\n\n"; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub divider { | ||
| 223 | my ($char, $width) = @_; | ||
| 224 | $char //= '-'; | ||
| 225 | $width //= 60; | ||
| 226 | print $char x $width, "\n"; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub status { | ||
| 230 | my ($type, $message) = @_; | ||
| 231 | my %colors = ( | ||
| 232 | success => 'green', | ||
| 233 | error => 'red', | ||
| 234 | warning => 'yellow', | ||
| 235 | info => 'cyan', | ||
| 236 | ); | ||
| 237 | my %icons = ( | ||
| 238 | success => "\x{2713}", | ||
| 239 | error => "\x{2717}", | ||
| 240 | warning => "\x{26A0}", | ||
| 241 | info => "\x{2139}", | ||
| 242 | ); | ||
| 243 | my $color = $colors{$type} // 'white'; | ||
| 244 | my $icon = $icons{$type} // '*'; | ||
| 245 | print _encoded(colored([$color], "$icon $message") . "\n"); | ||
| 246 | } | ||
| 247 | |||
| 248 | sub _encoded { | ||
| 249 | my ($text) = @_; | ||
| 250 | return $text if grep { /utf8|encoding/ } PerlIO::get_layers(\*STDOUT); | ||
| 251 | utf8::encode($text); | ||
| 252 | return $text; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub clear_line { | ||
| 256 | print "\r\033[K"; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub move_up { | ||
| 260 | my ($n) = @_; | ||
| 261 | $n //= 1; | ||
| 262 | print "\033[${n}A"; | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | 1; | ||
| 267 | |||
| 268 | __END__ |