Compare commits
7 commits
main
...
anki_suppo
Author | SHA1 | Date | |
---|---|---|---|
6b8e8af1ee | |||
85629ef43f | |||
dc9c6c5cdb | |||
da574b367b | |||
e9ddba49f0 | |||
e48caa4c4b | |||
f9e1625e36 |
18
README.md
18
README.md
|
@ -2,12 +2,16 @@
|
||||||
|
|
||||||
This is a very lightweight program to show a popup with the dictionary entry of the selected text. It uses [sdcv](https://github.com/Dushistov/sdcv) for the dictionary lookup and optionally, a slightly modified version of [herbe](https://github.com/dudik/herbe) to display the popup.
|
This is a very lightweight program to show a popup with the dictionary entry of the selected text. It uses [sdcv](https://github.com/Dushistov/sdcv) for the dictionary lookup and optionally, a slightly modified version of [herbe](https://github.com/dudik/herbe) to display the popup.
|
||||||
|
|
||||||
![image](https://files.catbox.moe/dx3hf5.png)
|
![image](https://github.com/KonstantinDjairo/suckless_dictpopup/assets/53496273/e681f789-eac7-482b-b503-e66f8d73bb37)
|
||||||
|
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
notice that most of them might be already installed, they are:
|
notice that most of them might be already installed, they are:
|
||||||
|
|
||||||
sselp , sdcv, perl
|
`sselp , sdcv, perl`
|
||||||
|
|
||||||
|
|
||||||
|
By using Perl one-liners, we are able to replace the usage of awk, grep, and sed with Perl equivalents, making the code more consistent and eliminating the need for external tools.
|
||||||
|
|
||||||
### Currently tested on:
|
### Currently tested on:
|
||||||
- GNU/Linux
|
- GNU/Linux
|
||||||
|
@ -43,6 +47,16 @@ that you could try to apply if you like.
|
||||||
`popup` can also be used as a standalone program to show the contents of stdin.
|
`popup` can also be used as a standalone program to show the contents of stdin.
|
||||||
|
|
||||||
|
|
||||||
|
## Recommended fonts
|
||||||
|
For low-resolution monitors i recommend that font:
|
||||||
|
|
||||||
|
<p>
|
||||||
|
<img src="https://github.com/KonstantinDjairo/suckless_dictpopup/assets/53496273/a34a8a20-9e17-4915-aad4-343e76b75de8" alt>
|
||||||
|
<br><em><a href="https://github.com/KonstantinDjairo/source-han-code-jp">source-han-code-jp<a></em></br>
|
||||||
|
in this picture the font is set at 10px
|
||||||
|
</p>
|
||||||
|
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
- [x] Completed
|
- [x] Completed
|
||||||
- [ ] Work in Progress
|
- [ ] Work in Progress
|
||||||
|
|
134
dictpopup
134
dictpopup
|
@ -1,14 +1,134 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
word=${1:-$(sselp)}
|
# Invocation: dictpopup [html] [word]
|
||||||
|
# - If no argument is provided, the selection is used.
|
||||||
|
# - If supplied with the 'html' option, the dictionary output will be piped through lynx
|
||||||
|
# to format HTML as plain text.
|
||||||
|
|
||||||
dict_lookup=$(sdcv -n --utf8-output -e "$word")
|
# TODO: replace any ocurrence of xclip with sselp
|
||||||
|
# get rid of html and create a dotfile where it reads from, for example ~/.config/dictpopup
|
||||||
|
# this shall be better than the spaguethi at the bottom:
|
||||||
|
|
||||||
|
|
||||||
if echo "$dict_lookup" | perl -0777 -ne 'exit 0 if /Nothing similar to/ ; exit 1'; then
|
#### Settings
|
||||||
echo "$dict_lookup" | popup
|
readonly ANKI_DECK="${ANKI_DECK:-Japanese::sentences}"
|
||||||
|
readonly ANKI_MODEL="${ANKI_MODEL:-Japanese sentences}"
|
||||||
|
readonly ANKI_SENTENCE_FIELD="${ANKI_SENTENCE_FIELD:-SentKanji}"
|
||||||
|
readonly ANKI_WORD_FIELD="${ANKI_WORD_FIELD:-VocabKanji}"
|
||||||
|
readonly ANKI_DEFINITION_FIELD="${ANKI_DEFINITION_FIELD:-VocabDef}"
|
||||||
|
|
||||||
else
|
HTML_SUPPORT=1
|
||||||
echo "$dict_lookup" | tail -n +5 | perl -pe 's/<[^>]*>//g' | popup
|
ANKI_SUPPORT=0
|
||||||
|
ANKI_SEARCH_AFTER_ADD=1
|
||||||
|
####
|
||||||
|
|
||||||
fi
|
die() {
|
||||||
|
printf '%s\n' "$1"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
checkDependencies() {
|
||||||
|
command -v sdcv >/dev/null 2>&1 || die "sdcv not installed."
|
||||||
|
command -v popup >/dev/null 2>&1 || die "The popup binary is not accessible."
|
||||||
|
command -v xclip >/dev/null 2>&1 || die "xclip is not installed."
|
||||||
|
if ! command -v clipnotify >/dev/null 2>&1; then
|
||||||
|
echo "clipnotify not installed. Anki support disabled"
|
||||||
|
ANKI_SUPPORT=0
|
||||||
|
fi
|
||||||
|
if ! command -v lynx >/dev/null 2>&1; then
|
||||||
|
echo "Lynx not installed. HTML support disabled"
|
||||||
|
HTML_SUPPORT=0
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
__escape_json() {
|
||||||
|
text="$1"
|
||||||
|
text=${text%"$'\n'"}
|
||||||
|
text=$(printf '%s\n' "$text" | perl -pe 's/"/\\&/g; s/\t/\\t/g; s/\n/\\n/g; s/\r/\\r/g; s/\b/\\b/g; s/\f/\\f/g')
|
||||||
|
printf '%s\n' "$text"
|
||||||
|
unset text
|
||||||
|
}
|
||||||
|
|
||||||
|
__ankiconnect_request() {
|
||||||
|
curl -fsS localhost:8765 -X POST -d "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
anki_search() {
|
||||||
|
request='{
|
||||||
|
"action": "guiBrowse",
|
||||||
|
"version": 6,
|
||||||
|
"params": {
|
||||||
|
"query": "nid:<note_id>"
|
||||||
|
}
|
||||||
|
}'
|
||||||
|
__ankiconnect_request "${request#<note_id>}" >/dev/null 2>&1
|
||||||
|
unset request
|
||||||
|
}
|
||||||
|
|
||||||
|
create_anki_card() {
|
||||||
|
word=$(__escape_json "$1")
|
||||||
|
definition="$2"
|
||||||
|
# Remove the first line if it contains 【
|
||||||
|
if printf '%s\n' "$definition" | perl -ne 'print unless /【/ && !$seen++'; then
|
||||||
|
definition=$(printf '%s\n' "$definition" | tail -n +2)
|
||||||
|
fi
|
||||||
|
definition=$(__escape_json "$definition")
|
||||||
|
|
||||||
|
printf 'Please select the sentence\n'
|
||||||
|
clipnotify >/dev/null && sentence=$(xclip -o 2>/dev/null)
|
||||||
|
sentence=$(__escape_json "${sentence/$word/<b>$word</b>}")
|
||||||
|
|
||||||
|
request=$(cat <<-EOF
|
||||||
|
{
|
||||||
|
"action": "addNote",
|
||||||
|
"version": 6,
|
||||||
|
"params": {
|
||||||
|
"note": {
|
||||||
|
"deckName": "$ANKI_DECK",
|
||||||
|
"modelName": "$ANKI_MODEL",
|
||||||
|
"fields": {
|
||||||
|
"$ANKI_SENTENCE_FIELD": "$sentence",
|
||||||
|
"$ANKI_WORD_FIELD": "$word",
|
||||||
|
"$ANKI_DEFINITION_FIELD": "$definition"
|
||||||
|
},
|
||||||
|
"options": {
|
||||||
|
"allowDuplicate": true
|
||||||
|
},
|
||||||
|
"tags": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
|
||||||
|
if output=$(__ankiconnect_request "$request" 2>&1); then
|
||||||
|
note_id=$(printf '%s\n' "$output" | perl -ne 'print $1 if /"result": (\d+)/')
|
||||||
|
[ $ANKI_SEARCH_AFTER_ADD -eq 1 ] && anki_search "$note_id"
|
||||||
|
else
|
||||||
|
die "$output"
|
||||||
|
fi
|
||||||
|
unset word
|
||||||
|
unset definition
|
||||||
|
unset request
|
||||||
|
}
|
||||||
|
|
||||||
|
main() {
|
||||||
|
if [ "$1" = "html" ] && [ $HTML_SUPPORT -eq 1 ]; then
|
||||||
|
word=${2:-$(xclip -o 2>/dev/null)}
|
||||||
|
dict_entry=$(sdcv -n --utf8-output -e "$word" | perl -0777 -pe 'exit 1 if /Nothing similar to/')
|
||||||
|
dict_entry=$(printf '%s\n' "$dict_entry" | lynx -dump -stdin -assume_charset=UTF-8 -display_charset=UTF-8)
|
||||||
|
else
|
||||||
|
word=${1:-$(xclip -o 2>/dev/null)}
|
||||||
|
dict_entry=$(sdcv -n --utf8-output -e "$word" | perl -0777 -pe 'exit 1 if /Nothing similar to/')
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
printf '%s\n' "$dict_entry" | popup
|
||||||
|
[ $? -eq 2 ] && [ $ANKI_SUPPORT -eq 1 ] && create_anki_card "$word" "$dict_entry"
|
||||||
|
else
|
||||||
|
echo "Pattern not found"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
checkDependencies
|
||||||
|
main "$@"
|
||||||
|
|
|
@ -3,15 +3,26 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
int prevseen = 0;
|
||||||
static char *buf = NULL;
|
static char *buf = NULL;
|
||||||
static size_t size = 0;
|
static size_t size = 0;
|
||||||
long n = 1;
|
long n = 1;
|
||||||
while(getline(&buf, &size, stdin) > 0)
|
while(getline(&buf, &size, stdin) > 0)
|
||||||
{
|
{
|
||||||
if (n++ < 5) /* overflow possible */
|
if (n++ < 5)
|
||||||
continue;
|
continue;
|
||||||
if (strncmp(buf,"-->", 3) == 0)
|
if (strncmp(buf,"-->", 3) == 0)
|
||||||
putchar('\n');
|
{
|
||||||
|
if (!prevseen)
|
||||||
|
{
|
||||||
|
prevseen = 1;
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
prevseen = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
else if (buf[0] != 10)
|
else if (buf[0] != 10)
|
||||||
fputs(buf, stdout);
|
fputs(buf, stdout);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue