d9e64344aa151f9b8e13a4f8054509a5d78bb718

Author
sinrimin <ashen@moesrc.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

-mAdd hitokoto plugin (#8422)

Diff

 1diff --git a/plugins/hitokoto/README.md b/plugins/hitokoto/README.md
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..3a8758f562755b839a6b0411159b036da2bdd029
 4--- /dev/null
 5+++ b/plugins/hitokoto/README.md
 6@@ -0,0 +1,15 @@
 7+# hitokoto plugin
 8+
 9+Displays a random quote taken from [hitokoto.cn](https://v1.hitokoto.cn/)
10+
11+Created by [Sinrimin](https://github.com/sinrimin)
12+
13+## Usage
14+
15+Add the plugin to the plugins array in your zshrc file and restart zsh:
16+
17+```zsh
18+plugins=(... hitokoto)
19+```
20+
21+Then, run `hitokoto` to get a new random quote.
22diff --git a/plugins/hitokoto/hitokoto.plugin.zsh b/plugins/hitokoto/hitokoto.plugin.zsh
23new file mode 100644
24index 0000000000000000000000000000000000000000..8646ebf3b48e9aa735b673a650d4f2c30d170933
25--- /dev/null
26+++ b/plugins/hitokoto/hitokoto.plugin.zsh
27@@ -0,0 +1,14 @@
28+if ! (( $+commands[curl] )); then
29+    echo "hitokoto plugin needs curl to work" >&2
30+    return
31+fi
32+
33+function hitokoto {
34+    emulate -L zsh
35+    Q=$(curl -s --connect-timeout 2 "https://v1.hitokoto.cn" | jq -j '.hitokoto+"\t"+.from')
36+
37+    TXT=$(echo "$Q" | awk -F '\t' '{print $1}')
38+    WHO=$(echo "$Q" | awk -F '\t' '{print $2}')
39+
40+    [[ -n "$WHO" && -n "$TXT" ]] && print -P "%F{3}${WHO}%f: “%F{5}${TXT}%f”"
41+}