From 46fc793f7ef22354dfb883aba2b004d0743db91f Mon Sep 17 00:00:00 2001
From: Andi Gerken <andi.gerken@gmail.com>
Date: Wed, 2 Mar 2022 13:38:43 +0100
Subject: [PATCH] Wrapped text to avoid overflow.

---
 src/robofish/io/file.py  | 17 ++++++++++++-----
 src/robofish/io/utils.py |  5 +++++
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/src/robofish/io/file.py b/src/robofish/io/file.py
index e70562a..526155b 100644
--- a/src/robofish/io/file.py
+++ b/src/robofish/io/file.py
@@ -16,6 +16,7 @@
 # -----------------------------------------------------------
 
 import robofish.io
+from robofish.io import utils
 from robofish.io.entity import Entity
 import h5py
 
@@ -32,7 +33,6 @@ import uuid
 import deprecation
 import types
 import warnings
-from textwrap import wrap
 
 import matplotlib as mpl
 import matplotlib.pyplot as plt
@@ -747,7 +747,7 @@ class File(h5py.File):
             fig, ax = plt.subplots(1, 1, figsize=figsize)
 
         if self.path is not None:
-            ax.set_title("\n".join(wrap(Path(self.path).name, width=35)))
+            ax.set_title(utils.wrap_newline(Path(self.path).name, width=35))
 
         ax.set_xlim(-x_world / 2, x_world / 2)
         ax.set_ylim(-y_world / 2, y_world / 2)
@@ -918,7 +918,7 @@ class File(h5py.File):
         self.view_size = np.max([options["view_size"], min_view + options["margin"]])
 
         def get_host_and_filename() -> str:
-            return f"{socket.gethostname()}:{self.path}\n\n"
+            return f"{socket.gethostname()}:{str(self.path).strip()}\n\n"
 
         def get_text_datasets(file_frame: int) -> str:
             """Search for datasets containing text for displaying it in the video"""
@@ -942,9 +942,16 @@ class File(h5py.File):
             separator = "-" * 130 + "\n"
             text = ""
             text += separator
-            text += f"experiment_setup:\n{self.attrs['experiment_setup']}\n"
+            text += "experiment_setup:\n%s\n" % utils.wrap_newline(
+                self.attrs["experiment_setup"], 60
+            )
             text += separator
-            text += "\n".join([f"{key}: {val}" for key, val in attrs.items()])
+            text += "\n".join(
+                [
+                    "%s: %s" % (key, utils.wrap_newline(val, 60))
+                    for key, val in attrs.items()
+                ]
+            )
             return text
 
         def get_goal(file_frame: int) -> Optional[np.ndarray]:
diff --git a/src/robofish/io/utils.py b/src/robofish/io/utils.py
index e1349cd..1f704fb 100644
--- a/src/robofish/io/utils.py
+++ b/src/robofish/io/utils.py
@@ -3,6 +3,7 @@ import numpy as np
 from typing import Union, Iterable
 from pathlib import Path
 from tqdm import tqdm
+import textwrap
 
 
 def np_array(*arrays):
@@ -120,3 +121,7 @@ def get_all_data_from_paths(
         all_data.append(data_from_files)
     pbar.close()
     return all_data, expected_settings
+
+
+def wrap_newline(text, width=60):
+    return "\n".join(textwrap.wrap(str(text), width))
-- 
GitLab