« blib/lib/Term/Fabulous/Widget/Dropdown/List.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/FileDialog/FolderLine.pm »
| 1 | package Term::Fabulous::Widget::FileDialog; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | use Object::Pad 0.825; | ||
| 9 | |||
| 10 | use Term::Fabulous::Widget::Dialog; | ||
| 11 | |||
| 12 | our $VERSION = '0.02'; | ||
| 13 | |||
| 14 | class Term::Fabulous::Widget::FileDialog | ||
| 15 | :isa(Term::Fabulous::Widget::Dialog) | ||
| 16 | :strict(params) | ||
| 17 | { | ||
| 18 | use Clay::UI::Enum::Result; | ||
| 19 | use Clay::XS qw(sizing_grow); | ||
| 20 | use Cwd qw(abs_path getcwd); | ||
| 21 | use Encode qw(decode encode); | ||
| 22 | use File::Basename qw(basename dirname); | ||
| 23 | use File::Spec; | ||
| 24 | use List::Util qw(any first min); | ||
| 25 | use POSIX qw(strftime); | ||
| 26 | use Scalar::Util qw(weaken); | ||
| 27 | use Term::Fabulous::Check qw(boolean describe one_of positive_integer string); | ||
| 28 | use Term::Fabulous::Event::Answer; | ||
| 29 | use Term::Fabulous::Widget::Box; | ||
| 30 | use Term::Fabulous::Widget::Button; | ||
| 31 | use Term::Fabulous::Widget::Checkbox; | ||
| 32 | use Term::Fabulous::Widget::Dropdown; | ||
| 33 | use Term::Fabulous::Widget::FileDialog::FolderLine; | ||
| 34 | use Term::Fabulous::Widget::Prompt; | ||
| 35 | use Term::Fabulous::Widget::Table; | ||
| 36 | use Term::Fabulous::Widget::Table::Frame qw(framed); | ||
| 37 | use Term::Fabulous::Widget::Text; | ||
| 38 | use Term::Fabulous::Widget::TextField; | ||
| 39 | |||
| 40 | use constant DEFAULT_WIDTH => 70; | ||
| 41 | use constant CANCEL => 'cancel'; | ||
| 42 | use constant FEWEST_ROWS => 2; # the entries the list keeps on a small screen | ||
| 43 | |||
| 44 | # What each mode calls itself: its default title, the label and the | ||
| 45 | # placeholder of the field, and the button (and answer) that takes | ||
| 46 | # the choice. | ||
| 47 | my %MODE = ( | ||
| 48 | open => { title => 'Open a file', field => 'File', placeholder => 'A file name or a path', button => 'Open', answer => 'open' }, | ||
| 49 | save => { title => 'Save the file as', field => 'File', placeholder => 'A file name or a path', button => 'Save', answer => 'save' }, | ||
| 50 | folder => { title => 'Choose a folder', field => 'Name', placeholder => 'A folder name or a path', button => 'Choose', answer => 'choose' }, | ||
| 51 | ); | ||
| 52 | |||
| 53 | my $generated_ids = 0; | ||
| 54 | |||
| 55 | field $mode :param = 'open'; | ||
| 56 | field $directory :param = undef; # bytes, as the file system has them | ||
| 57 | field $current_file :param = undef; # bytes | ||
| 58 | field $title :param = undef; | ||
| 59 | field $list_rows :param = 10; | ||
| 60 | field $show_hidden :param = 0; | ||
| 61 | field $default_extension :param = undef; | ||
| 62 | field $confirm_overwrite :param = undef; | ||
| 63 | |||
| 64 | field @_filters; # [ { label, patterns => [ regex, ... ] } ], in order | ||
| 65 | field $_filter; # the index of the active filter, undef without filters | ||
| 66 | field $_folder_line; | ||
| 67 | field $_list; | ||
| 68 | field $_field; | ||
| 69 | field $_filter_choice; # the Dropdown, undef with fewer than two filters | ||
| 70 | field $_hidden_box; | ||
| 71 | field $_message; | ||
| 72 | field $_replace_question; # the open Replace question, a weak reference | ||
| 73 | |||
| 74 | # A file dialog is a dialog with room at its sides and no gaps between | ||
| 75 | # its parts (it has many), as wide as the program asks or as the | ||
| 76 | # screen when that is narrower, with an id the id of its list derives | ||
| 77 | # from. | ||
| 78 | sub BUILDARGS ( $class, %params ) { | ||
| 79 | my $width = positive_integer( $class, width => $params{width} // DEFAULT_WIDTH ); | ||
| 80 | my $layout = $params{layout} // {}; | ||
| 81 | $params{id} //= 'file-dialog-' . ++$generated_ids; | ||
| 82 | $params{layout} = { | ||
| 83 | padding => { left => 2, right => 2, top => 1, bottom => 1 }, | ||
| 84 | child_gap => 0, | ||
| 85 | %$layout, | ||
| 86 | sizing => { width => sizing_grow( 0, $width ), %{ $layout->{sizing} // {} } }, | ||
| 87 | }; | ||
| 88 | return $class->SUPER::BUILDARGS(%params); | ||
| 89 | } | ||
| 90 | |||
| 91 | ADJUST :params ( :$filters = undef, :$name = undef, :$width = undef ) { | ||
| 92 | $mode = one_of( $self, mode => $mode, sort keys %MODE ); | ||
| 93 | if ( $mode eq 'folder' ) { | ||
| 94 | my %meaningless = ( filters => $filters, default_extension => $default_extension, name => $name, confirm_overwrite => $confirm_overwrite ); | ||
| 95 | my @given = grep { defined $meaningless{$_} } sort keys %meaningless; | ||
| 96 | die ref($self) . ": a folder dialog takes no " . join( ', ', @given ) if @given; | ||
| 97 | } | ||
| 98 | $directory = $self->_checked_directory( $directory // getcwd() ); | ||
| 99 | $current_file = $self->_checked_current_file($current_file); | ||
| 100 | $title = string( $self, title => $title // $MODE{$mode}{title} ); | ||
| 101 | $list_rows = positive_integer( $self, list_rows => $list_rows ); | ||
| 102 | $show_hidden = boolean( $self, show_hidden => $show_hidden ); | ||
| 103 | $default_extension = $self->_checked_extension($default_extension); | ||
| 104 | $confirm_overwrite = boolean( $self, confirm_overwrite => $confirm_overwrite // 1 ); | ||
| 105 | @_filters = $self->_parsed_filters( $filters // [] ); | ||
| 106 | $_filter = @_filters ? 0 : undef; | ||
| 107 | |||
| 108 | $self->_build( _decoded( $self->_checked_bytes( name => $name // '' ) ) ); | ||
| 109 | } | ||
| 110 | |||
| 111 | # --------------------------------------------------------------------- | ||
| 112 | # Parameters | ||
| 113 | # --------------------------------------------------------------------- | ||
| 114 | |||
| 115 | # Paths and names come as the file system has them: bytes, not | ||
| 116 | # characters. | ||
| 117 | method _checked_bytes ( $name, $value ) { | ||
| 118 | $value = string( $self, $name => $value ); | ||
| 119 | die ref($self) . ": $name must be bytes as the file system has them (encode it to UTF-8 first), got a character string" if $value =~ /[^\x00-\xff]/; | ||
| 120 | return $value; | ||
| 121 | } | ||
| 122 | |||
| 123 | method _checked_directory ($path) { | ||
| 124 | $path = $self->_checked_bytes( directory => $path ); | ||
| 125 | die ref($self) . ": directory '$path' is not a folder" unless -d $path; | ||
| 126 | return _absolute($path); | ||
| 127 | } | ||
| 128 | |||
| 129 | method _checked_current_file ($path) { | ||
| 130 | return undef unless defined $path; | ||
| 131 | $path = $self->_checked_bytes( current_file => $path ); | ||
| 132 | die ref($self) . ": current_file must be an absolute path, got '$path'" unless File::Spec->file_name_is_absolute($path); | ||
| 133 | return _canonical($path); | ||
| 134 | } | ||
| 135 | |||
| 136 | method _checked_extension ($extension) { | ||
| 137 | return undef unless defined $extension; | ||
| 138 | $extension = string( $self, default_extension => $extension ); | ||
| 139 | die ref($self) . ": default_extension must be an extension without its dot, like 'txt', got '$extension'" unless $extension =~ m{\A[^./\s][^/\s]*\z}; | ||
| 140 | return $extension; | ||
| 141 | } | ||
| 142 | |||
| 143 | # A filter is [ label, patterns ]: the patterns are globs separated by | ||
| 144 | # white space, matched without case. | ||
| 145 | method _parsed_filters ($filters) { | ||
| 146 | die ref($self) . ": filters must be an array reference, got " . describe($filters) unless ref $filters eq 'ARRAY'; | ||
| 147 | my ( @parsed, %seen ); | ||
| 148 | foreach my $filter (@$filters) { | ||
| 149 | die ref($self) . ": a filter must be [ label, patterns ], got " . describe($filter) unless ref $filter eq 'ARRAY' && @$filter == 2; | ||
| 150 | my $label = string( $self, 'the label of a filter' => $filter->[0] ); | ||
| 151 | my @patterns = split ' ', string( $self, "the patterns of the filter '$label'" => $filter->[1] ); | ||
| 152 | die ref($self) . ": the filter '$label' has no patterns" unless @patterns; | ||
| 153 | die ref($self) . ": two filters are labelled '$label'" if $seen{$label}++; | ||
| 154 | push @parsed, { label => $label, patterns => [ map { _glob_regex($_) } @patterns ] }; | ||
| 155 | } | ||
| 156 | return @parsed; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub _glob_regex ($glob) { | ||
| 160 | my $regex = join '', map { $_ eq '*' ? '.*' : $_ eq '?' ? '.' : quotemeta } split /([*?])/, $glob; | ||
| 161 | return qr/\A$regex\z/is; | ||
| 162 | } | ||
| 163 | |||
| 164 | # --------------------------------------------------------------------- | ||
| 165 | # The parts | ||
| 166 | # --------------------------------------------------------------------- | ||
| 167 | |||
| 168 | method _build ($name) { | ||
| 169 | weaken( my $weak_self = $self ); | ||
| 170 | my @size_column = $mode eq 'folder' ? () : ( { key => 'size', title => 'Size', width => 'fit', sortable => 0, align => 'right' } ); | ||
| 171 | $_folder_line = Term::Fabulous::Widget::FileDialog::FolderLine->new; | ||
| 172 | $_list = Term::Fabulous::Widget::Table->new( | ||
| 173 | id => $self->id . '/list', | ||
| 174 | row_id => 'entry', | ||
| 175 | selection => 'single', | ||
| 176 | border => 'none', | ||
| 177 | column_lines => 'none', | ||
| 178 | header_line => 'Solid', | ||
| 179 | columns => [ | ||
| 180 | { key => 'name', title => 'Name', width => 'grow', sortable => 0, cell => \&_name_cell }, | ||
| 181 | @size_column, | ||
| 182 | { key => 'modified', title => 'Modified', width => 'fit', sortable => 0 }, | ||
| 183 | ], | ||
| 184 | layout => { sizing => { width => sizing_grow(), height => sizing_grow() } }, | ||
| 185 | ); | ||
| 186 | $_list->on( RowActivate => sub ($event) { $weak_self->_enter( $event->row_id ); return } ); | ||
| 187 | $_list->on( CursorMove => sub ($event) { $weak_self->_pointed( $event->row_id ); return Clay::UI::Enum::Result->CONTINUE } ); | ||
| 188 | $_list->on( KeyPress => sub ($event) { return ( $event->main_key_name // '' ) eq 'Backspace' && $weak_self->_up ? Clay::UI::Enum::Result->HANDLED : Clay::UI::Enum::Result->CONTINUE } ); | ||
| 189 | |||
| 190 | $_field = Term::Fabulous::Widget::TextField->new( value => $name, placeholder => $MODE{$mode}{placeholder}, layout => { sizing => { width => sizing_grow() } } ); | ||
| 191 | $_field->on( Submit => sub ($event) { $weak_self->_submitted( $event->value ); return } ); | ||
| 192 | my $field_row = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow() }, child_gap => 1 } ); | ||
| 193 | $field_row->add_child( Term::Fabulous::Widget::Text->new( text => $MODE{$mode}{field} ), $_field ); | ||
| 194 | |||
| 195 | my $options_row = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_grow() }, child_gap => 2 } ); | ||
| 196 | if ( @_filters > 1 ) { | ||
| 197 | $_filter_choice = Term::Fabulous::Widget::Dropdown->new( options => [ map { $_->{label} } @_filters ], value => $_filters[0]{label} ); | ||
| 198 | $_filter_choice->on( Change => sub ($event) { $weak_self->filter( $event->value ); return Clay::UI::Enum::Result->CONTINUE } ); | ||
| 199 | $options_row->add_child($_filter_choice); | ||
| 200 | } | ||
| 201 | $_hidden_box = Term::Fabulous::Widget::Checkbox->new( label => 'Show hidden files', checked => $show_hidden ); | ||
| 202 | $_hidden_box->on( Change => sub ($event) { $weak_self->show_hidden( $event->value ); return Clay::UI::Enum::Result->CONTINUE } ); | ||
| 203 | $options_row->add_child($_hidden_box); | ||
| 204 | |||
| 205 | # The list counts its header and the line below it as rows. | ||
| 206 | $_message = Term::Fabulous::Widget::FileDialog::FolderLine->new( label => '' ); | ||
| 207 | $self->add_child( | ||
| 208 | Term::Fabulous::Widget::Text->new( text => $title, bold => 1 ), | ||
| 209 | $_folder_line, framed( $_list, $list_rows + 2, min( $list_rows, FEWEST_ROWS ) + 2 ), | ||
| 210 | $field_row, $options_row, $_message, $self->_button_row, | ||
| 211 | ); | ||
| 212 | return; | ||
| 213 | } | ||
| 214 | |||
| 215 | # A name too long for its column is cut from the left, so that it | ||
| 216 | # does not push the size and the time out of the list. | ||
| 217 | sub _name_cell ($cell) { | ||
| 218 | return Term::Fabulous::Widget::FileDialog::FolderLine->new( label => '', path => $cell->{display} ); | ||
| 219 | } | ||
| 220 | |||
| 221 | method _button_row () { | ||
| 222 | weaken( my $weak_self = $self ); | ||
| 223 | my $row = Term::Fabulous::Widget::Box->new( layout => { child_gap => 2 } ); | ||
| 224 | foreach my $definition ( [ $MODE{$mode}{button}, ['primary'], sub { $weak_self->choose( $weak_self->typed ) } ], [ 'Cancel', [], sub { $weak_self->_cancel } ] ) { | ||
| 225 | my ( $label, $classes, $action ) = @$definition; | ||
| 226 | my $button = Term::Fabulous::Widget::Button->new( bordered => 1, classes => $classes, layout => { padding => { left => 1, right => 1 } } ); | ||
| 227 | $button->add_child( Term::Fabulous::Widget::Text->new( text => $label ) ); | ||
| 228 | $button->on( Activate => sub ($event) { $action->(); return } ); | ||
| 229 | $row->add_child($button); | ||
| 230 | } | ||
| 231 | return $row; | ||
| 232 | } | ||
| 233 | |||
| 234 | # The line above the buttons: one row, empty while all is well. A path | ||
| 235 | # in it is cut from the left to fit, like the folder line. | ||
| 236 | method _say ( $text, $path = '', $after = '' ) { | ||
| 237 | $_message->say( $text, $path, $after ); | ||
| 238 | return $self; | ||
| 239 | } | ||
| 240 | |||
| 241 | # A file or folder that is not there, named as the user typed it. | ||
| 242 | method _say_missing ( $kind, $target ) { | ||
| 243 | return $self->_say( "There is no $kind ", $target->{shown}, $target->{here} ? ' in this folder.' : '.' ); | ||
| 244 | } | ||
| 245 | |||
| 246 | # --------------------------------------------------------------------- | ||
| 247 | # Accessors | ||
| 248 | # --------------------------------------------------------------------- | ||
| 249 | |||
| 250 | method directory () { | ||
| 251 | return $directory; | ||
| 252 | } | ||
| 253 | |||
| 254 | method typed () { | ||
| 255 | return $_field->value; | ||
| 256 | } | ||
| 257 | |||
| 258 | method list () { | ||
| 259 | return $_list; | ||
| 260 | } | ||
| 261 | |||
| 262 | method filter (@new) { | ||
| 263 | return defined $_filter ? $_filters[$_filter]{label} : undef unless @new; | ||
| 264 | my $label = $new[0]; | ||
| 265 | my $index = first { $_filters[$_]{label} eq ( $label // '' ) } 0 .. $#_filters; | ||
| 266 | die ref($self) . ": the dialog has no filter labelled " . describe($label) unless defined $index; | ||
| 267 | $_filter = $index; | ||
| 268 | $_filter_choice->value($label) if defined $_filter_choice && ( $_filter_choice->value // '' ) ne $label; | ||
| 269 | $self->_list_directory if $self->is_open; | ||
| 270 | return $label; | ||
| 271 | } | ||
| 272 | |||
| 273 | method show_hidden (@new) { | ||
| 274 | return $show_hidden unless @new; | ||
| 275 | $show_hidden = boolean( $self, show_hidden => $new[0] ); | ||
| 276 | $_hidden_box->checked($show_hidden) if $_hidden_box->checked != $show_hidden; | ||
| 277 | $self->_list_directory if $self->is_open; | ||
| 278 | return $show_hidden; | ||
| 279 | } | ||
| 280 | |||
| 281 | # --------------------------------------------------------------------- | ||
| 282 | # The folder | ||
| 283 | # --------------------------------------------------------------------- | ||
| 284 | |||
| 285 | method _list_directory () { | ||
| 286 | $_folder_line->path( _decoded($directory) ); | ||
| 287 | my @rows = $directory eq File::Spec->rootdir ? () : ( { name => '../', entry => '..', kind => 'up', size => '', modified => '' } ); | ||
| 288 | if ( opendir my $handle, $directory ) { | ||
| 289 | push @rows, $self->_rows_of( grep { $_ ne '.' && $_ ne '..' } readdir $handle ); | ||
| 290 | closedir $handle; | ||
| 291 | $self->_say(''); | ||
| 292 | } | ||
| 293 | else { | ||
| 294 | $self->_say("Cannot read the folder: $!."); | ||
| 295 | } | ||
| 296 | $_list->rows( \@rows ); | ||
| 297 | return; | ||
| 298 | } | ||
| 299 | |||
| 300 | # The rows of the folder's entries: the folders, then the files the | ||
| 301 | # filter lets through, each sorted by the name shown. | ||
| 302 | method _rows_of (@entries) { | ||
| 303 | my ( @folders, @files ); | ||
| 304 | foreach my $entry ( grep { $show_hidden || !/\A\./ } @entries ) { | ||
| 305 | my $path = File::Spec->catfile( $directory, $entry ); | ||
| 306 | my $name = _decoded($entry); | ||
| 307 | if ( -d $path ) { | ||
| 308 | push @folders, { name => "$name/", entry => $entry, kind => 'folder', size => '', modified => _when($path) }; | ||
| 309 | next; | ||
| 310 | } | ||
| 311 | next if $mode eq 'folder' || !$self->_passes_filter($name); | ||
| 312 | push @files, { name => $name, entry => $entry, kind => 'file', size => _size($path), modified => _when($path) }; | ||
| 313 | } | ||
| 314 | return map { | ||
| 315 | sort { fc $a->{name} cmp fc $b->{name} or $a->{entry} cmp $b->{entry} } | ||
| 316 | @$_ | ||
| 317 | } \@folders, \@files; | ||
| 318 | } | ||
| 319 | |||
| 320 | method _passes_filter ($name) { | ||
| 321 | return 1 unless defined $_filter; | ||
| 322 | return any { $name =~ $_ } @{ $_filters[$_filter]{patterns} }; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub _size ($path) { | ||
| 326 | my $bytes = -s $path // 0; | ||
| 327 | return $bytes < 1024 ? "$bytes B" : $bytes < 1024 * 1024 ? sprintf( '%.1f KiB', $bytes / 1024 ) : sprintf( '%.1f MiB', $bytes / 1024 / 1024 ); | ||
| 328 | } | ||
| 329 | |||
| 330 | sub _when ($path) { | ||
| 331 | my $time = ( stat $path )[9] // return ''; | ||
| 332 | return strftime( '%Y-%m-%d %H:%M', localtime $time ); | ||
| 333 | } | ||
| 334 | |||
| 335 | # The file system's names are bytes; the dialog shows and matches them | ||
| 336 | # as UTF-8 text, with a byte that is no UTF-8 shown as \xHH. | ||
| 337 | sub _decoded ($bytes) { | ||
| 338 | return decode( 'UTF-8', $bytes, Encode::FB_PERLQQ | Encode::LEAVE_SRC ); | ||
| 339 | } | ||
| 340 | |||
| 341 | sub _encoded ($text) { | ||
| 342 | return encode( 'UTF-8', $text ); | ||
| 343 | } | ||
| 344 | |||
| 345 | # An absolute path without symbolic links, or just made absolute when | ||
| 346 | # they cannot be resolved. | ||
| 347 | sub _absolute ($path) { | ||
| 348 | return abs_path($path) // File::Spec->rel2abs($path); | ||
| 349 | } | ||
| 350 | |||
| 351 | # The same for a file that may not exist yet: its folder resolved. | ||
| 352 | sub _canonical ($path) { | ||
| 353 | return _absolute($path) if -d $path; | ||
| 354 | return File::Spec->catfile( _absolute( dirname($path) ), basename($path) ); | ||
| 355 | } | ||
| 356 | |||
| 357 | # Another folder: the field empties, except in save mode, where it | ||
| 358 | # keeps the name to save under. | ||
| 359 | method _go ($path) { | ||
| 360 | $directory = _absolute($path); | ||
| 361 | $_field->value('') unless $mode eq 'save'; | ||
| 362 | $self->_list_directory; | ||
| 363 | return 1; | ||
| 364 | } | ||
| 365 | |||
| 366 | method _up () { | ||
| 367 | return 0 if $directory eq File::Spec->rootdir; | ||
| 368 | return $self->_go( dirname($directory) ); | ||
| 369 | } | ||
| 370 | |||
| 371 | # Enter or a double click on an entry: a folder opens, a file is | ||
| 372 | # chosen (and shown in the field). | ||
| 373 | method _enter ($entry) { | ||
| 374 | return unless defined $entry; | ||
| 375 | my $row = $_list->row($entry); | ||
| 376 | return $self->_up if $row->{kind} eq 'up'; | ||
| 377 | return $self->_go( File::Spec->catdir( $directory, $entry ) ) if $row->{kind} eq 'folder'; | ||
| 378 | $_field->value( $row->{name} ); | ||
| 379 | $self->choose( $row->{name} ); | ||
| 380 | return; | ||
| 381 | } | ||
| 382 | |||
| 383 | # In open mode the cursor on a file puts its name into the field; in | ||
| 384 | # folder mode the cursor on a folder does, and on ../ empties it. In | ||
| 385 | # save mode the field keeps the name typed. | ||
| 386 | method _pointed ($entry) { | ||
| 387 | return unless defined $entry; | ||
| 388 | my $row = $_list->row($entry); | ||
| 389 | if ( $mode eq 'folder' ) { | ||
| 390 | $_field->value( $row->{kind} eq 'folder' ? $row->{name} =~ s{/\z}{}r : '' ); | ||
| 391 | return; | ||
| 392 | } | ||
| 393 | $_field->value( $row->{name} ) if $mode eq 'open' && $row->{kind} eq 'file'; | ||
| 394 | return; | ||
| 395 | } | ||
| 396 | |||
| 397 | # --------------------------------------------------------------------- | ||
| 398 | # Choosing | ||
| 399 | # --------------------------------------------------------------------- | ||
| 400 | |||
| 401 | # What a text in the field names: the entry the list shows under this | ||
| 402 | # name, or else the text as a path (in UTF-8 for the file system): | ||
| 403 | # relative to the folder shown, absolute, or starting with ~ for the | ||
| 404 | # home folder. The target keeps the text, to name it in messages, and | ||
| 405 | # whether it is relative to the folder shown. Undef for no text. | ||
| 406 | method _target ($typed) { | ||
| 407 | my $shown = $typed =~ s/\A\s+|\s+\z//gr; | ||
| 408 | return undef unless length $shown; | ||
| 409 | my $row = first { $_->{kind} ne 'up' && ( $_->{name} =~ s{/\z}{}r ) eq $shown } @{ $_list->rows }; | ||
| 410 | return { path => File::Spec->catfile( $directory, $row->{entry} ), shown => $shown, here => 1 } if defined $row; | ||
| 411 | my $path = _encoded($shown) =~ s{\A~(?=/|\z)}{$ENV{HOME} // '~'}er; | ||
| 412 | return { path => $path, shown => $shown, here => 0 } if File::Spec->file_name_is_absolute($path); | ||
| 413 | return { path => File::Spec->catfile( $directory, $path ), shown => $shown, here => 1 }; | ||
| 414 | } | ||
| 415 | |||
| 416 | # Enter in the field: in folder mode a folder typed opens, an empty | ||
| 417 | # field chooses the folder shown; otherwise as the button. | ||
| 418 | method _submitted ($typed) { | ||
| 419 | return $self->choose($typed) unless $mode eq 'folder'; | ||
| 420 | my $target = $self->_target($typed) // return $self->choose(''); | ||
| 421 | return $self->_say_missing( folder => $target ) unless -d $target->{path}; | ||
| 422 | $self->_go( $target->{path} ); | ||
| 423 | return; | ||
| 424 | } | ||
| 425 | |||
| 426 | method choose ($typed) { | ||
| 427 | die ref($self) . ": choose needs an open dialog" unless $self->is_open; | ||
| 428 | my $target = $self->_target($typed); | ||
| 429 | return $self->_choose_folder($target) if $mode eq 'folder'; | ||
| 430 | return $self->_say( $mode eq 'open' ? 'Choose a file, or type its name.' : 'Type a file name.' ) unless defined $target; | ||
| 431 | if ( -d $target->{path} ) { # a folder typed is used up: it opens | ||
| 432 | $_field->value(''); | ||
| 433 | $self->_go( $target->{path} ); | ||
| 434 | return $self; | ||
| 435 | } | ||
| 436 | return $mode eq 'open' ? $self->_choose_file_to_open($target) : $self->_choose_file_to_save($target); | ||
| 437 | } | ||
| 438 | |||
| 439 | method _choose_folder ($target) { | ||
| 440 | return $self->_answer( _absolute($directory) ) unless defined $target; | ||
| 441 | return $self->_say_missing( folder => $target ) unless -d $target->{path}; | ||
| 442 | return $self->_answer( _absolute( $target->{path} ) ); | ||
| 443 | } | ||
| 444 | |||
| 445 | method _choose_file_to_open ($target) { | ||
| 446 | return $self->_say_missing( file => $target ) unless -f $target->{path}; | ||
| 447 | return $self->_answer( _canonical( $target->{path} ) ); | ||
| 448 | } | ||
| 449 | |||
| 450 | method _choose_file_to_save ($target) { | ||
| 451 | my ( $path, $shown ) = @$target{qw(path shown)}; | ||
| 452 | ( $path, $shown ) = ( "$path.$default_extension", "$shown.$default_extension" ) if defined $default_extension && basename($path) !~ /\./; | ||
| 453 | return $self->_say_missing( folder => _folder_target( $target, $path, $shown ) ) unless -d dirname($path); | ||
| 454 | return $self->_say( '', $shown, ' is a folder.' ) if -d $path; # a folder only once the default extension is added | ||
| 455 | $path = _canonical($path); | ||
| 456 | return $self->_ask_to_replace($path) if $confirm_overwrite && -e $path && $path ne ( $current_file // '' ); | ||
| 457 | return $self->_answer($path); | ||
| 458 | } | ||
| 459 | |||
| 460 | # The folder of a file to save that is not there, named by the folder | ||
| 461 | # part of the text typed. | ||
| 462 | sub _folder_target ( $target, $path, $shown ) { | ||
| 463 | my ($folder) = $shown =~ m{\A(.*)/}; | ||
| 464 | return { shown => _decoded( dirname($path) ), here => 0 } unless defined $folder; | ||
| 465 | return { here => $target->{here}, shown => length $folder ? $folder : '/' }; | ||
| 466 | } | ||
| 467 | |||
| 468 | # A question above the dialog, starting on Cancel: Replace answers | ||
| 469 | # with the path, Cancel or Escape leave the dialog open. | ||
| 470 | method _ask_to_replace ($path) { | ||
| 471 | weaken( my $weak_self = $self ); | ||
| 472 | my $question = Term::Fabulous::Widget::Prompt->confirm( | ||
| 473 | $self->ui, | ||
| 474 | title => 'Replace the file?', | ||
| 475 | message => '[bold]' . ( _decoded( basename($path) ) =~ s/\[/\\[/gr ) . '[/] already exists. Replace it?', | ||
| 476 | ok => 'Replace', | ||
| 477 | focus => CANCEL, | ||
| 478 | z_index => $self->z_index + 1, | ||
| 479 | ); | ||
| 480 | $question->on( | ||
| 481 | Answer => sub ($event) { | ||
| 482 | $weak_self->_answer($path) if $event->button eq 'ok' && $weak_self && $weak_self->is_open; | ||
| 483 | return; | ||
| 484 | } | ||
| 485 | ); | ||
| 486 | weaken( $_replace_question = $question ); | ||
| 487 | return $self; | ||
| 488 | } | ||
| 489 | |||
| 490 | method _close_question () { | ||
| 491 | $_replace_question->close if defined $_replace_question && $_replace_question->is_open; | ||
| 492 | return; | ||
| 493 | } | ||
| 494 | |||
| 495 | # --------------------------------------------------------------------- | ||
| 496 | # Opening and answering | ||
| 497 | # --------------------------------------------------------------------- | ||
| 498 | |||
| 499 | # The folder is read each time the dialog opens. Save mode starts in | ||
| 500 | # the field. | ||
| 501 | method open :override ($ui) { | ||
| 502 | return $self if $self->is_open; | ||
| 503 | $self->_list_directory; | ||
| 504 | $self->SUPER::open($ui); | ||
| 505 | $self->_select_name($ui) if $mode eq 'save'; | ||
| 506 | return $self; | ||
| 507 | } | ||
| 508 | |||
| 509 | # The field focused with the name selected up to its extension, so | ||
| 510 | # typing replaces the name and keeps the extension. | ||
| 511 | method _select_name ($ui) { | ||
| 512 | my $stem = $_field->value =~ s/(?<=.)\.[^.]*\z//r; | ||
| 513 | $ui->interaction->set_focused_widget($_field); | ||
| 514 | $_field->editor->set_selection( 0, 0, 0, length $stem ); | ||
| 515 | return; | ||
| 516 | } | ||
| 517 | |||
| 518 | # The dialog's close, then the answer: a choice is no dismissal. | ||
| 519 | method _answer ($path) { | ||
| 520 | $self->_close_question; | ||
| 521 | $self->SUPER::close; | ||
| 522 | $self->fire_event( Term::Fabulous::Event::Answer->new( button => $MODE{$mode}{answer}, values => { path => $path } ) ); | ||
| 523 | return $self; | ||
| 524 | } | ||
| 525 | |||
| 526 | method _cancel () { | ||
| 527 | $self->_close_question; | ||
| 528 | $self->SUPER::close; | ||
| 529 | $self->fire_event( Term::Fabulous::Event::Answer->new( button => CANCEL ) ); | ||
| 530 | return $self; | ||
| 531 | } | ||
| 532 | |||
| 533 | # Escape, or close from the program: the question above closes too, | ||
| 534 | # and the dialog answers cancel, after the dialog's Close. | ||
| 535 | method close :override () { | ||
| 536 | return $self unless $self->is_open; | ||
| 537 | $self->_close_question; | ||
| 538 | $self->SUPER::close; | ||
| 539 | $self->fire_event( Term::Fabulous::Event::Answer->new( button => CANCEL, dismissed => 1 ) ); | ||
| 540 | return $self; | ||
| 541 | } | ||
| 542 | } | ||
| 543 | |||
| 544 | 1; | ||
| 545 | |||
| 546 | __END__ |