From 51b66eff3385719fac07227551281d1d2a2bd915 Mon Sep 17 00:00:00 2001 From: NonlinearFruit <1123benji5813@gmail.com> Date: Sat, 8 Jun 2024 06:32:31 -0600 Subject: [PATCH] Better chat --- bashrc.sh | 2 +- scripts/chat | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/bashrc.sh b/bashrc.sh index 920c957..5a38f76 100644 --- a/bashrc.sh +++ b/bashrc.sh @@ -3,7 +3,7 @@ # Scripts if [ -d ~/scripts ]; then - export PATH=$PATH:~/scripts + export PATH="~/scripts:$PATH" fi # Exit if this shell should not be interative diff --git a/scripts/chat b/scripts/chat index f3e9479..8aed56a 100755 --- a/scripts/chat +++ b/scripts/chat @@ -7,22 +7,30 @@ def main [ --read # Read most recent ] { if $read { - read-most-recent-message + read-most-recent-message $user } else { post-message $msg $user } } -def read-most-recent-message [] { +def read-most-recent-message [user] { read-file - | last - | $"($in.message) -($in.user)" + | where $it.user == $user + | if ($in | is-empty) { + "" + } else { + last + | if $in.user != null { + $"($in.message) -($in.user)" + } else { + $in.message + } + } } def post-message [msg user] { if $msg == null or ($msg | is-empty) { - print $"(ansi red)No message provided(ansi reset)" - exit 1 + error "No message provided" } read-file @@ -35,15 +43,28 @@ def post-message [msg user] { } def read-file [] { - let file = "~/.chat-history" + let file = (filepath) touch $file open $file | from yaml - | sort-by time + | if ($in | is-empty) { + [] + } else { + sort-by time + } } def save-file [] { to yaml - | save -f "~/.chat-history" + | save -f (filepath) } +def filepath [] { + "~/.chat-history" + | path expand +} + +def error [msg] { + print $"(ansi red)($msg)(ansi reset)" + exit 1 +}