tree-wide: trivial code style tweaks
[git-cola.git] / cola / widgets / log.py
blob4cca28b4caf7248965fcd73d9084cfc90d365bd2
1 from __future__ import absolute_import, division, print_function, unicode_literals
2 import time
4 from qtpy import QtGui
5 from qtpy import QtWidgets
6 from qtpy.QtCore import Qt
7 from qtpy.QtCore import Signal
9 from .. import core
10 from .. import qtutils
11 from ..i18n import N_
12 from . import defs
13 from .text import VimTextEdit
16 class LogWidget(QtWidgets.QFrame):
17 """A simple dialog to display command logs."""
19 channel = Signal(object)
21 def __init__(self, context, parent=None, output=None):
22 QtWidgets.QFrame.__init__(self, parent)
24 self.output_text = VimTextEdit(context, parent=self)
25 self.highlighter = LogSyntaxHighlighter(self.output_text.document())
26 if output:
27 self.set_output(output)
28 self.main_layout = qtutils.vbox(defs.no_margin, defs.spacing, self.output_text)
29 self.setLayout(self.main_layout)
30 self.channel.connect(self.append, type=Qt.QueuedConnection)
31 self.log(N_('Right-click links to open:'))
32 self.log(' Documentation: https://git-cola.readthedocs.io/en/latest/')
33 self.log(
34 ' Keyboard Shortcuts: '
35 'https://git-cola.github.io/share/doc/git-cola/hotkeys.html\n'
38 def clear(self):
39 self.output_text.clear()
41 def set_output(self, output):
42 self.output_text.set_value(output)
44 def log_status(self, status, out, err=None):
45 msg = []
46 if out:
47 msg.append(out)
48 if err:
49 msg.append(err)
50 if status:
51 msg.append(N_('exit code %s') % status)
52 self.log('\n'.join(msg))
54 def append(self, msg):
55 """Append to the end of the log message"""
56 if not msg:
57 return
58 msg = core.decode(msg)
59 cursor = self.output_text.textCursor()
60 cursor.movePosition(cursor.End)
61 text = self.output_text
62 # NOTE: the ': ' colon-SP-SP suffix is for the syntax highlighter
63 prefix = core.decode(time.strftime('%Y-%m-%d %H:%M:%S: ')) # ISO-8601
64 for line in msg.split('\n'):
65 cursor.insertText(prefix + line + '\n')
66 cursor.movePosition(cursor.End)
67 text.setTextCursor(cursor)
69 def log(self, msg):
70 """Add output to the log window"""
71 # Funnel through a Qt queued to allow thread-safe logging from
72 # asynchronous QRunnables, filesystem notification, etc.
73 self.channel.emit(msg)
76 class LogSyntaxHighlighter(QtGui.QSyntaxHighlighter):
77 """Implements the log syntax highlighting"""
79 def __init__(self, doc):
80 QtGui.QSyntaxHighlighter.__init__(self, doc)
81 palette = QtGui.QPalette()
82 QPalette = QtGui.QPalette
83 self.disabled_color = palette.color(QPalette.Disabled, QPalette.Text)
85 def highlightBlock(self, text):
86 end = text.find(': ')
87 if end > 0:
88 self.setFormat(0, end + 1, self.disabled_color)