Skip to content

Commit 5d9ffca

Browse files
committed
highlight top item
1 parent 2686bba commit 5d9ffca

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

src/textual/command.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from dataclasses import dataclass
2121
from functools import total_ordering
2222
from inspect import isclass
23+
from operator import attrgetter
2324
from time import monotonic
2425
from typing import (
2526
TYPE_CHECKING,
@@ -455,9 +456,9 @@ class CommandList(OptionList, can_focus=False):
455456
}
456457
457458
CommandList > .option-list--option-highlighted {
458-
color: $block-cursor-foreground;
459-
background: $block-cursor-background;
460-
text-style: $block-cursor-text-style;
459+
color: $block-cursor-blurred-foreground;
460+
background: $block-cursor-blurred-background;
461+
text-style: $block-cursor-blurred-text-style;
461462
}
462463
463464
CommandList:nocolor > .option-list--option-highlighted {
@@ -1014,26 +1015,12 @@ def _refresh_command_list(
10141015
commands: The commands to show in the widget.
10151016
clear_current: Should the current content of the list be cleared first?
10161017
"""
1017-
# For the moment, this is a fairly naive approach to populating the
1018-
# command list with a list of commands. Every time we add a
1019-
# new one we're nuking the list of options and populating them
1020-
# again. If this turns out to not be a great approach, we may try
1021-
# and get a lot smarter with this (ideally OptionList will grow a
1022-
# method to sort its content in an efficient way; but for now we'll
1023-
# go with "worse is better" wisdom).
1024-
highlighted = (
1025-
command_list.get_option_at_index(command_list.highlighted)
1026-
if command_list.highlighted is not None and not clear_current
1027-
else None
1028-
)
1029-
1030-
def sort_key(command: Command) -> float:
1031-
return -command.hit.score
10321018

1033-
sorted_commands = sorted(commands, key=sort_key)
1019+
sorted_commands = sorted(commands, key=attrgetter("hit.score"), reverse=True)
10341020
command_list.clear_options().add_options(sorted_commands)
1035-
if highlighted is not None and highlighted.id:
1036-
command_list.highlighted = command_list.get_option_index(highlighted.id)
1021+
1022+
if sorted_commands:
1023+
command_list.highlighted = 0
10371024

10381025
self._list_visible = bool(command_list.option_count)
10391026
self._hit_count = command_list.option_count

0 commit comments

Comments
 (0)