Skip to content

Commit 86db432

Browse files
committed
Fix singleline highlighting
1 parent 3fca7d3 commit 86db432

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/Commands/ShowLog.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -211,29 +211,34 @@ protected function printRecordSingleline(LogRecord $record): void {
211211

212212
// and shorten the entire string to fit one line
213213
if(strlen($plain) > $this->terminalWidth) {
214-
// calculate the allowed length by adding the formatting length
215-
$formattingLength = strlen($str) - strlen($plain);
216-
217-
// make it shorter
218-
$str = substr($str, 0, $this->terminalWidth + $formattingLength);
219-
220-
// and make sure we are closing every formatting properly
221-
if(substr($str, -2) === '</')
222-
$str = substr($str, 0, -3).'</>';
223-
224-
// count to make sure we close every opened formatting tag
225-
$countOpen = count(preg_split('/<[^\/]*?>/', $str));
226-
$countClose = count(preg_split('/<\/>/', $str));
227-
228-
// check if we are odd between open and close
229-
if($countOpen > $countClose) {
230-
// make sure we didn't cut a closing tag short
231-
if(substr($str, -1) == '<')
232-
$str = substr($str, 0, -3);
214+
// split the text open according to preg match
215+
$m = [];
216+
preg_match_all('/(<(?<tag>[^\/]*?)>(?<string>.*?)<\/>|.*?)/', $str, $m, PREG_SET_ORDER);
217+
218+
// now go through this and make sure we stay short of the line length
219+
$remaining = $this->terminalWidth;
220+
$str = '';
221+
foreach($m as $match) {
222+
$sub = '';
223+
// if we found a tag, handle that
224+
if(isset($match['tag'])) {
225+
// create substring that is not longer than the remaining length
226+
$sub = substr($match['string'], 0, $remaining);
227+
// rebuild the tag
228+
$str .= '<'.$match['tag'].'>'.$sub.'</>';
229+
}
230+
else {
231+
// else simply take whatever string was found, but at most the remaining characters in length
232+
$sub = substr($match[0], 0, $remaining);
233+
$str .= $sub;
234+
}
235+
// update remaining depending on the substring we just added
236+
$remaining -= strlen($sub);
233237

234-
// append for each missing one the close tag
235-
for($i = $countClose; $i < $countOpen; $i++)
236-
$str .= '</>';
238+
if($remaining <= 0) {
239+
// should never go below 0 but we include it to be sure
240+
break;
241+
}
237242
}
238243
}
239244

0 commit comments

Comments
 (0)