Compare commits

...

7 commits

Author SHA1 Message Date
Konstantin 6b8e8af1ee
fix demo 2023-07-04 19:56:17 -04:00
Konstantin 85629ef43f
Update README.md 2023-06-22 18:42:56 -04:00
João Paulo da Cruz dc9c6c5cdb using more posix compliant forms
Double quoting initial vars to prevent globbing and word
splitting (not a posix thing, just a safer method).

Removing `local` forms and `unset`ing variables at end of functions.

Moving `EOF` closing to column 1 since it doesn't support indentation
with tabs.
2023-06-08 21:18:58 -04:00
Konstantin da574b367b
what's different in that fork 2023-06-05 23:28:17 -04:00
Konstantin e9ddba49f0
identifier for each entry
we need to know where each entry begins, then we can work in the anki integration
2023-06-05 23:22:44 -04:00
Konstantin e48caa4c4b
Update dictpopup 2023-06-05 23:17:35 -04:00
Konstantin f9e1625e36
begin to work at the anki support
try to implement genji's modification , replacing any ocurrence of awk, grep and sed with the perl equivalent, to make it faster and portable.
2023-06-05 23:15:15 -04:00
3 changed files with 156 additions and 11 deletions

View file

@ -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.
![image](https://files.catbox.moe/dx3hf5.png)
![image](https://github.com/KonstantinDjairo/suckless_dictpopup/assets/53496273/e681f789-eac7-482b-b503-e66f8d73bb37)
## Dependencies
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:
- 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.
## 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
- [x] Completed
- [ ] Work in Progress

132
dictpopup
View file

@ -1,14 +1,134 @@
#!/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
echo "$dict_lookup" | popup
#### Settings
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
echo "$dict_lookup" | tail -n +5 | perl -pe 's/<[^>]*>//g' | popup
HTML_SUPPORT=1
ANKI_SUPPORT=0
ANKI_SEARCH_AFTER_ADD=1
####
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 "$@"

View file

@ -3,15 +3,26 @@
int main()
{
int prevseen = 0;
static char *buf = NULL;
static size_t size = 0;
long n = 1;
while(getline(&buf, &size, stdin) > 0)
{
if (n++ < 5) /* overflow possible */
if (n++ < 5)
continue;
if (strncmp(buf,"-->", 3) == 0)
{
if (!prevseen)
{
prevseen = 1;
putchar('\n');
}
else
{
prevseen = 0;
}
}
else if (buf[0] != 10)
fputs(buf, stdout);
}