Terminal plugin: a few tweaks (#5985)

- Show output in a TextViewer with a monospace font.
- Include stderr, so errors are shown
- Use Trapper to allow interrupting command (and to fix
  some refresh issues)
reviewable/pr5999/r1
poire-z 4 years ago committed by GitHub
parent 8e68dc11db
commit 55d7109042
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,6 +2,8 @@ local DataStorage = require("datastorage")
local Font = require("ui/font") local Font = require("ui/font")
local InfoMessage = require("ui/widget/infomessage") local InfoMessage = require("ui/widget/infomessage")
local InputDialog = require("ui/widget/inputdialog") local InputDialog = require("ui/widget/inputdialog")
local TextViewer = require("ui/widget/textviewer")
local Trapper = require("ui/trapper")
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer") local WidgetContainer = require("ui/widget/container/widgetcontainer")
local logger = require("logger") local logger = require("logger")
@ -36,7 +38,9 @@ function Terminal:start()
is_enter_default = true, is_enter_default = true,
callback = function() callback = function()
UIManager:close(self.input) UIManager:close(self.input)
self:execute() Trapper:wrap(function()
self:execute()
end)
end, end,
}}}, }}},
} }
@ -46,32 +50,27 @@ end
function Terminal:execute() function Terminal:execute()
self.command = self.input:getInputText() self.command = self.input:getInputText()
UIManager:show(InfoMessage:new{ local wait_msg = InfoMessage:new{
text = _("Executing…"), text = _("Executing…"),
timeout = 0.1, }
}) UIManager:show(wait_msg)
UIManager:forceRePaint()
local std_out = io.popen(self.command)
local entries = { self.command } local entries = { self.command }
if std_out then local command = self.command .. " 2>&1 ; echo" -- ensure we get stderr and output something
while true do local completed, result_str = Trapper:dismissablePopen(command, wait_msg)
local line = std_out:read() if completed then
if line == nil then break end table.insert(entries, result_str)
table.insert(entries, line) self:dump(entries)
end table.insert(entries, _("Output was also written to"))
std_out:close() table.insert(entries, self.dump_file)
else else
table.insert(entries, _("Failed to execute command.")) table.insert(entries, _("Execution canceled."))
end end
self:dump(entries) UIManager:close(wait_msg)
table.insert(entries, _("Output will also be written to")) UIManager:show(TextViewer:new{
table.insert(entries, self.dump_file) title = _("Command output"),
UIManager:show(InfoMessage:new{ text = table.concat(entries, "\n"),
cface = Font:getFace("xx_smallinfofont"), justified = false,
text = _("Command output\n") .. table.concat(entries, "\n"), text_face = Font:getFace("smallinfont"),
show_icon = false,
width = Screen:getWidth() * 0.8,
height = Screen:getHeight() * 0.8,
}) })
end end

Loading…
Cancel
Save