@@ -340,27 +340,117 @@ shorten\_string
340340
341341``` python
342342shorten_string(
343- content : str , max_length: int , * , sep: str = " ..."
343+ text : str , max_length: int | None , * , sep: str = " ..."
344344) -> str
345345```
346346
347- Return a string at most max\_ length characters long by removing the middle of the string .
347+ Return a string at most max\_ length characters long by removing the middle.
348348
349349<Accordion title = " Source code in rigging/util.py" icon = " code" >
350350``` python
351- def shorten_string (content : str , max_length : int , * , sep : str = " ..." ) -> str :
351+ def shorten_string (text : str , max_length : int | None , * , sep : str = " ..." ) -> str :
352352 """
353- Return a string at most max_length characters long by removing the middle of the string .
353+ Return a string at most max_length characters long by removing the middle.
354354 """
355- if len (content) <= max_length:
356- return content
355+ if max_length is None or len (text) <= max_length:
356+ return text
357+ return shorten_text(text, max_chars = max_length, separator = sep)
358+ ```
357359
358- remaining = max_length - len (sep)
359- if remaining <= 0 :
360- return sep
361360
362- middle = remaining // 2
363- return content[:middle] + sep + content[- middle:]
361+ </Accordion >
362+
363+ shorten\_ text
364+ -------------
365+
366+ ``` python
367+ shorten_text(
368+ text: str ,
369+ * ,
370+ max_lines: int | None = None ,
371+ max_chars: int | None = None ,
372+ separator: str = " ..." ,
373+ ) -> str
374+ ```
375+
376+ Shortens text to a maximum number of lines and/or characters by removing
377+ content from the middle.
378+
379+ Line shortening is applied first, followed by character shortening.
380+
381+ ** Parameters:**
382+
383+ * ** ` text ` **
384+ (` str ` )
385+ –The string to shorten.
386+ * ** ` max_lines ` **
387+ (` int | None ` , default:
388+ ` None `
389+ )
390+ –The maximum number of lines to allow.
391+ * ** ` max_chars ` **
392+ (` int | None ` , default:
393+ ` None `
394+ )
395+ –The maximum number of characters to allow.
396+ * ** ` separator ` **
397+ (` str ` , default:
398+ ` '...' `
399+ )
400+ –The separator to insert in the middle of the shortened text.
401+
402+ ** Returns:**
403+
404+ * ` str `
405+ –The shortened text
406+
407+ <Accordion title = " Source code in rigging/util.py" icon = " code" >
408+ ``` python
409+ def shorten_text (
410+ text : str ,
411+ * ,
412+ max_lines : int | None = None ,
413+ max_chars : int | None = None ,
414+ separator : str = " ..." ,
415+ ) -> str :
416+ """
417+ Shortens text to a maximum number of lines and/or characters by removing
418+ content from the middle.
419+
420+ Line shortening is applied first, followed by character shortening.
421+
422+ Args:
423+ text: The string to shorten.
424+ max_lines: The maximum number of lines to allow.
425+ max_chars: The maximum number of characters to allow.
426+ separator: The separator to insert in the middle of the shortened text.
427+
428+ Returns:
429+ The shortened text
430+ """
431+ # 1 - line count first
432+ if max_lines is not None :
433+ lines = text.splitlines()
434+ if len (lines) > max_lines:
435+ remaining_lines = max_lines - 1 # leave space for the separator
436+ if remaining_lines <= 0 :
437+ text = separator # if max_lines is 1, just use the separator
438+ else :
439+ half = remaining_lines // 2
440+ start_lines = lines[:half]
441+ end_lines = lines[- (remaining_lines - half) :]
442+ text = " \n " .join([* start_lines, separator, * end_lines])
443+
444+ # 2 - character count
445+ if max_chars is not None and len (text) > max_chars:
446+ remaining_chars = max_chars - len (separator)
447+ if remaining_chars <= 0 :
448+ text = separator
449+ else :
450+ half_chars = remaining_chars // 2
451+ text = text[:half_chars] + separator + text[- half_chars:]
452+
453+ return text
364454```
365455
366456
@@ -413,26 +503,26 @@ truncate\_string
413503
414504``` python
415505truncate_string(
416- content : str , max_length: int , * , suf: str = " ..."
506+ text : str , max_length: int , * , suf: str = " ..."
417507) -> str
418508```
419509
420510Return a string at most max\_ length characters long by removing the end of the string.
421511
422512<Accordion title = " Source code in rigging/util.py" icon = " code" >
423513``` python
424- def truncate_string (content : str , max_length : int , * , suf : str = " ..." ) -> str :
514+ def truncate_string (text : str , max_length : int , * , suf : str = " ..." ) -> str :
425515 """
426516 Return a string at most max_length characters long by removing the end of the string.
427517 """
428- if len (content ) <= max_length:
429- return content
518+ if len (text ) <= max_length:
519+ return text
430520
431521 remaining = max_length - len (suf)
432522 if remaining <= 0 :
433523 return suf
434524
435- return content [:remaining] + suf
525+ return text [:remaining] + suf
436526```
437527
438528
0 commit comments