88import sys
99import sysconfig
1010import time
11+ import _colorize
1112
1213from ..collector import Collector
1314from ..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
0 commit comments