diff options
Diffstat (limited to 'translate-widget')
-rw-r--r-- | translate-widget/gnome-translate.svg | 12 | ||||
-rw-r--r-- | translate-widget/translate.lua | 174 |
2 files changed, 101 insertions, 85 deletions
diff --git a/translate-widget/gnome-translate.svg b/translate-widget/gnome-translate.svg new file mode 100644 index 0000000..ca02b1a --- /dev/null +++ b/translate-widget/gnome-translate.svg @@ -0,0 +1,12 @@ +<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" version="1"> + <path style="opacity:0.5" d="M 16,32 32,44 V 20 L 16,4 Z"/> + <path style="fill:#e4e4e4" d="M 6,16 C 4.892,16 4,16.892 4,18 V 42 C 4,43.108 4.892,44 6,44 H 32 V 16 Z"/> + <path style="opacity:0.2" d="M 15.403,22.554 C 16.003,22.97 16.557,23.478 17.065,24.078 L 16.165,25.186 C 15.611,24.493 15.034,23.916 14.433,23.454 L 15.403,22.554 M 15.403,26.918 C 15.957,27.333 16.488,27.795 16.996,28.303 L 16.096,29.48 C 15.587,28.834 15.033,28.28 14.433,27.818 L 15.402,26.918 M 15.679,31.697 16.788,32.459 C 16.233,34.352 15.425,36.176 14.363,37.931 L 13.185,37.03 C 14.247,35.321 15.079,33.544 15.679,31.697 M 18.934,22 20.25,22.277 C 20.158,22.738 20.019,23.177 19.835,23.593 H 25.237 V 24.839 H 19.281 L 18.657,25.878 C 18.472,26.248 18.172,26.64 17.757,27.056 L 16.925,26.087 C 17.802,25.025 18.472,23.663 18.933,22 M 18.587,26.295 H 24.959 C 24.959,27.726 24.936,29.065 24.89,30.312 H 25.791 V 31.559 H 24.821 C 24.728,32.529 24.659,33.475 24.613,34.399 H 25.652 V 35.576 H 24.475 C 24.29,37.192 23.643,38 22.535,38 22.258,38 21.866,37.954 21.358,37.862 L 21.15,36.684 C 21.75,36.777 22.212,36.823 22.535,36.823 22.858,36.823 23.066,36.407 23.159,35.576 H 17.756 C 17.941,34.329 18.102,32.99 18.241,31.559 H 17.271 V 30.312 H 18.379 C 18.472,29.019 18.541,27.68 18.587,26.295 M 19.626,30.312 H 23.574 C 23.62,29.342 23.643,28.396 23.643,27.472 H 19.834 C 19.788,28.488 19.718,29.435 19.626,30.312 M 21.358,27.819 C 21.866,28.142 22.327,28.511 22.743,28.927 L 21.912,29.966 C 21.496,29.504 21.035,29.088 20.527,28.719 L 21.358,27.818 M 23.297,34.399 C 23.389,33.429 23.459,32.482 23.505,31.559 H 19.557 C 19.465,32.575 19.349,33.521 19.211,34.399 H 23.297 M 21.15,31.905 C 21.704,32.228 22.212,32.598 22.674,33.013 L 21.773,34.052 C 21.312,33.544 20.827,33.106 20.319,32.736 L 21.15,31.905 M 13.877,23.663 V 34.537 H 12.63 V 33.359 H 11.453 V 35.16 H 10.206 V 23.662 H 13.877 M 11.453,32.182 H 12.63 V 24.84 H 11.453 V 32.182"/> + <path style="opacity:0.2" d="M 4,42 V 43 C 4,44.108 4.892,45 6,45 H 32 V 44 H 6 C 4.892,44 4,43.108 4,42 Z"/> + <path style="fill:#a3a3a3" d="M 16,32 32,44 V 20 L 16,4 Z"/> + <path style="opacity:0.2;fill:#ffffff" d="M 6,16 C 4.892,16 4,16.892 4,18 V 19 C 4,17.892 4.892,17 6,17 H 32 V 16 Z"/> + <path style="fill:#2095f2" d="M 16,4 V 32 H 42 C 43.108,32 44,31.108 44,30 V 6 C 44,4.892 43.108,4 42,4 Z"/> + <path style="fill:#ffffff" d="M 28.221,19.374 28.614,17.902 C 29.055,16.306 29.473,14.441 29.865,12.748 H 29.963 C 30.381,14.417 30.822,16.306 31.264,17.902 L 31.632,19.374 H 28.221 M 33.349,26 H 37.177 L 32.171,10 H 27.828 L 22.821,26 H 26.502 L 27.484,22.196 H 32.367 L 33.349,26"/> + <path style="opacity:0.2" d="M 44,30 C 44,31.108 43.108,32 42,32 H 16 V 33 H 42 C 43.108,33 44,32.108 44,31 Z"/> + <path style="opacity:0.2;fill:#ffffff" d="M 16,4 V 5 H 42 C 43.108,5 44,5.892 44,7 V 6 C 44,4.892 43.108,4 42,4 Z"/> +</svg> diff --git a/translate-widget/translate.lua b/translate-widget/translate.lua index b333e74..9352ac9 100644 --- a/translate-widget/translate.lua +++ b/translate-widget/translate.lua @@ -7,16 +7,17 @@ ------------------------------------------------- local awful = require("awful") +local spawn = require("awful.spawn") local capi = {keygrabber = keygrabber } -local https = require("ssl.https") +local beautiful = require("beautiful") local json = require("json") local naughty = require("naughty") local wibox = require("wibox") local gears = require("gears") local gfs = require("gears.filesystem") -local BASE_URL = 'https://translate.yandex.net/api/v1.5/tr.json/translate' -local ICON = '/usr/share/icons/Papirus-Dark/48x48/apps/gnome-translate.svg' +local TRANSLATE_CMD = [[bash -c 'curl -s -u "apikey:%s" -H "Content-Type: application/json" -d '\''{"text": ["%s"], "model_id":"%s"}'\'' "%s/v3/translate?version=2018-05-01"']] +local ICON = os.getenv("HOME") .. '/.config/awesome/awesome-wm-widgets/translate-widget/gnome-translate.svg' --- Returns two values - string to translate and direction: -- 'dog enfr' -> 'dog', 'en-fr' @@ -31,88 +32,85 @@ local function extract(input_string) return word, lang end ---- Simple url encoder - replaces spaces with '+' sign --- @param url to encode -local function urlencode(url) - if (url) then - url = string.gsub(url, " ", "+") - end - return url +local function show_warning(message) + naughty.notify{ + preset = naughty.config.presets.critical, + title = 'Translate Shell', + text = message} end -local w = wibox { - width = 300, +local w = awful.popup { + widget = {}, + visible = false, border_width = 1, + maximum_width = 400, + width = 400, border_color = '#66ccff', ontop = true, - expand = true, - bg = '#1e252c', - max_widget_size = 500, + bg = beautiful.bg_normal, shape = function(cr, width, height) gears.shape.rounded_rect(cr, width, height, 3) - end - + end, } +awful.placement.top(w, { margins = {top = 40}}) -w:setup { - { - { - image = ICON, - widget = wibox.widget.imagebox, - resize = false - }, - id = 'img', - layout = wibox.container.margin(_, 0, 0, 10) - }, - { - { - id = 'header', - widget = wibox.widget.textbox - }, - { - id = 'src', - widget = wibox.widget.textbox - }, - { - id = 'res', - widget = wibox.widget.textbox - }, - id = 'text', - layout = wibox.layout.fixed.vertical, - }, - id = 'left', - layout = wibox.layout.fixed.horizontal -} --- Main function - takes the user input and shows the widget with translation -- @param request_string - user input (dog enfr) -local function translate(to_translate, lang, api_key) - local urll = BASE_URL .. '?lang=' .. lang .. '&text=' .. urlencode(to_translate) .. '&key=' .. api_key - - local resp_json, code = https.request(urll) - if (code == 200 and resp_json ~= nil) then - local resp = json.decode(resp_json).text[1] - - w.left.text.header:set_markup('<big>' .. lang .. '</big>') - w.left.text.src:set_markup('<b>' .. lang:sub(1,2) .. '</b>: <span color="#FFFFFF"> ' .. to_translate .. '</span>') - w.left.text.res:set_markup('<b>' .. lang:sub(4) .. '</b>: <span color="#FFFFFF"> ' .. resp .. '</span>') +local function translate(to_translate, lang, api_key, url) - awful.placement.top(w, { margins = {top = 40}}) - - local h1 = w.left.text.header:get_height_for_width(w.width, w.screen) - local h2 = w.left.text.src:get_height_for_width(w.width, w.screen) - local h3 = w.left.text.res:get_height_for_width(w.width, w.screen) + local cmd = string.format(TRANSLATE_CMD, api_key, to_translate, lang, url) + spawn.easy_async(cmd, function (stdout, stderr) + if stderr ~= '' then + show_warning(stderr) + end - -- calculate height of the widget - w.height = h1 + h2 + h3 + 20 - -- try to vertically align the icon - w.left.img:set_top((h1 + h2 + h3 + 20 - 48)/2) + local resp = json.decode(stdout) + + w:setup { + { + { + { + { + image = ICON, + widget = wibox.widget.imagebox, + resize = false + }, + valigh = 'center', + layout = wibox.container.place, + }, + { + { + id = 'src', + markup = '<b>' .. lang:sub(1,2) .. '</b>: <span color="#FFFFFF"> ' .. to_translate .. '</span>', + widget = wibox.widget.textbox + }, + { + id = 'res', + markup = '<b>' .. lang:sub(4) .. '</b>: <span color="#FFFFFF"> ' .. resp.translations[1].translation .. '</span>', + widget = wibox.widget.textbox + }, + id = 'text', + layout = wibox.layout.fixed.vertical, + }, + id = 'left', + spacing = 8, + layout = wibox.layout.fixed.horizontal + }, + bg = beautiful.bg_normal, + forced_width = 400, + widget = wibox.container.background + }, + color = beautiful.bg_normal, + margins = 8, + widget = wibox.container.margin + } w.visible = true w:buttons( awful.util.table.join( awful.button({}, 1, function() - awful.spawn.with_shell("echo '" .. resp .. "' | xclip -selection clipboard") + awful.spawn.with_shell("echo '" .. resp.translations[1].translation .. "' | xclip -selection clipboard") w.visible = false end), awful.button({}, 3, function() @@ -129,40 +127,46 @@ local function translate(to_translate, lang, api_key) w.visible = false end end) - else - naughty.notify({ - preset = naughty.config.presets.critical, - title = 'Translate Widget Error', - text = resp_json, - }) - end + end) end +local prompt = awful.widget.prompt() local input_widget = wibox { + visible = false, width = 300, + height = 100, + maxmimum_width = 300, + maxmimum_height = 900, ontop = true, screen = mouse.screen, expand = true, - bg = '#1e252c', + bg = beautiful.bg_normal, max_widget_size = 500, border_width = 1, border_color = '#66ccff', shape = function(cr, width, height) gears.shape.rounded_rect(cr, width, height, 3) - end + end, } -local prompt = awful.widget.prompt() - -input_widget:setup { - layout = wibox.container.margin, - prompt, - left = 10 +input_widget:setup{ + { + prompt, + bg = beautiful.bg_normal, + widget = wibox.container.background + }, + margins = 8, + widget = wibox.container.margin } -local function show_translate_prompt(api_key) +local function launch(args) + + local args = args or {} + + local api_key = args.api_key + local url = args.url + awful.placement.top(input_widget, { margins = {top = 40}, parent = awful.screen.focused()}) - input_widget.height = 40 input_widget.visible = true awful.prompt.run { @@ -181,7 +185,7 @@ local function show_translate_prompt(api_key) }) return end - translate(to_translate, lang, api_key) + translate(to_translate, lang, api_key, url) end, done_callback = function() input_widget.visible = false @@ -190,5 +194,5 @@ local function show_translate_prompt(api_key) end return { - show_translate_prompt = show_translate_prompt + launch = launch } |