more improvements

This commit is contained in:
pmakhov 2017-12-05 16:22:33 -05:00
parent c2d601aab4
commit 957ebc441e
1 changed files with 31 additions and 28 deletions

View File

@ -4,6 +4,7 @@ local awful = require("awful")
local json = require("json") local json = require("json")
local https = require("ssl.https") local https = require("ssl.https")
local wibox = require("wibox") local wibox = require("wibox")
local capi = {keygrabber = keygrabber }
local api_key = secrets.translate_widget_api_key local api_key = secrets.translate_widget_api_key
local base_url = 'https://translate.yandex.net/api/v1.5/tr.json/translate' local base_url = 'https://translate.yandex.net/api/v1.5/tr.json/translate'
@ -26,37 +27,31 @@ local function urlencode(str)
return str return str
end end
local translate_widget_txt = wibox.widget {
layout = wibox.layout.flex.vertical
}
local lang_wdgt = wibox.widget{
widget = wibox.widget.textbox,
}
local to_translate_wdgt = wibox.widget{
widget = wibox.widget.textbox,
}
local translation_wdgt = wibox.widget{
widget = wibox.widget.textbox,
}
translate_widget_txt:add(lang_wdgt)
translate_widget_txt:add(to_translate_wdgt)
translate_widget_txt:add(translation_wdgt)
local translate_widget = wibox.widget { local translate_widget = wibox.widget {
{ {
image = '/usr/share/icons/Papirus-Dark/48x48/apps/gnome-translate.svg', image = '/usr/share/icons/Papirus-Dark/48x48/apps/gnome-translate.svg',
resize = false, resize = false,
widget = wibox.widget.imagebox widget = wibox.widget.imagebox
}, },
translate_widget_txt, {
{
id = 'header',
widget = wibox.widget.textbox
},
{
id = 'src',
widget = wibox.widget.textbox
},
{
id = 'res',
widget = wibox.widget.textbox
},
id = 'text',
layout = wibox.layout.flex.vertical
},
layout = wibox.layout.fixed.horizontal layout = wibox.layout.fixed.horizontal
} }
local function translate(request_string) local function translate(request_string)
local to_translate, lang = extract(request_string) local to_translate, lang = extract(request_string)
local urll = base_url .. '?lang=' .. lang .. '&text=' .. urlencode(to_translate) .. '&key=' .. api_key local urll = base_url .. '?lang=' .. lang .. '&text=' .. urlencode(to_translate) .. '&key=' .. api_key
@ -65,9 +60,9 @@ local function translate(request_string)
if (code == 200 and resp_json ~= nil) then if (code == 200 and resp_json ~= nil) then
local resp = json.decode(resp_json).text[1] local resp = json.decode(resp_json).text[1]
lang_wdgt:set_markup('<big>' .. lang.. '</big>') translate_widget.text.header:set_markup('<big>' .. lang.. '</big>')
to_translate_wdgt:set_markup('<span color="#FFFFFF"> ' .. to_translate .. '</span>') translate_widget.text.src:set_markup('<span color="#FFFFFF"> ' .. to_translate .. '</span>')
translation_wdgt:set_markup('<span color="#FFFFFF"> ' .. resp .. '</span>') translate_widget.text.res:set_markup('<span color="#FFFFFF"> ' .. resp .. '</span>')
local w = wibox { local w = wibox {
width = 300, width = 300,
@ -75,22 +70,30 @@ local function translate(request_string)
ontop = true, ontop = true,
screen = mouse.screen, screen = mouse.screen,
expand = true, expand = true,
strategy = 'min',
widget = translate_widget widget = translate_widget
} }
awful.placement.top(w, { margins = {top = 25}}) awful.placement.top(w, { margins = {top = 25}})
w.visible = true w.visible = true
w:buttons( w:buttons(
awful.util.table.join( awful.util.table.join(
awful.button({}, 1, function() awful.spawn("echo left | xclip") awful.button({}, 1, function()
awful.spawn.with_shell("echo '" .. resp .. "' | xclip -selection clipboard")
w.visible = false w.visible = false
end), end),
awful.button({}, 3, function() awful.button({}, 3, function()
awful.spawn.with_shell("echo right | xclip") awful.spawn.with_shell("echo '" .. to_translate .."' | xclip -selection clipboard")
w.visible = false w.visible = false
end) end)
) )
) )
capi.keygrabber.run(function(_, key, event)
if event == "release" then return end
if key then
capi.keygrabber.stop()
w.visible = false
end
end)
end end
end end