[kupfer] Handle large text objects a bit better
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [kupfer] Handle large text objects a bit better
- Date: Sat, 16 Apr 2011 12:43:50 +0000 (UTC)
commit 4e84ab5bf4ffebf8e099f932e1ae048f26b67831
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date: Sat Apr 16 14:43:20 2011 +0200
Handle large text objects a bit better
In particular, we should never call .splitlines() just to find out how
many lines the text consists of. This slowed down in particular the
construction of the description string for TextLeaf.
Launchpad-bug: https://bugs.launchpad.net/bugs/762607
kupfer/obj/objects.py | 25 +++++++++++++++++++------
kupfer/plugin/clipboard.py | 3 +--
kupfer/plugin/commands.py | 2 +-
kupfer/plugin/core/selection.py | 3 +--
kupfer/plugin/devhelp.py | 2 +-
5 files changed, 23 insertions(+), 12 deletions(-)
---
diff --git a/kupfer/obj/objects.py b/kupfer/obj/objects.py
index a059814..fe78033 100644
--- a/kupfer/obj/objects.py
+++ b/kupfer/obj/objects.py
@@ -410,9 +410,8 @@ class TextLeaf (Leaf, TextRepresentation):
"""@text *must* be unicode or UTF-8 str"""
text = tounicode(text)
if not name:
- lines = [l for l in text.splitlines() if l.strip()]
- name = lines[0] if lines else text
- if len(text) == 0:
+ name = self.get_first_text_line(text)
+ if len(text) == 0 or not name:
name = _("(Empty Text)")
Leaf.__init__(self, text, name)
@@ -422,10 +421,24 @@ class TextLeaf (Leaf, TextRepresentation):
def repr_key(self):
return hash(self.object)
+ @classmethod
+ def get_first_text_line(cls, text):
+ firstline = None
+ firstnl = text.find("\n")
+ if firstnl != -1:
+ firstline = text[:firstnl].strip()
+ if not firstline:
+ splut = text.split(None, 1)
+ firstline = splut[0] if splut else text
+ else:
+ firstline = text
+ if not firstline:
+ firstline = text.strip("\n")
+ return firstline
+
def get_description(self):
- lines = [l for l in self.object.splitlines() if l.strip()]
- desc = lines[0] if lines else self.object
- numlines = len(lines) or 1
+ numlines = self.object.count("\n") + 1
+ desc = self.get_first_text_line(self.object)
# TRANS: This is description for a TextLeaf, a free-text search
# TRANS: The plural parameter is the number of lines %(num)d
diff --git a/kupfer/plugin/clipboard.py b/kupfer/plugin/clipboard.py
index 963e854..8943b7c 100644
--- a/kupfer/plugin/clipboard.py
+++ b/kupfer/plugin/clipboard.py
@@ -37,9 +37,8 @@ __kupfer_settings__ = plugin_support.PluginSettings(
class ClipboardText (TextLeaf):
def get_description(self):
- lines = self.object.splitlines()
+ numlines = max(self.object.count("\n"), 1)
desc = unicode(self)
- numlines = len(lines) or 1
return ngettext('Clipboard "%(desc)s"',
'Clipboard with %(num)d lines "%(desc)s"',
diff --git a/kupfer/plugin/commands.py b/kupfer/plugin/commands.py
index 6e6e908..c6bde6d 100644
--- a/kupfer/plugin/commands.py
+++ b/kupfer/plugin/commands.py
@@ -192,7 +192,7 @@ class CommandTextSource (TextSource):
def get_text_items(self, text):
if not text.strip():
return
- if len(text.splitlines()) > 1:
+ if '\n' in text:
return
## check for absolute path with arguments
firstwords = text.split()
diff --git a/kupfer/plugin/core/selection.py b/kupfer/plugin/core/selection.py
index a45c791..593d8ad 100644
--- a/kupfer/plugin/core/selection.py
+++ b/kupfer/plugin/core/selection.py
@@ -15,8 +15,7 @@ class SelectedText (TextLeaf):
qf_id = "selectedtext"
def __init__(self, text):
text = kupferstring.tounicode(text)
- lines = filter(None, text.splitlines())
- summary = lines[0] if lines else text
+ summary = self.get_first_text_line(text)
maxlen = 10
if len(summary) > maxlen:
summary = summary[:maxlen] + u".."
diff --git a/kupfer/plugin/devhelp.py b/kupfer/plugin/devhelp.py
index 338c3ac..1808fc4 100644
--- a/kupfer/plugin/devhelp.py
+++ b/kupfer/plugin/devhelp.py
@@ -18,7 +18,7 @@ class LookUp (Action):
yield TextLeaf
def valid_for_item(self, leaf):
text = leaf.object
- return len(text.splitlines()) <= 1
+ return '\n' not in text
def get_description(self):
return None
def get_icon_name(self):
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]