« blib/lib/Term/Fabulous/ThemeEditor/SettingsList.pm
↑ Summary
blib/lib/Term/Fabulous/ThemeEditor/UI.pm »
| 1 | package Term::Fabulous::ThemeEditor::Tab; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | use Object::Pad 0.825; | ||
| 11 | |||
| 12 | # One theme open in the theme editor: its text (in a text area, whose | ||
| 13 | # editor keeps the undo history), the document parsed from it, where it | ||
| 14 | # is saved and what was saved. | ||
| 15 | class Term::Fabulous::ThemeEditor::Tab :strict(params) { | ||
| 16 | use Clay::XS qw(sizing_grow); | ||
| 17 | use File::Basename qw(basename); | ||
| 18 | use Term::Fabulous::Check qw(describe); | ||
| 19 | use Term::Fabulous::Theme; | ||
| 20 | use Term::Fabulous::Theme::Document; | ||
| 21 | use Term::Fabulous::ThemeEditor::KdlArea; | ||
| 22 | |||
| 23 | field $path :param :reader = undef; | ||
| 24 | field $untitled :param :reader = undef; # the number of an untitled theme | ||
| 25 | field $mode :param = 'form'; | ||
| 26 | |||
| 27 | field $area :reader; | ||
| 28 | field $document :reader; | ||
| 29 | field $saved_text :reader; | ||
| 30 | field $saved_stamp; | ||
| 31 | field $crlf = 0; | ||
| 32 | field $last_valid_theme; | ||
| 33 | field $parsed_revision = -1; | ||
| 34 | |||
| 35 | # What the form showed last: the cursor's key, open sections, search. | ||
| 36 | field $form_state :reader = {}; | ||
| 37 | |||
| 38 | ADJUST :params ( :$text, :$modified = 0 ) { | ||
| 39 | die "Term::Fabulous::ThemeEditor::Tab: a tab needs a path or an untitled number" unless defined $path || defined $untitled; | ||
| 40 | die "Term::Fabulous::ThemeEditor::Tab: mode must be 'form' or 'text', got " . describe($mode) unless defined $mode && ( $mode eq 'form' || $mode eq 'text' ); | ||
| 41 | $crlf = _uses_crlf($text); | ||
| 42 | $area = Term::Fabulous::ThemeEditor::KdlArea->new( | ||
| 43 | value => $text, | ||
| 44 | wrap => 0, | ||
| 45 | layout => { sizing => { width => sizing_grow(), height => sizing_grow() } }, | ||
| 46 | ); | ||
| 47 | $saved_text = $modified ? undef : $area->value; | ||
| 48 | $saved_stamp = defined $path ? _stamp($path) : undef; | ||
| 49 | $self->reparse; | ||
| 50 | } | ||
| 51 | |||
| 52 | # Whether every line of a text ends in CRLF (a text that mixes line | ||
| 53 | # ends is saved with LF). | ||
| 54 | sub _uses_crlf ($text) { | ||
| 55 | return $text =~ /\r\n/ && $text !~ /(?<!\r)\n/ ? 1 : 0; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _stamp ($file) { | ||
| 59 | my @stat = stat $file; | ||
| 60 | return @stat ? join( ':', @stat[ 7, 9 ] ) : undef; | ||
| 61 | } | ||
| 62 | |||
| 63 | # --------------------------------------------------------------------- | ||
| 64 | # Names and state | ||
| 65 | # --------------------------------------------------------------------- | ||
| 66 | |||
| 67 | method title () { | ||
| 68 | return defined $path ? basename($path) : "untitled-$untitled"; | ||
| 69 | } | ||
| 70 | |||
| 71 | method text () { | ||
| 72 | return $area->value; | ||
| 73 | } | ||
| 74 | |||
| 75 | method is_modified () { | ||
| 76 | return !defined $saved_text || $self->text ne $saved_text ? 1 : 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | method mode (@new) { | ||
| 80 | return $mode unless @new; | ||
| 81 | die "Term::Fabulous::ThemeEditor::Tab: mode must be 'form' or 'text', got " . describe( $new[0] ) unless defined $new[0] && ( $new[0] eq 'form' || $new[0] eq 'text' ); | ||
| 82 | return $mode = $new[0]; | ||
| 83 | } | ||
| 84 | |||
| 85 | # The theme the preview shows: the document's, or while the text has | ||
| 86 | # errors the last one it had without, or the base theme. | ||
| 87 | method preview_theme () { | ||
| 88 | return $document->theme // $last_valid_theme // Term::Fabulous::Theme->builtin('dark'); | ||
| 89 | } | ||
| 90 | |||
| 91 | # Whether the preview shows an older state of the text. | ||
| 92 | method is_stale () { | ||
| 93 | return $document->is_valid ? 0 : 1; | ||
| 94 | } | ||
| 95 | |||
| 96 | # --------------------------------------------------------------------- | ||
| 97 | # The text | ||
| 98 | # --------------------------------------------------------------------- | ||
| 99 | |||
| 100 | # Parses the text again when it changed since it was parsed last; | ||
| 101 | # returns 1 when it did. | ||
| 102 | method sync () { | ||
| 103 | return 0 if $area->editor->text_revision == $parsed_revision; | ||
| 104 | $self->reparse; | ||
| 105 | return 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | method reparse () { | ||
| 109 | $parsed_revision = $area->editor->text_revision; | ||
| 110 | $document = Term::Fabulous::Theme::Document->parse( $area->value ); | ||
| 111 | my $theme = $document->theme; | ||
| 112 | $last_valid_theme = $theme if defined $theme; | ||
| 113 | return $document; | ||
| 114 | } | ||
| 115 | |||
| 116 | # Changes the text to a new one as one undo step, replacing the whole | ||
| 117 | # lines that differ, so the cursor and the view stay near the change. | ||
| 118 | method change_text ($new) { | ||
| 119 | my $old = $self->text; | ||
| 120 | return 0 if $old eq $new; | ||
| 121 | my ( $start, $old_end, $new_end ) = _changed_lines( $old, $new ); | ||
| 122 | my $editor = $area->editor; | ||
| 123 | $editor->set_selection( _position( $old, $start ), _position( $old, $old_end ) ); | ||
| 124 | $editor->insert( substr( $new, $start, $new_end - $start ) ); | ||
| 125 | $area->mark_changed; | ||
| 126 | $self->reparse; | ||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | |||
| 130 | # Changes the document with code that returns a changed document. | ||
| 131 | method change_document ($change) { | ||
| 132 | $self->sync; | ||
| 133 | my $changed = $change->($document); | ||
| 134 | return $self->change_text( $changed->text ); | ||
| 135 | } | ||
| 136 | |||
| 137 | # The range that differs between two texts, widened to whole lines: | ||
| 138 | # where it starts, and where it ends in the old and in the new text. | ||
| 139 | sub _changed_lines ( $old, $new ) { | ||
| 140 | my $limit = length $old < length $new ? length $old : length $new; | ||
| 141 | my $prefix = 0; | ||
| 142 | $prefix++ while $prefix < $limit && substr( $old, $prefix, 1 ) eq substr( $new, $prefix, 1 ); | ||
| 143 | my $suffix = 0; | ||
| 144 | $suffix++ while $suffix < $limit - $prefix && substr( $old, -1 - $suffix, 1 ) eq substr( $new, -1 - $suffix, 1 ); | ||
| 145 | |||
| 146 | my $start = $prefix == 0 ? 0 : rindex( $old, "\n", $prefix - 1 ) + 1; | ||
| 147 | my $tail_start = length($old) - $suffix; | ||
| 148 | my $old_end = $tail_start == 0 || substr( $old, $tail_start - 1, 1 ) eq "\n" ? $tail_start : index( $old, "\n", $tail_start ) + 1 || length $old; | ||
| 149 | my $new_end = length($new) - ( length($old) - $old_end ); | ||
| 150 | return ( $start, $old_end, $new_end ); | ||
| 151 | } | ||
| 152 | |||
| 153 | # ( row, offset ) of a character offset in a text. | ||
| 154 | sub _position ( $text, $offset ) { | ||
| 155 | my $before = substr( $text, 0, $offset ); | ||
| 156 | my $row = ( $before =~ tr/\n// ); | ||
| 157 | return ( $row, $offset - ( rindex( $before, "\n" ) + 1 ) ); | ||
| 158 | } | ||
| 159 | |||
| 160 | method undo () { | ||
| 161 | return 0 unless $area->editor->undo; | ||
| 162 | $area->mark_changed; | ||
| 163 | $self->reparse; | ||
| 164 | return 1; | ||
| 165 | } | ||
| 166 | |||
| 167 | method redo () { | ||
| 168 | return 0 unless $area->editor->redo; | ||
| 169 | $area->mark_changed; | ||
| 170 | $self->reparse; | ||
| 171 | return 1; | ||
| 172 | } | ||
| 173 | |||
| 174 | method can_undo () { | ||
| 175 | return $area->editor->can_undo ? 1 : 0; | ||
| 176 | } | ||
| 177 | |||
| 178 | method can_redo () { | ||
| 179 | return $area->editor->can_redo ? 1 : 0; | ||
| 180 | } | ||
| 181 | |||
| 182 | # --------------------------------------------------------------------- | ||
| 183 | # Files | ||
| 184 | # --------------------------------------------------------------------- | ||
| 185 | |||
| 186 | # Whether the file changed on the disk since it was read or saved. | ||
| 187 | method changed_on_disk () { | ||
| 188 | return 0 unless defined $path && defined $saved_stamp; | ||
| 189 | my $now = _stamp($path) // return 1; | ||
| 190 | return $now eq $saved_stamp ? 0 : 1; | ||
| 191 | } | ||
| 192 | |||
| 193 | # Writes the text to a file (in the line ends it was read with) and | ||
| 194 | # makes that the tab's file. | ||
| 195 | method save ( $target = $path ) { | ||
| 196 | die "Term::Fabulous::ThemeEditor::Tab: save needs a file name" unless defined $target && length $target; | ||
| 197 | my $text = $self->text; | ||
| 198 | Term::Fabulous::Theme::Document->parse( $self->_file_text )->write_file($target); | ||
| 199 | $path = $target; | ||
| 200 | $untitled = undef; | ||
| 201 | $saved_text = $text; | ||
| 202 | $saved_stamp = _stamp($target); | ||
| 203 | return $self; | ||
| 204 | } | ||
| 205 | |||
| 206 | # Reads the file again; its text replaces the tab's as one undo step. | ||
| 207 | method revert () { | ||
| 208 | die "Term::Fabulous::ThemeEditor::Tab: an untitled theme has no file to revert to" unless defined $path; | ||
| 209 | my $file = Term::Fabulous::Theme::Document->read_file($path)->text; | ||
| 210 | $crlf = _uses_crlf($file); | ||
| 211 | $self->change_text( $file =~ s/\r\n?/\n/gr ); | ||
| 212 | $saved_text = $self->text; | ||
| 213 | $saved_stamp = _stamp($path); | ||
| 214 | return $self; | ||
| 215 | } | ||
| 216 | |||
| 217 | # Writes the unsaved text next to the file (or into a directory for an | ||
| 218 | # untitled theme) without making that the tab's file; returns the path. | ||
| 219 | method write_recovery ($directory) { | ||
| 220 | my $target = ( defined $path ? $path : "$directory/" . $self->title . '.kdl' ) . '.unsaved'; | ||
| 221 | Term::Fabulous::Theme::Document->parse( $self->_file_text )->write_file($target); | ||
| 222 | return $target; | ||
| 223 | } | ||
| 224 | |||
| 225 | # The text as the file holds it: in the line ends it was read with. | ||
| 226 | method _file_text () { | ||
| 227 | my $text = $self->text; | ||
| 228 | return $crlf ? $text =~ s/\n/\r\n/gr : $text; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | 1; | ||
| 233 | |||
| 234 | __END__ |