-
-
Save Mrfiregem/bacfe353e0d3117d1ba195d994431bec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env nu | |
| # Path to folder where note files are stored | |
| const jottfile = $nu.data-dir | path dirname | path join 'jottfile.json' | |
| # Barebones note keeper with basic category support | |
| def main [] { help main } | |
| # Check if the input string matches a regex pattern | |
| def 'str like' [--case-insensitive(-i), pattern: string]: string -> bool { | |
| $in like ([(if $case_insensitive { '(?i)' }), $pattern] | str join) | |
| } | |
| # Open the jottfile | |
| def get-jotts []: nothing -> table<id: int, note: string, category: string> { | |
| if not ($jottfile | path dirname | path exists) { | |
| mkdir ($jottfile | path dirname) | |
| } | |
| if not ($jottfile | path exists) { '[]' | save --raw $jottfile } | |
| open --raw $jottfile | from json | |
| } | |
| # List jotts | |
| def 'main list' [--category(-c): string, --case-insensitive(-i), query?: string]: nothing -> nothing { | |
| # Open jottfile and filter msgs based on optional regex string | |
| get-jotts | |
| | if $category != null { where { $in.category == ($category | str downcase) } } else {} | |
| | if $query != null { where { $in.note | str like --case-insensitive=$case_insensitive $query } } else {} | |
| | if ($in | is-empty) { | |
| print 'No jotts found!' | |
| exit 1 | |
| } else { | |
| let data = $in | group-by --to-table category | sort-by category | |
| # List jotts without a category above everything else | |
| $data | where category == '' | |
| | if $in != [] { | |
| print 'Uncategorized jotts:' | |
| $data | where category == '' | |
| | get items.0 | sort-by id | |
| | each {|jott| | |
| print $' - [($jott.id)] ($jott.note)' | |
| } | |
| } | |
| # Group jotts by category (sorted) and print them (sorted again by id) | |
| $data | where category != '' | |
| | each {|x| | |
| print $'($x.category):' | |
| $x.items | sort-by id | each {|jott| | |
| print $' - [($jott.id)] ($jott.note)' | |
| } | |
| } | |
| } | |
| | ignore | |
| } | |
| # Create a new jott | |
| def 'main new' [--category(-c): string, ...msg: string] { | |
| let category = $category | default '' | str downcase | |
| let msg = $msg | str join ' ' | str trim | ansi strip | |
| if ($msg | is-empty) { | |
| print 'Message cannot be empty' | |
| exit 2 | |
| } | |
| let jotts = get-jotts | |
| let possible_ids = $jotts.id? | default -e [-1] | math max | seq 0 ($in + 1) | |
| let new_id = $possible_ids | where $it not-in $jotts.id | math min | |
| $jotts | append {id: $new_id, note: $msg, category: $category} | collect { save -f $jottfile } | |
| } | |
| # Delete a jott by its id | |
| def 'main remove' [...id: int] { | |
| if ($id | is-empty) { | |
| print 'No ids given' | |
| exit 2 | |
| } | |
| get-jotts | |
| | where id not-in $id | |
| | collect { save -f $jottfile } | |
| } | |
| alias 'main rm' = main remove | |
| alias 'main ls' = main list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment