Skip to content

Instantly share code, notes, and snippets.

@benjacoblee
Created November 1, 2023 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjacoblee/71bc276c168cbddc339c5ab6b9532693 to your computer and use it in GitHub Desktop.
Save benjacoblee/71bc276c168cbddc339c5ab6b9532693 to your computer and use it in GitHub Desktop.
#!/home/benn/.asdf/shims/raku
our $TIL_EDITOR = %*ENV<TIL_EDITOR>;
our $TIL_DIR = %*ENV<TIL_DIR>;
exit say_env_errs("TIL_DIR") unless $TIL_DIR;
exit say_env_errs("TIL_EDITOR") unless $TIL_EDITOR;
sub today(--> Str) { Date.today.yyyy-mm-dd }
sub til_path(--> Str) {
my $today_str = today();
try {
mkdir $TIL_DIR unless $TIL_DIR.IO ~~ :d;
}
if $! {
say "Error: directory not created";
exit say $!;
}
return "$TIL_DIR/$today_str.md"
}
multi sub MAIN(
:o(:$open)? #= Opens a daily note in the configured editor
) {
try {
run ($TIL_EDITOR, til_path), :err;
:err;
}
if $! {
say "Error: something went wrong. Please make sure \"$TIL_EDITOR\" is a valid and executable editor"
} else { say til_path }
}
multi sub MAIN(
Str :a(:$append) #= Appends to daily note. Creates one if it doesn't exist
) {
spurt(til_path, "",) unless til_path.IO ~~ :e;
try {
spurt(til_path, "$append\n", :append);
}
}
multi sub MAIN(
Str :r(:$read) #= Show list of daily notes and open one for reading
) {
my @stack = dir($TIL_DIR);
@stack = @stack.sort: { $^a.IO.modified cmp $^b.IO.modified };
for @stack.kv -> $i, $file {
say "{$i + 1}. $file";
}
say "0. Exit";
my $file_idx_to_read = get;
exit if $file_idx_to_read eq "0";
try {
my $file_idx = $file_idx_to_read.Int - 1;
my $file = @stack[$file_idx];
exit say "Error: file doesn't exist" unless $file;
run ($TIL_EDITOR, $file), :err if $file;
:err;
}
if $! {
say "Error: {$!.reason}";
}
}
sub say_env_errs($env_var_name) is export {
say Q[Error: missing environment variable];
say Q:s「$env_var_name is not set. Please set it before running the CLI application.」;
say Q"For example:";
say Q:b:s"\$ export $env_var_name=value";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment