Philipp Hörist pushed to branch master at gajim / gajim


Commits:
f182fecb by lovetox at 2022-06-27T20:02:40+02:00
refactor: Replace reduce_chars_newlines() with stdlib

- - - - -


3 changed files:

- gajim/common/helpers.py
- gajim/gtk/conversation/rows/message.py
- gajim/gtk/menus.py


Changes:

=====================================
gajim/common/helpers.py
=====================================
@@ -385,36 +385,6 @@ def sanitize_filename(filename: str) -> str:
     return filename
 
 
-def reduce_chars_newlines(text: str, max_chars: int = 0,
-                          max_lines: int = 0) -> str:
-    """
-    Cut the chars after 'max_chars' on each line and show only the first
-    'max_lines'
-
-    If any of the params is not present (None or 0) the action on it is not
-    performed
-    """
-    def _cut_if_long(string_: str) -> str:
-        if len(string_) > max_chars:
-            string_ = string_[:max_chars - 3] + '…'
-        return string_
-
-    if max_lines == 0:
-        lines = text.split('\n')
-    else:
-        lines = text.split('\n', max_lines)[:max_lines]
-    if max_chars > 0:
-        if lines:
-            lines = [_cut_if_long(e) for e in lines]
-    if lines:
-        reduced_text = '\n'.join(lines)
-        if reduced_text != text:
-            reduced_text += '…'
-    else:
-        reduced_text = ''
-    return reduced_text
-
-
 def get_contact_dict_for_account(account: str) -> dict[str, types.BareContact]:
     """
     Creates a dict of jid -> contact with all contacts of account


=====================================
gajim/gtk/conversation/rows/message.py
=====================================
@@ -18,6 +18,7 @@
 
 from datetime import datetime
 from datetime import timedelta
+import textwrap
 
 from gi.repository import Gdk
 from gi.repository import GdkPixbuf
@@ -40,7 +41,6 @@
 from gajim.common.helpers import get_group_chat_nick
 from gajim.common.helpers import get_muc_context
 from gajim.common.helpers import message_needs_highlight
-from gajim.common.helpers import reduce_chars_newlines
 from gajim.common.helpers import to_user_string
 from gajim.common.i18n import _
 from gajim.common.i18n import Q_
@@ -187,8 +187,10 @@ def __init__(self,
             if correction_original is not None:
                 self._original_text = correction_original
                 self._message_icons.set_correction_icon_visible(True)
-                original_text = reduce_chars_newlines(
-                    correction_original, max_chars=150, max_lines=10)
+                original_text = textwrap.fill(correction_original,
+                                              width=150,
+                                              max_lines=10,
+                                              placeholder='…')
                 self._message_icons.set_correction_tooltip(
                     _('Message corrected. Original message:'
                       '\n%s') % original_text)
@@ -425,8 +427,10 @@ def set_correction(self, text: str, nickname: 
Optional[str]) -> None:
         self._message_icons.set_receipt_icon_visible(False)
         self._message_icons.set_correction_icon_visible(True)
 
-        original_text = reduce_chars_newlines(
-            self._original_text, max_chars=150, max_lines=10)
+        original_text = textwrap.fill(self._original_text,
+                                      width=150,
+                                      max_lines=10,
+                                      placeholder='…')
         self._message_icons.set_correction_tooltip(
             _('Message corrected. Original message:\n%s') % original_text)
 


=====================================
gajim/gtk/menus.py
=====================================
@@ -21,6 +21,7 @@
 from typing import Optional
 from typing import Union
 
+import textwrap
 from urllib.parse import quote
 
 from gi.repository import Gtk
@@ -34,7 +35,6 @@
 from gajim.common.helpers import is_affiliation_change_allowed
 from gajim.common.helpers import is_role_change_allowed
 from gajim.common.helpers import jid_is_blocked
-from gajim.common.helpers import reduce_chars_newlines
 from gajim.common.i18n import _
 from gajim.common.i18n import get_short_lang_code
 from gajim.common.const import URIType
@@ -222,7 +222,9 @@ def get_encryption_menu(control_id: str,
 def get_conv_action_context_menu(account: str,
                                  selected_text: str
                                  ) -> Gtk.MenuItem:
-    selected_text_short = reduce_chars_newlines(selected_text, 10, 1)
+    selected_text_short = textwrap.shorten(selected_text,
+                                           width=10,
+                                           placeholder='…')
 
     action_menu_item = Gtk.MenuItem.new_with_mnemonic(
         _('_Actions for "%s"') % selected_text_short)



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/f182fecb7a2dd249545b849725bb660a29cf6773

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/f182fecb7a2dd249545b849725bb660a29cf6773
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list
Commits@gajim.org
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to