From 5b6a23f3e8f9fe98b3622140f8babdd7b1d2b4d8 Mon Sep 17 00:00:00 2001
From: Mathis Hocke <mathis.hocke@fu-berlin.de>
Date: Wed, 2 Mar 2022 18:01:50 +0100
Subject: [PATCH] Abbreviate long texts in rendering

---
 src/robofish/io/file.py  | 23 ++++++++++++++++-------
 src/robofish/io/utils.py | 15 ++++++++++++---
 2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/src/robofish/io/file.py b/src/robofish/io/file.py
index 526155b..a2eca16 100644
--- a/src/robofish/io/file.py
+++ b/src/robofish/io/file.py
@@ -902,8 +902,12 @@ class File(h5py.File):
         points = [grid_points, goal_point, target_line]
 
         if options["show_text"]:
-            label_top_right = ax_text.text(0, 0.95, "", fontsize=10)
-            label_bottom_right = ax_text.text(0, 0, "", fontsize=10)
+            label_top_right = ax_text.text(
+                0, 0.90, "", fontsize=9, fontfamily="monospace"
+            )
+            label_bottom_right = ax_text.text(
+                0, 0.1, "", fontsize=9, fontfamily="monospace"
+            )
             labels = [label_top_right, label_bottom_right]
         else:
             labels = []
@@ -935,20 +939,25 @@ class File(h5py.File):
 
         def get_attributes_as_text() -> str:
             attrs = {}
+            width = 92
             for key, val in self.attrs.items():
                 if key == "experiment_setup":
                     continue
                 attrs[key] = val
-            separator = "-" * 130 + "\n"
+            separator = "\u2500" * width + "\n"
             text = ""
             text += separator
-            text += "experiment_setup:\n%s\n" % utils.wrap_newline(
-                self.attrs["experiment_setup"], 60
+            text += "experiment_setup:\n"
+            text += utils.wrap_newline(
+                text=self.attrs["experiment_setup"],
+                width=width,
+                justify=True,
+                maxlines=10,
             )
-            text += separator
+            text += "\n" + separator
             text += "\n".join(
                 [
-                    "%s: %s" % (key, utils.wrap_newline(val, 60))
+                    f"{key}: {utils.wrap_newline(text=val, width=width-len(key)-2, justify=True, maxlines=3)}"
                     for key, val in attrs.items()
                 ]
             )
diff --git a/src/robofish/io/utils.py b/src/robofish/io/utils.py
index 1f704fb..0144f39 100644
--- a/src/robofish/io/utils.py
+++ b/src/robofish/io/utils.py
@@ -1,6 +1,6 @@
 import robofish.io
 import numpy as np
-from typing import Union, Iterable
+from typing import Union, Iterable, Optional
 from pathlib import Path
 from tqdm import tqdm
 import textwrap
@@ -123,5 +123,14 @@ def get_all_data_from_paths(
     return all_data, expected_settings
 
 
-def wrap_newline(text, width=60):
-    return "\n".join(textwrap.wrap(str(text), width))
+def wrap_newline(
+    text: str, width: int = 60, justify: bool = False, maxlines: Optional[int] = None
+) -> str:
+    text = str(text).strip()
+    if justify:
+        text = text.replace("\n", " ")
+    lines = textwrap.wrap(text, width)
+    if maxlines and len(lines) > maxlines:
+        lines = lines[:maxlines]
+        lines[-1] = lines[-1][: width - 3] + "..."
+    return "\n".join(lines)
-- 
GitLab