qvm-template-gui: improve displaying progress

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.
This commit is contained in:
Marek Marczykowski-Górecki 2021-04-01 22:25:48 +02:00
parent 679a23980d
commit 03a34fbedd
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 21 additions and 6 deletions

View File

@ -15,7 +15,7 @@ from . import utils
#pylint: disable=invalid-name #pylint: disable=invalid-name
BASE_CMD = ['qvm-template', '--enablerepo=*', '--yes', '--quiet'] BASE_CMD = ['qvm-template', '--enablerepo=*', '--yes']
class Template(typing.NamedTuple): class Template(typing.NamedTuple):
status: str status: str
@ -291,6 +291,19 @@ class TemplateInstallProgressDialog(
self.actions = actions self.actions = actions
self.buttonBox.hide() 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 = prefix.rsplit('\n', 1)[0]
prefix += '\n'
else:
prefix = ''
text = prefix + suffix
return text
def install(self): def install(self):
async def coro(): async def coro():
self.actions.sort() self.actions.sort()
@ -313,12 +326,14 @@ class TemplateInstallProgressDialog(
stderr=asyncio.subprocess.STDOUT, stderr=asyncio.subprocess.STDOUT,
env=envs) env=envs)
#pylint: disable=cell-var-from-loop #pylint: disable=cell-var-from-loop
status_text = ''
while True: while True:
line = await proc.stdout.readline() line = await proc.stdout.read(100)
if line == b'': if line == b'':
break break
line = line.decode('ASCII') line = line.decode('UTF-8')
self.textEdit.append(line.rstrip()) status_text = self._process_cr(status_text + line)
self.textEdit.setPlainText(status_text)
if await proc.wait() != 0: if await proc.wait() != 0:
self.buttonBox.show() self.buttonBox.show()
self.progressBar.setMaximum(100) self.progressBar.setMaximum(100)

View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>840</width>
<height>300</height> <height>260</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">