summaryrefslogtreecommitdiff
path: root/plugins/hitokoto/hitokoto.plugin.zsh
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/hitokoto/hitokoto.plugin.zsh')
-rw-r--r--plugins/hitokoto/hitokoto.plugin.zsh18
1 files changed, 11 insertions, 7 deletions
diff --git a/plugins/hitokoto/hitokoto.plugin.zsh b/plugins/hitokoto/hitokoto.plugin.zsh
index 8646ebf3b..e346d18c5 100644
--- a/plugins/hitokoto/hitokoto.plugin.zsh
+++ b/plugins/hitokoto/hitokoto.plugin.zsh
@@ -1,14 +1,18 @@
if ! (( $+commands[curl] )); then
- echo "hitokoto plugin needs curl to work" >&2
- return
+ echo "hitokoto plugin needs curl to work" >&2
+ return
fi
function hitokoto {
- emulate -L zsh
- Q=$(curl -s --connect-timeout 2 "https://v1.hitokoto.cn" | jq -j '.hitokoto+"\t"+.from')
+ setopt localoptions nopromptsubst
- TXT=$(echo "$Q" | awk -F '\t' '{print $1}')
- WHO=$(echo "$Q" | awk -F '\t' '{print $2}')
+ # Get hitokoto data
+ local -a data
+ data=("${(ps:\n:)"$(command curl -s --connect-timeout 2 "https://v1.hitokoto.cn" | command jq -j '.hitokoto+"\n"+.from')"}")
- [[ -n "$WHO" && -n "$TXT" ]] && print -P "%F{3}${WHO}%f: “%F{5}${TXT}%f”"
+ # Exit if could not fetch hitokoto
+ [[ -n "$data" ]] || return 0
+
+ local quote="${data[1]}" author="${data[2]}"
+ print -P "%F{3}${author}%f: “%F{5}${quote}%f”"
}