Skip to content

Commit 3afb96a

Browse files
committed
Highlight short ptop lines all the way to the end
Doing multiple crops on the same ANSI decorated line puts reset characters in it, which made us inverting it later fail.
1 parent 5316683 commit 3afb96a

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

px/px_terminal.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def to_screen_lines(procs, # type: List[px_process.PxProcess]
294294
max_cpu_time = proc.cpu_time_seconds
295295
max_cpu_time_s = proc.cpu_time_s
296296

297-
for proc in procs:
297+
for line_number, proc in enumerate(procs):
298298
cpu_percent_s = proc.cpu_percent_s
299299
if proc.cpu_percent_s == "0%":
300300
cpu_percent_s = faint(cpu_percent_s.rjust(cpu_width))
@@ -317,19 +317,13 @@ def to_screen_lines(procs, # type: List[px_process.PxProcess]
317317
memory_percent_s, proc.cmdline)
318318

319319
cropped = line
320-
if columns is not None:
320+
if row_to_highlight == line_number:
321+
cropped = get_string_of_length(cropped, columns)
322+
cropped = inverse_video(cropped)
323+
elif columns is not None:
321324
cropped = crop_ansi_string_at_length(line, columns)
322325
lines.append(cropped)
323326

324-
if row_to_highlight is not None:
325-
# The "+ 1" here is to skip the heading line
326-
highlighted = lines[row_to_highlight + 1]
327-
highlighted = get_string_of_length(highlighted, columns)
328-
highlighted = inverse_video(highlighted)
329-
330-
# The "+ 1" here is to skip the heading line
331-
lines[row_to_highlight + 1] = highlighted
332-
333327
lines[0] = heading_line
334328

335329
return lines
@@ -396,14 +390,14 @@ def get_string_of_length(string, length):
396390
if length is None:
397391
return string
398392

399-
incoming_length = visual_length(string)
400-
if incoming_length == length:
393+
initial_length = visual_length(string)
394+
if initial_length == length:
401395
return string
402396

403-
if incoming_length < length:
404-
return string + u' ' * (length - incoming_length)
397+
if initial_length < length:
398+
return string + u' ' * (length - initial_length)
405399

406-
if incoming_length > length:
400+
if initial_length > length:
407401
return crop_ansi_string_at_length(string, length)
408402

409403
assert False # How did we end up here?

tests/px_terminal_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,19 @@ def test_to_screen_lines_unicode():
6565

6666

6767
def test_get_string_of_length():
68+
CSI = u"\x1b["
69+
6870
assert px_terminal.get_string_of_length("12345", 3) == "123"
6971
assert px_terminal.get_string_of_length("12345", 5) == "12345"
7072
assert px_terminal.get_string_of_length("12345", 7) == "12345 "
7173
assert px_terminal.get_string_of_length("12345", None) == "12345"
7274

75+
# Test with escaped strings
76+
mid_bold = "1" + px_terminal.bold("234") + "5"
77+
assert px_terminal.get_string_of_length(mid_bold, 6) == mid_bold + " "
78+
assert px_terminal.get_string_of_length(mid_bold, 3) == \
79+
"1" + CSI + "1m23" + CSI + "0m"
80+
7381

7482
def test_crop_ansi_string_at_length():
7583
CSI = u"\x1b["

0 commit comments

Comments
 (0)