From 16bdce0c6f288701a5ad8aab79dd5b1a3bced76a Mon Sep 17 00:00:00 2001 From: GenjiFujimoto Date: Sun, 28 May 2023 13:15:50 +0200 Subject: [PATCH] Improve html dictionaries --- Makefile | 27 ++++++++---- README.md | 20 +++++---- config.h => config.def.h | 9 ++-- dictpopup | 26 +++++++++--- format_output.c | 32 +++++++++++++++ popup.c | 88 ++++++++++++++-------------------------- 6 files changed, 120 insertions(+), 82 deletions(-) rename config.h => config.def.h (59%) create mode 100644 format_output.c diff --git a/Makefile b/Makefile index 41439c9..6c2ff57 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ include config.mk -SRC = popup.c +SRC = popup.c format_output.c OBJ = $(SRC:.c=.o) -all: options popup +all: options dictpopup options: @echo popup build options: @@ -11,20 +11,33 @@ options: @echo "LDFLAGS = $(LDFLAGS)" @echo "CC = $(CC)" +config.h: + cp config.def.h config.h + .c.o: $(CC) -c $(CFLAGS) $< -popup: popup.o - $(CC) -o $@ popup.o $(LDFLAGS) +popup.o: config.h + +$(OBJ): config.h config.mk + +dictpopup: $(OBJ) + $(CC) -o format_output format_output.o + $(CC) -o popup popup.o $(LDFLAGS) + +clean: + rm -f popup $(OBJ) format_output install: all mkdir -p ${DESTDIR}${PREFIX}/bin cp -f popup ${DESTDIR}${PREFIX}/bin + cp -f dictpopup ${DESTDIR}${PREFIX}/bin + cp -f format_output ${DESTDIR}${PREFIX}/bin uninstall: rm -f ${DESTDIR}${PREFIX}/bin/popup + rm -f ${DESTDIR}${PREFIX}/bin/dictpopup + rm -f ${DESTDIR}${PREFIX}/bin/format_output -clean: - rm -f popup $(OBJ) -.PHONY: all install uninstall clean +.PHONY: all options clean install uninstall diff --git a/README.md b/README.md index cf7deb0..dd25bbf 100644 --- a/README.md +++ b/README.md @@ -5,17 +5,21 @@ This is a very lightweight program to show a popup with the translation of the s ![image](https://github.com/GenjiFujimoto/dictpopup/assets/50422430/c4a3663b-fd91-4a66-95ad-f1528071c932) ## Dependencies -xclip, sdcv +sselp (can be replaced with xclip -o), sdcv. Optional: lynx for html support ## Setup First setup [sdcv](https://github.com/Dushistov/sdcv) according to their github page. -Then compile with `make` and place `dictpopup` as well as `popup` in your PATH. +Then install with `sudo make install`. \ +Uninstall with `sudo make uninstall` ## Usage -Call `dictpopup` to translate and display the popup of the selected text. The -popup can be dismissed by clicking on it. -The styling can be changed in `config.h`, but it has the be recompiled. -There is also a xresources patch from [herbe](https://github.com/dudik/herbe) -that you could try to apply. +Call `dictpopup [html] []`.\ +If no word as an argument is supplied, the selection is used.\ +If the string `html` is supplied as the first argument, then html support will be enabled.\ +The popup can be dismissed by clicking on it. -You can also use `popup` like `popup "Hello"` for other things if you want. +The styling can be changed in `config.h` and then recompile to apply. +There is also a xresources patch from [herbe](https://github.com/dudik/herbe) +that you could try to apply if you like. + +`popup` can also be used as a standalone program to show the contents of stdin. diff --git a/config.h b/config.def.h similarity index 59% rename from config.h rename to config.def.h index 82d0d0f..c22dace 100644 --- a/config.h +++ b/config.def.h @@ -5,10 +5,13 @@ static const char *font_pattern = "Noto Sans Mono CJK JP:size=12"; static const unsigned line_spacing = 5; static const unsigned int padding = 15; -static const unsigned int width = 450; +static const unsigned int width = 480; static const unsigned int border_size = 1; static const unsigned int duration = 5; /* in seconds */ -#define DISMISS_BUTTON Button1 -#define ACTION_BUTTON Button3 +#define MIN_BORDER_DISTANCE 3 /* The minimum distance to the monitor boundary */ + +#define DISMISS_BUTTON Button1 /* left click */ +#define ACTION1_BUTTON Button4 /* scroll up */ +#define ACTION2_BUTTON Button5 /* scroll down */ diff --git a/dictpopup b/dictpopup index 7f1765d..0a53d2f 100755 --- a/dictpopup +++ b/dictpopup @@ -1,10 +1,24 @@ #!/bin/sh -# Looks up argument instead, if provided -word=${1:-$(sselp)} -[ -z $word ] && exit 1 +# Depends on: +# - sdcv +# - sselp (for selection support) +# - lynx (for html support) +# invocation: dictpopup [html] [word] +# - If no argument provided, the selection is used +# - If supplied with the html option, the dictionary output will be piped through lynx +# in order to format html as plain text -sdcv -n --utf8-output -e "$word" | tail -n +5 | sed 's|
|\n|g' | sed 's|<[^>]*>||g' | popup +# TODO: Switch between dictionaries / dictionary results. +# Implementation idea (in C): Store each dictionary entry in an array. Invoke popup with a dictionary entry. +# Increment, decrement array and reinvoke or quit according to exit code of popup -# Instead of sed also possible: -# `lynx -dump -stdin -assume_charset=UTF-8 -display_charset=UTF-8` +if [ "$1" = "html" ]; then + word=${2:-$(sselp)} + sdcv -n --utf8-output -e "$word" | format_output | + lynx -dump -stdin -assume_charset=UTF-8 -display_charset=UTF-8 | + popup +else + word=${1:-$(sselp)} + sdcv -n --utf8-output -e "$word" | format_output | popup +fi diff --git a/format_output.c b/format_output.c new file mode 100644 index 0000000..dc5ed18 --- /dev/null +++ b/format_output.c @@ -0,0 +1,32 @@ +#include +#include + +int main() +{ + int second = 0; + static char *buf = NULL; + static size_t size = 0; + ssize_t len = 0; + for (long n=1; (len = getline(&buf, &size, stdin)) > 0; n++) + { + if (n < 5) + continue; + if (len && buf[len - 1] == '\n') + buf[len - 1] = '\0'; + if (strncmp(buf,"-->", 3) == 0) + { + if (second) + { + second = 0; + } + else + { + putchar('\n'); + second = 1; + } + } + else if (buf[0] != 0) /* Don't print newline at EOF */ + puts(buf); + } + return 0; +} diff --git a/popup.c b/popup.c index 28bce91..97d5559 100644 --- a/popup.c +++ b/popup.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -14,18 +13,17 @@ #include "config.h" -/* macros */ #define MAX(A, B) ((A) > (B) ? (A) : (B)) #define MIN(A, B) ((A) < (B) ? (A) : (B)) #define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \ * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org))) //TODO: Make selection possible -//TODO: Switch between dictionaries -#define EXIT_ACTION 0 +#define EXIT_DISMISS 0 #define EXIT_FAIL 1 -#define EXIT_DISMISS 2 +#define EXIT_ACTION1 2 +#define EXIT_ACTION2 3 static Display *dpy; static Window window; @@ -78,45 +76,21 @@ int get_max_len(char *string, XftFont *font, int max_text_width) if (info.width <= max_text_width) return eol; - int temp = eol; - while (string[eol] != ' ' && eol) - --eol; + /* prefer splitting at spaces */ + /* int temp = eol; */ + /* while (string[eol] != ' ' && eol) */ + /* --eol; */ - if (eol == 0) - return temp; - else - return ++eol; -} - -void expire(int sig) -{ - XEvent event; - event.type = ButtonPress; - event.xbutton.button = (sig == SIGUSR2) ? (ACTION_BUTTON) : (DISMISS_BUTTON); - XSendEvent(dpy, window, 0, 0, &event); - XFlush(dpy); + /* if (eol == 0) */ + /* return temp; */ + /* else */ + /* return ++eol; */ + return eol; } int main() { - struct sigaction act_expire, act_ignore; - - act_expire.sa_handler = expire; - act_expire.sa_flags = SA_RESTART; - sigemptyset(&act_expire.sa_mask); - - act_ignore.sa_handler = SIG_IGN; - act_ignore.sa_flags = 0; - sigemptyset(&act_ignore.sa_mask); - - sigaction(SIGALRM, &act_expire, 0); - sigaction(SIGTERM, &act_expire, 0); - sigaction(SIGINT, &act_expire, 0); - - sigaction(SIGUSR1, &act_ignore, 0); - sigaction(SIGUSR2, &act_ignore, 0); - if (!(dpy = XOpenDisplay(0))) die("Cannot open display"); @@ -144,16 +118,11 @@ int main() static char *buf = NULL; static size_t size = 0; - ssize_t len = 0; - char *str; - for (;(len = getline(&buf, &size, stdin)) > 0;) + char *line = NULL; + while (getline(&buf, &size, stdin) > 0) { - /* Remove the trailing newline if one is present. */ - if (len && buf[len - 1] == '\n') - buf[len - 1] = '\0'; - - str=buf; - for (unsigned int eol = get_max_len(str, font, max_text_width); eol; str += eol, num_of_lines++, eol = get_max_len(str, font, max_text_width)) + line=buf; + for (unsigned int eol = get_max_len(line, font, max_text_width); eol; line += eol, num_of_lines++, eol = get_max_len(line, font, max_text_width)) { if (lines_size <= num_of_lines) { @@ -166,10 +135,13 @@ int main() if (!lines[num_of_lines]) die("malloc failed"); - strncpy(lines[num_of_lines], str, eol); + strncpy(lines[num_of_lines], line, eol); lines[num_of_lines][eol] = '\0'; } } + free(buf); + if (num_of_lines == 0) + die("stdin is empty"); unsigned int text_height = font->ascent - font->descent; unsigned int height = (num_of_lines - 1) * line_spacing + num_of_lines * text_height + 2 * padding; @@ -178,7 +150,7 @@ int main() unsigned int du; Window dw; if (!XQueryPointer(dpy, window, &dw, &dw, &x, &y, &di, &di, &du)) - die("Could not query pointer position"); + die("Could not query pointer position"); #ifdef XINERAMA XineramaScreenInfo *info; int n, i=0; @@ -193,7 +165,6 @@ int main() mh = info[i].height; mw = info[i].width; XFree(info); - } else #endif { @@ -204,8 +175,8 @@ int main() } // Correct on monitor boundary - int by = y - y_offset + height + border_size; - int rx = x - x_offset + width + border_size; + int by = y - y_offset + height + border_size + MIN_BORDER_DISTANCE; + int rx = x - x_offset + width + border_size + MIN_BORDER_DISTANCE; if (by > mh) y = MAX(y-(by-mh), 0); if (rx > mw) @@ -220,9 +191,6 @@ int main() XSelectInput(dpy, window, ExposureMask | ButtonPress); XMapWindow(dpy, window); - sigaction(SIGUSR1, &act_expire, 0); - sigaction(SIGUSR2, &act_expire, 0); - for (;;) { XEvent event; @@ -235,14 +203,18 @@ int main() XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding, (FcChar8 *)lines[i], strlen(lines[i])); } - /* else if (event.type == ButtonPress) */ else if (event.type == ButtonPress) { if (event.xbutton.button == DISMISS_BUTTON) break; - else if (event.xbutton.button == ACTION_BUTTON) + else if (event.xbutton.button == ACTION1_BUTTON) { - exit_code = EXIT_ACTION; + exit_code = EXIT_ACTION1; + break; + } + else if (event.xbutton.button == ACTION2_BUTTON) + { + exit_code = EXIT_ACTION2; break; } }