Skip to content

Commit

Permalink
qvm-template-gui: improve displaying progress
Browse files Browse the repository at this point in the history
The final version of qvm-template does not display untrusted data, so it
is safe to decode UTF-8. Specifically, this shows download progress bar
done using tqdm library.
For this to work, \r needs to be properly handled, as tqdm uses it to
erase the current line to update progress status. Since QTextEdit does
not support it natively, add a simple wrapper for this job.
Alternatively, qvm-template could be modified to display
machine-readable progress info that would be passed through QT progress
widget, but it would basically duplicate the work already done by tqdm
library. We might do this at some point, though.

And also enlarge progress window default size, to avoid wrapping.
  • Loading branch information
marmarek committed Apr 2, 2021
1 parent 679a239 commit e891191
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
23 changes: 19 additions & 4 deletions qubesmanager/qvm_template_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#pylint: disable=invalid-name

BASE_CMD = ['qvm-template', '--enablerepo=*', '--yes', '--quiet']
BASE_CMD = ['qvm-template', '--enablerepo=*', '--yes']

class Template(typing.NamedTuple):
status: str
Expand Down Expand Up @@ -291,6 +291,19 @@ def __init__(self, actions):
self.actions = actions
self.buttonBox.hide()

@staticmethod
def _process_cr(text):
"""Reduce lines replaced using CR character (\r)"""
while '\r' in text:
prefix, suffix = text.rsplit('\r', 1)
if '\n' in prefix:
prefix, lastline = prefix.rsplit('\n', 1)
prefix += '\n'
else:
prefix, lastline = '', prefix
text = prefix + suffix
return text

def install(self):
async def coro():
self.actions.sort()
Expand All @@ -313,12 +326,14 @@ async def coro():
stderr=asyncio.subprocess.STDOUT,
env=envs)
#pylint: disable=cell-var-from-loop
status_text = ''
while True:
line = await proc.stdout.readline()
line = await proc.stdout.read(100)
if line == b'':
break
line = line.decode('ASCII')
self.textEdit.append(line.rstrip())
line = line.decode('UTF-8')
status_text = self._process_cr(status_text + line)
self.textEdit.setPlainText(status_text)
if await proc.wait() != 0:
self.buttonBox.show()
self.progressBar.setMaximum(100)
Expand Down
4 changes: 2 additions & 2 deletions ui/templateinstallprogressdlg.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>840</width>
<height>260</height>
</rect>
</property>
<property name="windowTitle">
Expand Down

0 comments on commit e891191

Please sign in to comment.