awesome-wm-widgets/translate-widget/translate.lua

202 lines
6.2 KiB
Lua
Raw Permalink Normal View History

2017-12-07 23:31:07 +01:00
-------------------------------------------------
2017-12-27 19:30:56 +01:00
-- Translate Widget based on the Yandex.Translate API
-- https://tech.yandex.com/translate/
2017-12-07 23:31:07 +01:00
-- @author Pavel Makhov
2020-04-30 16:07:50 +02:00
-- @copyright 2020 Pavel Makhov
2017-12-07 23:31:07 +01:00
-------------------------------------------------
2017-12-04 17:56:34 +01:00
local awful = require("awful")
local spawn = require("awful.spawn")
2017-12-07 23:31:07 +01:00
local capi = {keygrabber = keygrabber }
local beautiful = require("beautiful")
2017-12-07 23:31:07 +01:00
local json = require("json")
2017-12-27 19:30:56 +01:00
local naughty = require("naughty")
2017-12-04 17:56:34 +01:00
local wibox = require("wibox")
2018-04-15 01:38:32 +02:00
local gears = require("gears")
local gfs = require("gears.filesystem")
2017-12-04 17:56:34 +01:00
2020-12-02 15:24:05 +01:00
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'
2017-12-04 17:56:34 +01:00
2017-12-07 23:31:07 +01:00
--- Returns two values - string to translate and direction:
-- 'dog enfr' -> 'dog', 'en-fr'
-- @param input_string user's input which consists of
-- text to translate and direction, 'dog enfr'
2017-12-04 17:56:34 +01:00
local function extract(input_string)
local word, lang = input_string:match('^(.+)%s(%a%a%a%a)$')
2020-04-30 16:07:50 +02:00
if word ~= nil and lang ~= nil then
2017-12-04 17:56:34 +01:00
lang = lang:sub(1, 2) .. '-' .. lang:sub(3)
end
return word, lang
end
local function show_warning(message)
naughty.notify{
preset = naughty.config.presets.critical,
title = 'Translate Shell',
text = message}
2017-12-04 17:56:34 +01:00
end
local w = awful.popup {
widget = {},
visible = false,
2018-04-15 01:38:32 +02:00
border_width = 1,
maximum_width = 400,
width = 400,
2018-04-15 01:38:32 +02:00
border_color = '#66ccff',
2017-12-06 23:24:21 +01:00
ontop = true,
bg = beautiful.bg_normal,
2018-04-15 01:38:32 +02:00
shape = function(cr, width, height)
gears.shape.rounded_rect(cr, width, height, 3)
end,
2017-12-06 23:24:21 +01:00
}
awful.placement.top(w, { margins = {top = 40}})
2017-12-06 23:24:21 +01:00
2017-12-04 17:56:34 +01:00
2018-01-21 03:44:27 +01:00
--- 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, url)
2017-12-04 17:56:34 +01:00
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
2017-12-07 23:31:07 +01:00
local resp = json.decode(stdout)
w:setup {
{
{
{
{
image = ICON,
widget = wibox.widget.imagebox,
resize = false
},
2021-12-11 02:53:09 +01:00
valign = 'center',
layout = wibox.container.place,
},
{
{
id = 'src',
2020-12-02 15:24:05 +01:00
markup = '<b>' .. lang:sub(1,2) .. '</b>: <span color="#FFFFFF"> '
.. to_translate .. '</span>',
widget = wibox.widget.textbox
},
{
id = 'res',
2020-12-02 15:24:05 +01:00
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
}
2017-12-07 23:31:07 +01:00
2017-12-04 17:56:34 +01:00
w.visible = true
w:buttons(
awful.util.table.join(
2017-12-05 22:22:33 +01:00
awful.button({}, 1, function()
2020-12-02 15:24:05 +01:00
spawn.with_shell("echo '" .. resp.translations[1].translation .. "' | xclip -selection clipboard")
2017-12-04 17:56:34 +01:00
w.visible = false
end),
awful.button({}, 3, function()
2020-12-02 15:24:05 +01:00
spawn.with_shell("echo '" .. to_translate .."' | xclip -selection clipboard")
2017-12-04 17:56:34 +01:00
w.visible = false
end)
)
)
2017-12-05 22:22:33 +01:00
capi.keygrabber.run(function(_, key, event)
if event == "release" then return end
if key then
capi.keygrabber.stop()
w.visible = false
end
end)
end)
2017-12-04 17:56:34 +01:00
end
local prompt = awful.widget.prompt()
2018-01-21 22:21:36 +01:00
local input_widget = wibox {
visible = false,
2018-01-21 22:21:36 +01:00
width = 300,
height = 100,
maxmimum_width = 300,
maxmimum_height = 900,
2018-01-21 22:21:36 +01:00
ontop = true,
screen = mouse.screen,
expand = true,
bg = beautiful.bg_normal,
2018-01-21 22:38:48 +01:00
max_widget_size = 500,
2018-04-15 01:38:32 +02:00
border_width = 1,
border_color = '#66ccff',
shape = function(cr, width, height)
gears.shape.rounded_rect(cr, width, height, 3)
end,
2018-01-21 22:21:36 +01:00
}
input_widget:setup{
{
prompt,
bg = beautiful.bg_normal,
widget = wibox.container.background
},
margins = 8,
widget = wibox.container.margin
2018-01-21 22:21:36 +01:00
}
2020-12-02 15:18:16 +01:00
local function launch(user_args)
2020-12-02 15:18:16 +01:00
local args = user_args or {}
local api_key = args.api_key
local url = args.url
awful.placement.top(input_widget, { margins = {top = 40}, parent = awful.screen.focused()})
2018-01-21 22:21:36 +01:00
input_widget.visible = true
awful.prompt.run {
prompt = "<b>Translate</b>: ",
textbox = prompt.widget,
history_path = gfs.get_dir('cache') .. '/translate_history',
2018-01-21 22:21:36 +01:00
bg_cursor = '#66ccff',
exe_callback = function(text)
if not text or #text == 0 then return end
local to_translate, lang = extract(text)
if not to_translate or #to_translate==0 or not lang or #lang == 0 then
naughty.notify({
preset = naughty.config.presets.critical,
title = 'Translate Widget Error',
text = 'Language is not provided',
})
return
end
translate(to_translate, lang, api_key, url)
2018-01-21 22:21:36 +01:00
end,
done_callback = function()
input_widget.visible = false
end
}
end
2017-12-04 17:56:34 +01:00
return {
launch = launch
2017-12-04 17:56:34 +01:00
}