2017-04-02 06:50:24 +00:00
|
|
|
local DataStorage = require("datastorage")
|
|
|
|
local Font = require("ui/font")
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local InputDialog = require("ui/widget/inputdialog")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
|
|
|
local logger = require("logger")
|
|
|
|
local util = require("ffi/util")
|
|
|
|
local _ = require("gettext")
|
|
|
|
local Screen = require("device").screen
|
|
|
|
|
|
|
|
local Terminal = WidgetContainer:new{
|
|
|
|
name = "terminal",
|
|
|
|
dump_file = util.realpath(DataStorage:getDataDir()) .. "/terminal_output.txt",
|
2017-04-14 19:12:28 +00:00
|
|
|
command = "",
|
2017-04-02 06:50:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function Terminal:init()
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Terminal:start()
|
|
|
|
self.input = InputDialog:new{
|
|
|
|
title = _("Enter a command and press \"Execute\""),
|
2017-04-14 19:12:28 +00:00
|
|
|
input = self.command,
|
2019-12-06 21:55:35 +00:00
|
|
|
para_direction_rtl = false, -- force LTR
|
2017-04-02 06:50:24 +00:00
|
|
|
text_height = Screen:getHeight() * 0.4,
|
|
|
|
input_type = "string",
|
|
|
|
buttons = {{{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(self.input)
|
|
|
|
end,
|
|
|
|
}, {
|
|
|
|
text = _("Execute"),
|
|
|
|
is_enter_default = true,
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(self.input)
|
|
|
|
self:execute()
|
|
|
|
end,
|
|
|
|
}}},
|
|
|
|
}
|
|
|
|
UIManager:show(self.input)
|
2018-03-30 10:46:36 +00:00
|
|
|
self.input:onShowKeyboard()
|
2017-04-02 06:50:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Terminal:execute()
|
2017-04-14 19:12:28 +00:00
|
|
|
self.command = self.input:getInputText()
|
2017-04-02 06:50:24 +00:00
|
|
|
UIManager:show(InfoMessage:new{
|
2017-04-03 08:16:58 +00:00
|
|
|
text = _("Executing…"),
|
2017-04-02 06:50:24 +00:00
|
|
|
timeout = 0.1,
|
|
|
|
})
|
|
|
|
UIManager:forceRePaint()
|
2017-04-14 19:12:28 +00:00
|
|
|
local std_out = io.popen(self.command)
|
|
|
|
local entries = { self.command }
|
2017-04-02 06:50:24 +00:00
|
|
|
if std_out then
|
|
|
|
while true do
|
|
|
|
local line = std_out:read()
|
|
|
|
if line == nil then break end
|
|
|
|
table.insert(entries, line)
|
|
|
|
end
|
|
|
|
std_out:close()
|
|
|
|
else
|
|
|
|
table.insert(entries, _("Failed to execute command."))
|
|
|
|
end
|
|
|
|
self:dump(entries)
|
2017-04-03 08:16:58 +00:00
|
|
|
table.insert(entries, _("Output will also be written to"))
|
2017-04-02 06:50:24 +00:00
|
|
|
table.insert(entries, self.dump_file)
|
|
|
|
UIManager:show(InfoMessage:new{
|
2017-04-29 08:38:09 +00:00
|
|
|
cface = Font:getFace("xx_smallinfofont"),
|
2017-04-02 06:50:24 +00:00
|
|
|
text = _("Command output\n") .. table.concat(entries, "\n"),
|
|
|
|
show_icon = false,
|
|
|
|
width = Screen:getWidth() * 0.8,
|
|
|
|
height = Screen:getHeight() * 0.8,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
function Terminal:dump(entries)
|
|
|
|
local content = table.concat(entries, "\n")
|
|
|
|
local file = io.open(self.dump_file, "w")
|
|
|
|
if file then
|
|
|
|
file:write(content)
|
|
|
|
file:close()
|
|
|
|
else
|
|
|
|
logger.warn("Failed to dump terminal output " .. content .. " to " .. self.dump_file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Terminal:addToMainMenu(menu_items)
|
|
|
|
menu_items.terminal = {
|
|
|
|
text = _("Terminal emulator"),
|
2018-09-04 21:55:58 +00:00
|
|
|
keep_menu_open = true,
|
2017-04-02 06:50:24 +00:00
|
|
|
callback = function()
|
|
|
|
self:start()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
return Terminal
|