10000 remotemessage: simplify implementation by leveraging VimTextEdit · ag-python-qt/git-cola@d6f466f · GitHub
[go: up one dir, main page]

Skip to content

Commit d6f466f

Browse files
committed
remotemessage: simplify implementation by leveraging VimTextEdit
VimTextEdit already allows opening links using right-click actions. We can drop all of the regexes and url escaping by leveraging the pre-existing class. Signed-off-by: David Aguilar <davvid@gmail.com>
1 parent 516b964 commit d6f466f

File tree

1 file changed

+24
-55
lines changed

1 file changed

+24
-55
lines changed

cola/widgets/remotemessage.py

Lines changed: 24 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,35 @@
1-
from __future__ import division, absolute_import, unicode_literals
2-
import re
3-
41
from qtpy.QtCore import Qt
52

63
from .. import qtutils
74
from ..i18n import N_
85
from . import defs
96
from . import standard
10-
11-
12-
URL_REGEX = re.compile(
13-
'https?://' # Protocol
14-
'([^/]+(:[^/]+)?@)?' # Optional authentication
15-
'[\\w.-]+' # Domain name
16-
'(:[0-9]+)?' # Optional port
17-
'(/[^\\s]*)?'
18-
) # Optional path
19-
20-
21-
def to_link(url):
22-
return qtutils.link(url, url)
23-
24-
25-
def escape(string):
26-
"""Escapes all occurrences of '<' and '>'"""
27-
return string.replace('<', '&lt;').replace('>', '&gt;')
28-
29-
30-
def format_raw(string, start, end, offset=0):
31-
"""Replace a part of a string and transform it to a link"""
32-
chars = list(string)
33-
chars[start + offset : end + offset] = to_link(
34-
string[start + offset : end + offset]
35-
)
36-
result = ''.join(chars)
37-
return result, offset + len(result) - len(string)
38-
39-
40-
def format_links(string):
41-
"""Transform all links into HTML links"""
42-
offset = 0
43-
for url_match in URL_REGEX.finditer(string):
44-
string, offset = format_raw(string, url_match.start(), url_match.end(), offset)
45-
return string
7+
from . import text
468

479

4810
def show(context, message):
4911
"""Display a window if the remote sent a message"""
50-
message_lines = escape(message).split('\n')
51-
# Get lines starting with 'remote: '
52-
remote_lines = [line[8:] for line in message_lines if line.startswith('remote: ')]
53-
if remote_lines:
54-
# Transform new lines to HTML '<br>' and render all white spaces
55-
remote_message = (
56-
'<body style="white-space: pre">' + '<br>'.join(remote_lines) + '</body>'
57-
)
58-
view = RemoteMessage(context, format_links(remote_message), parent=context.view)
12+
if message:
13+
view = RemoteMessage(context, message, parent=context.view)
5914
view.show()
15+
view.exec_()
6016

6117

6218
def with_context(context):
63-
"""Helper function to pass to the `result` argument of RunTask.start"""
19+
"""Return a closure for the `result` callback from RunTask.start()"""
6420

65-
def wrapper(raw_message):
66-
return show(context, raw_message[2])
21+
def show_result(result):
22+
"""Display the asynchronous "result" when remote tasks complete"""
23+
_, out, err = result
24+
output = '\n\n'.join((x for x in (out, err) if x))
25+
if output:
26+
message = N_('Right-click links to open:') + '\n\n' + output
27+
else:
28+
message = output
6729

68-
return wrapper
30+
return show(context, message)
31+
32+
return show_result
6933

7034

7135
class RemoteMessage(standard.Dialog):
@@ -76,19 +40,24 @@ def __init__(self, context, message, parent=None):
7640
self.context = context
7741
self.model = context.model
7842

79-
self.setWindowTitle(N_('Remote message'))
43+
self.setWindowTitle(N_('Remote Messages'))
8044
if parent is not None:
8145
self.setWindowModality(Qt.WindowModal)
8246

83-
self.text = qtutils.textbrowser(text=message)
47+
self.text = text.VimTextEdit(context, parent=self)
48+
self.text.set_value(message)
8449
# Set a monospace font, as some remote git messages include ASCII art
8550
self.text.setFont(qtutils.default_monospace_font())
8651

8752
self.close_button = qtutils.close_button()
8853
self.close_button.setDefault(True)
8954

55+
self.bottom_layout = qtutils.hbox(
56+
defs.no_margin, defs.button_spacing, qtutils.STRETCH, self.close_button
57+
)
58+
9059
self.main_layout = qtutils.vbox(
91-
defs.no_margin, defs.spacing, self.text, self.close_button
60+
defs.no_margin, defs.spacing, self.text, self.bottom_layout
9261
)
9362
self.setLayout(self.main_layout)
9463

0 commit comments

Comments
 (0)
0