Skip to content

Commit 4eb05e7

Browse files
committed
_fragment: cleanup and set a floor
1 parent d308740 commit 4eb05e7

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/gptcmd/cli.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,18 @@ def _fragment(tpl: str, msg: Message) -> str:
101101
"""
102102
PLACEHOLDER = "..."
103103
MAX_LENGTH = 79
104-
width = MAX_LENGTH - len(tpl.format(msg=""))
105-
content = msg.content
106-
res = shorten(content, width=width, placeholder=PLACEHOLDER)
107-
if res == PLACEHOLDER:
108-
# This isn't a very useful representation.
109-
# Go over slightly, even if the result is a bit more awkward.
110-
res = content[:width] + "..."
111-
return tpl.format(msg=repr(res))
104+
MIN_LENGTH = 5
105+
# length of the template once the fragment is stripped out
106+
head_length = len(tpl.replace("{msg}", ""))
107+
# 2 extra chars because repr() will add surrounding quotes
108+
avail = max(MIN_LENGTH, MAX_LENGTH - head_length - 2)
109+
110+
short = shorten(msg.content, avail, placeholder=PLACEHOLDER)
111+
112+
if short == PLACEHOLDER:
113+
short = msg.content[:avail] + PLACEHOLDER
114+
115+
return tpl.format(msg=repr(short))
112116

113117
@staticmethod
114118
def _user_range_to_python_range(

0 commit comments

Comments
 (0)