Skip to content

Commit

Permalink
Better chat
Browse files Browse the repository at this point in the history
  • Loading branch information
NonlinearFruit committed Jun 8, 2024
1 parent 1666a7c commit 51b66ef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bashrc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 30 additions & 9 deletions scripts/chat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit 51b66ef

Please sign in to comment.