-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnt.pl
executable file
Β·380 lines (255 loc) Β· 7.3 KB
/
nt.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
98
99
100
101
102
103
104
105
106
107
108
109
110
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env perl
use strict;
use warnings;
use Carp qw( carp croak );
use English qw( -no_match_vars);
use Getopt::Long qw( GetOptions );
use IPC::Open3 qw( open3 );
use List::Util qw( reduce );
use Path::Tiny qw( path );
use Pod::Usage qw( pod2usage );
use Readonly qw( Readonly );
use Symbol 'gensym';
our $VERSION = 0.12;
Readonly my $COMMANDS => {
usage => 1,
init => 1,
list => 1,
view => 1,
add => 1,
edit => 1,
delete => 1,
};
Readonly my $CONFIG => {
base_directory => "$ENV{'HOME'}/.nt",
glow => 1,
gum => 1,
};
sub main {
my $options = {};
if ( !GetOptions( 'base_directory|b=s' => \$options->{'base_directory'} ) ) {
croak('Error in command line arguments');
}
$options->{'command'} = _validate( 'command', ( shift @ARGV ) // 'default' );
$options->{'name'} = shift @ARGV;
{ usage => sub { _usage(2) },
init => sub { _init() },
list => sub { _list() },
view => sub { _view( $options->{'name'} ) },
add => sub { _add( $options->{'name'} ) },
edit => sub { _edit( $options->{'name'} ) },
delete => sub { _delete( $options->{'name'} ) },
default => sub { _usage(0) },
}->{ $options->{'command'} }->();
return;
}
sub _usage {
my $verbose_level = shift;
pod2usage( { -verbose => $verbose_level } );
return;
}
sub _init {
if ( -d $CONFIG->{'base_directory'} ) {
return;
}
my $path = _get_path();
if ( !$path ) {
return;
}
$path->mkpath;
return;
}
sub _list {
if ( !-d $CONFIG->{'base_directory'} ) {
return;
}
my $path = _get_path();
if ( !$path ) {
return;
}
my $files = [ $path->children(qr/^[^.]/smx) ];
if ( $CONFIG->{'gum'} ) {
my $file = _prompt($files);
if ( !$file ) {
return;
}
my $action = _prompt( [ 'view', 'edit' ] );
if ( $action eq 'view' ) {
_view_file($file);
return;
}
if ( $action eq 'edit' ) {
_edit_file($file);
return;
}
return;
}
for my $file ( $files->@* ) {
print "$file\n" or croak $OS_ERROR;
}
return;
}
sub _prompt {
my $choices = shift;
my ( $prompt, $choice );
if ( $CONFIG->{'gum'} ) {
$prompt = join q{ }, 'gum', 'choose', $choices->@*;
$choice = qx/$prompt/;
return $choice;
}
return $choice;
}
sub _view {
my $name = shift;
if ( !$name ) {
return;
}
if ( !-d $CONFIG->{'base_directory'} ) {
return;
}
my $path = _get_path( [$name] );
if ( !$path ) {
return;
}
#my $file = ( reduce { $a->{ $b->basename } = $b; $a } {}, $path->children(qr/^[^.]/smx) )->{$name};
#if ( !$file ) {
# return;
#}
_view_file($path);
return;
}
sub _view_file {
my $file = shift;
if ( $CONFIG->{'glow'} ) {
system 'glow', '-p', $file;
return;
}
return;
}
sub _add {
my $name = shift;
if ( !$name ) {
return;
}
my $path = _get_path( [$name] );
if ( !$path ) {
return;
}
$path->touch;
return;
}
sub _edit {
my $name = shift;
if ( !$name ) {
return;
}
my $path = _get_path( [$name] );
if ( !$path ) {
return;
}
_edit_file($path);
return;
}
sub _edit_file {
my $file = shift;
my $editor = $ENV{'EDITOR'} || 'vi';
system $editor, $file;
my $exit_status = $CHILD_ERROR >> 8;
if ( $exit_status != 0 ) {
croak "Editor exited with non-zero status: $exit_status";
}
return;
}
sub _delete {
my $name = shift;
if ( !$name ) {
return;
}
my $path = _get_path( [$name] );
if ( !$path ) {
return;
}
if ( !$path->remove ) {
croak "Couldn't delete $name";
}
return;
}
sub _validate {
my ( $type, $param ) = @_;
return { command => sub { $COMMANDS->{$param} } }->{$type}->() ? $param : 'default';
}
sub _get_path {
my $components = shift;
$components //= [];
my $path = path( join q{/}, $CONFIG->{'base_directory'}, $components->@* );
return $path;
}
main();
=head1 NAME
nt - A script to manage a collection of notes with commands to initialize, list, add, edit, and delete notes.
=head1 SYNOPSIS
nt [options] <command> [name]
Options:
-b, --base_directory Set the base directory for storing notes (default: $HOME/.nt)
Commands:
usage Display the usage information
init Initialize the notes directory
list List all notes
view [name] View an existing note with the specified name
add [name] Add a new note with the specified name
edit [name] Edit an existing note with the specified name
delete [name] Delete an existing note with the specified name
=head1 DESCRIPTION
This script provides a command-line interface for managing a collection of notes stored in a directory. It supports basic operations such as initializing a storage directory, listing notes, adding new notes, editing existing notes, and deleting notes.
=head1 OPTIONS
=over 4
=item B<-b, --base_directory>
Specify the base directory where notes are stored. If not provided, the default is C<$HOME/.nt>.
=back
=head1 COMMANDS
=over 4
=item B<usage>
Displays the usage information and exits.
=item B<init>
Initializes the base directory for storing notes if it doesn't already exist.
=item B<list>
Lists all notes in the base directory. If the "gum" command is enabled in the configuration, it will prompt the user to view or edit a selected note.
=item B<view [name]>
Opens the note in the base directory with the specified name.
=item B<add [name]>
Adds a new note with the specified name. The note will be created as an empty file in the base directory.
=item B<edit [name]>
Edits an existing note with the specified name. The script will open the note in the editor specified by the C<$EDITOR> environment variable or default to "vi".
=item B<delete [name]>
Deletes the note with the specified name from the base directory.
=back
=head1 CONFIGURATION
The script uses a configuration hashref to store settings such as the base directory, and whether to use external tools like "glow" and "gum".
=over 4
=item B<$CONFIG-E<gt>{'base_directory'}>
The directory where all notes are stored. Defaults to C<$HOME/.nt>.
=item B<$CONFIG-E<gt>{'glow'}>
A flag indicating whether to use the "glow" tool for rendering markdown files.
=item B<$CONFIG-E<gt>{'gum'}>
A flag indicating whether to use the "gum" tool for interactive prompts.
=back
=head1 EXAMPLES
=over 4
=item Initialize the notes directory:
nt init
=item List all notes:
nt list
=item Add a new note called "meeting_notes":
nt add meeting_notes
=item Edit an existing note called "meeting_notes":
nt edit meeting_notes
=item Delete a note called "meeting_notes":
nt delete meeting_notes
=back
=head1 AUTHOR
Paul Derscheid, <[email protected]>
=head1 VERSION
This documentation refers to version 0.12 of nt.
=head1 COPYRIGHT AND LICENSE
This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
=cut