@@ -71,32 +71,41 @@ def create_tooltip_text(
7171
7272 return tooltip
7373
74-
75- def write_output (track , artist , playing , player , tooltip_text ):
76- logger .info ("Writing output" )
77-
74+ def format_artist_track (artist , track , playing , max_length ):
7875 # Use the appropriate prefix based on playback status
7976 prefix = prefix_playing if playing else prefix_paused
80- max_length = max_length_module
81-
82- # Calculate the total length and truncate track if necessary
83- total_length = len (track ) + len (artist )
84- if total_length > max_length :
85- available_length = max (0 , max_length - len (artist ))
86- track = (
87- f"{ track [:available_length ]} …" if len (track ) > available_length else track
88- )
77+ prefix_separator = " "
78+ separator = " "
79+ full_length = len (artist + track )
8980
90- # Generate the "text" based on the presence of track and artist
9181 if track and not artist :
92- output_text = f"{ prefix } <b>{ track } </b>"
82+ if len (track ) != track [:max_length ]:
83+ track = track [:max_length ].rstrip () + "…"
84+ output_text = f"{ prefix } { prefix_separator } <b>{ track } </b>"
9385 elif track and artist :
94- output_text = f"{ prefix } <i>{ artist } </i> <b>{ track } </b>"
86+ if full_length > max_length :
87+ # proportion how to share max length between track and artist
88+ artist_weight = 0.65
89+ track_weight = 1 - artist_weight
90+ artist_limit = int (max_length * artist_weight )
91+ track_limit = int (max_length * track_weight )
92+
93+ if len (artist ) != artist [:artist_limit ]:
94+ artist = artist [:artist_limit ].rstrip () + "…"
95+ if len (track ) != track [:track_limit ]:
96+ track = track [:track_limit ].rstrip () + "…"
97+
98+ output_text = f"{ prefix } { prefix_separator } <i>{ artist } </i>{ separator } <b>{ track } </b>"
9599 else :
96100 output_text = "<b>Nothing playing</b>"
101+ return output_text
102+
103+
104+ def write_output (track , artist , playing , player , tooltip_text ):
105+ logger .info ("Writing output" )
97106
98107 output_data = {
99- "text" : output_text ,
108+ "text" : format_artist_track ( artist , track , playing , max_length_module ) ,
100109 "class" : "custom-" + player .props .player_name ,
101110 "alt" : player .props .player_name ,
102111 "tooltip" : tooltip_text ,
0 commit comments