Skip to content

Commit f597f8e

Browse files
committed
Implement color management and and S
1 parent 7a43706 commit f597f8e

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

Lib/profiling/sampling/live_collector/collector.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
import sysconfig
1010
import time
11+
import _colorize
1112

1213
from ..collector import Collector
1314
from ..constants import (
@@ -120,6 +121,9 @@ def __init__(
120121
self._footer_widget = None
121122
self._help_widget = None
122123

124+
# Color mode
125+
self._can_colorize = _colorize.can_colorize()
126+
123127
def _get_common_path_prefixes(self):
124128
"""Get common path prefixes to strip from file paths."""
125129
prefixes = []
@@ -373,8 +377,12 @@ def _update_display(self):
373377
except Exception:
374378
pass
375379

376-
def _cycle_sort(self):
377-
"""Cycle through different sort modes in column order (left to right)."""
380+
def _cycle_sort(self, reverse=False):
381+
"""Cycle through different sort modes in column order.
382+
383+
Args:
384+
reverse: If True, cycle backwards (right to left), otherwise forward (left to right)
385+
"""
378386
sort_modes = [
379387
"nsamples",
380388
"sample_pct",
@@ -384,18 +392,23 @@ def _cycle_sort(self):
384392
]
385393
try:
386394
current_idx = sort_modes.index(self.sort_by)
387-
self.sort_by = sort_modes[(current_idx + 1) % len(sort_modes)]
395+
if reverse:
396+
self.sort_by = sort_modes[(current_idx - 1) % len(sort_modes)]
397+
else:
398+
self.sort_by = sort_modes[(current_idx + 1) % len(sort_modes)]
388399
except ValueError:
389400
self.sort_by = "nsamples"
390401

391402
def _setup_colors(self):
392403
"""Set up color pairs and return color attributes."""
404+
393405
A_BOLD = self.display.get_attr("A_BOLD")
394406
A_REVERSE = self.display.get_attr("A_REVERSE")
395407
A_UNDERLINE = self.display.get_attr("A_UNDERLINE")
396408
A_NORMAL = self.display.get_attr("A_NORMAL")
397409

398-
if self.display.has_colors():
410+
# Check both curses color support and _colorize.can_colorize()
411+
if self.display.has_colors() and self._can_colorize:
399412
with contextlib.suppress(Exception):
400413
# Color constants (using curses values for compatibility)
401414
COLOR_CYAN = 6
@@ -685,8 +698,11 @@ def _handle_input(self):
685698
if ch == ord("q") or ch == ord("Q"):
686699
self.running = False
687700

688-
elif ch == ord("s") or ch == ord("S"):
689-
self._cycle_sort()
701+
elif ch == ord("s"):
702+
self._cycle_sort(reverse=False)
703+
704+
elif ch == ord("S"):
705+
self._cycle_sort(reverse=True)
690706

691707
elif ch == ord("h") or ch == ord("H") or ch == ord("?"):
692708
self.show_help = not self.show_help

Lib/profiling/sampling/live_collector/widgets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,8 @@ def render(self, line, width, **kwargs):
850850
("Tachyon Profiler - Interactive Commands", A_BOLD),
851851
("", A_NORMAL),
852852
("Navigation & Display:", A_BOLD),
853-
(" s - Cycle through sort modes", A_NORMAL),
853+
(" s - Cycle through sort modes (forward)", A_NORMAL),
854+
(" S - Cycle through sort modes (backward)", A_NORMAL),
854855
(" + - Faster display refresh rate", A_NORMAL),
855856
(" - - Slower display refresh rate", A_NORMAL),
856857
("", A_NORMAL),

0 commit comments

Comments
 (0)