Browse Source

detect URLs in rich console and make them clickable

closes #421

Also closes #427 Auto-URL is confused by wide characters
pull/473/head
eladeyal 5 years ago
committed by Chris
parent
commit
26e744b52c
2 changed files with 30 additions and 4 deletions
  1. 6
      gooey/gui/components/console.py
  2. 28
      gooey/gui/components/widgets/richtextconsole.py

6
gooey/gui/components/console.py

@ -40,7 +40,11 @@ class Console(wx.Panel):
def evtUrl(self, event):
if event.MouseEvent.LeftUp():
webbrowser.open(self.textbox.GetValue()[event.URLStart:event.URLEnd])
# The rich console provides the embedded URL via GetString()
# but the basic console does not
webbrowser.open(
event.GetString() or
self.textbox.GetRange(event.URLStart,event.URLEnd))
event.Skip()

28
gooey/gui/components/widgets/richtextconsole.py

@ -1,6 +1,7 @@
import wx
import wx.richtext
import colored
import re
kColorList = ["#000000", "#800000", "#008000", "#808000", "#000080", "#800080", "#008080", "#c0c0c0",
"#808080", "#ff0000", "#00ff00", "#ffff00", "#0000ff", "#ff00ff", "#00ffff", "#ffffff", "#000000",
@ -39,6 +40,8 @@ class RichTextConsole(wx.richtext.RichTextCtrl):
def __init__(self, parent):
super(wx.richtext.RichTextCtrl, self).__init__(parent, -1, "", style=wx.richtext.RE_MULTILINE | wx.richtext.RE_READONLY)
self.regex_urls=re.compile(r'\b((?:file://|https?://|mailto:)[^][\s<>|]*)')
self.url_colour = wx.Colour(0,0,255)
self.esc = colored.style.ESC
self.end = colored.style.END
self.noop = lambda *args, **kwargs: None
@ -58,6 +61,25 @@ class RichTextConsole(wx.richtext.RichTextCtrl):
# NB : we use a default parameter to force the evaluation of the binding
self.actionsMap[escSeq] = lambda bindedColor=wxcolor: self.BeginTextColour(bindedColor)
def PreprocessAndWriteText(self, content):
"""Write text into console, while capturing URLs and making
them blue, underlined, and clickable.
"""
textStream=iter(re.split(self.regex_urls, content))
# The odd elements in textStream are plaintext;
# the even elements are URLs.
for plaintext in textStream:
url=next(textStream, None)
self.WriteText(plaintext)
if url:
self.BeginTextColour(self.url_colour)
self.BeginUnderline()
self.BeginURL(url)
self.WriteText(url)
self.EndURL()
self.EndUnderline()
self.EndTextColour()
def AppendText(self, content):
"""
wx method overriden to capture the terminal control character and translate them into wx styles.
@ -73,7 +95,7 @@ class RichTextConsole(wx.richtext.RichTextCtrl):
# Invariant : found an escape sequence starting at escPos
# NB : we flush all the characters before the escape sequence, if any
if content[unprocIndex:escPos]:
self.WriteText(content[unprocIndex:escPos])
self.PreprocessAndWriteText(content[unprocIndex:escPos])
endEsc = content.find(self.end, escPos)
if endEsc == -1:
unprocIndex = escPos + len(self.esc)
@ -82,5 +104,5 @@ class RichTextConsole(wx.richtext.RichTextCtrl):
self.actionsMap.get(content[escPos:endEsc+1], self.noop)()
unprocIndex = endEsc + 1
# Invariant : unprocessed end of buffer is escape-free, ready to be printed
self.WriteText(content[unprocIndex:])
self.ShowPosition(self.GetInsertionPoint())
self.PreprocessAndWriteText(content[unprocIndex:])
self.ShowPosition(self.GetInsertionPoint())
Loading…
Cancel
Save