@@ -694,24 +694,41 @@ class _DialogList(Generic[_T]):
694
694
Common code for `RadioList` and `CheckboxList`.
695
695
"""
696
696
697
- open_character : str = ""
698
- close_character : str = ""
699
- container_style : str = ""
700
- default_style : str = ""
701
- selected_style : str = ""
702
- checked_style : str = ""
703
- multiple_selection : bool = False
704
- show_scrollbar : bool = True
705
-
706
697
def __init__ (
707
698
self ,
708
699
values : Sequence [tuple [_T , AnyFormattedText ]],
709
700
default_values : Sequence [_T ] | None = None ,
701
+ select_on_focus : bool = False ,
702
+ open_character : str = "" ,
703
+ select_character : str = "*" ,
704
+ close_character : str = "" ,
705
+ container_style : str = "" ,
706
+ default_style : str = "" ,
707
+ number_style : str = "" ,
708
+ selected_style : str = "" ,
709
+ checked_style : str = "" ,
710
+ multiple_selection : bool = False ,
711
+ show_scrollbar : bool = True ,
712
+ show_cursor : bool = True ,
713
+ show_numbers : bool = False ,
710
714
) -> None :
711
715
assert len (values ) > 0
712
716
default_values = default_values or []
713
717
714
718
self .values = values
719
+ self .show_numbers = show_numbers
720
+
721
+ self .open_character = open_character
722
+ self .select_character = select_character
723
+ self .close_character = close_character
724
+ self .container_style = container_style
725
+ self .default_style = default_style
726
+ self .number_style = number_style
727
+ self .selected_style = selected_style
728
+ self .checked_style = checked_style
729
+ self .multiple_selection = multiple_selection
730
+ self .show_scrollbar = show_scrollbar
731
+
715
732
# current_values will be used in multiple_selection,
716
733
# current_value will be used otherwise.
717
734
keys : list [_T ] = [value for (value , _ ) in values ]
@@ -736,10 +753,14 @@ def __init__(
736
753
@kb .add ("up" )
737
754
def _up (event : E ) -> None :
738
755
self ._selected_index = max (0 , self ._selected_index - 1 )
756
+ if select_on_focus :
757
+ self ._handle_enter ()
739
758
740
759
@kb .add ("down" )
741
760
def _down (event : E ) -> None :
742
761
self ._selected_index = min (len (self .values ) - 1 , self ._selected_index + 1 )
762
+ if select_on_focus :
763
+ self ._handle_enter ()
743
764
744
765
@kb .add ("pageup" )
745
766
def _pageup (event : E ) -> None :
@@ -774,9 +795,22 @@ def _find(event: E) -> None:
774
795
self ._selected_index = self .values .index (value )
775
796
return
776
797
798
+ numbers_visible = Condition (lambda : self .show_numbers )
799
+
800
+ for i in range (1 , 10 ):
801
+
802
+ @kb .add (str (i ), filter = numbers_visible )
803
+ def _select_i (event : E , index : int = i ) -> None :
804
+ self ._selected_index = min (len (self .values ) - 1 , index - 1 )
805
+ if select_on_focus :
806
+ self ._handle_enter ()
807
+
777
808
# Control and window.
778
809
self .control = FormattedTextControl (
779
- self ._get_text_fragments , key_bindings = kb , focusable = True
810
+ self ._get_text_fragments ,
811
+ key_bindings = kb ,
812
+ focusable = True ,
813
+ show_cursor = show_cursor ,
780
814
)
781
815
782
816
self .window = Window (
@@ -831,13 +865,19 @@ def mouse_handler(mouse_event: MouseEvent) -> None:
831
865
result .append (("[SetCursorPosition]" , "" ))
832
866
833
867
if checked :
834
- result .append ((style , "*" ))
868
+ result .append ((style , self . select_character ))
835
869
else :
836
870
result .append ((style , " " ))
837
871
838
872
result .append ((style , self .close_character ))
839
- result .append ((self .default_style , " " ))
840
- result .extend (to_formatted_text (value [1 ], style = self .default_style ))
873
+ result .append ((f"{ style } { self .default_style } " , " " ))
874
+
875
+ if self .show_numbers :
876
+ result .append ((f"{ style } { self .number_style } " , f"{ i + 1 :2d} . " ))
877
+
878
+ result .extend (
879
+ to_formatted_text (value [1 ], style = f"{ style } { self .default_style } " )
880
+ )
841
881
result .append (("" , "\n " ))
842
882
843
883
# Add mouse handler to all fragments.
@@ -858,25 +898,44 @@ class RadioList(_DialogList[_T]):
858
898
:param values: List of (value, label) tuples.
859
899
"""
860
900
861
- open_character = "("
862
- close_character = ")"
863
- container_style = "class:radio-list"
864
- default_style = "class:radio"
865
- selected_style = "class:radio-selected"
866
- checked_style = "class:radio-checked"
867
- multiple_selection = False
868
-
869
901
def __init__ (
870
902
self ,
871
903
values : Sequence [tuple [_T , AnyFormattedText ]],
872
904
default : _T | None = None ,
905
+ show_numbers : bool = False ,
906
+ select_on_focus : bool = False ,
907
+ open_character : str = "(" ,
908
+ select_character : str = "*" ,
909
+ close_character : str = ")" ,
910
+ container_style : str = "class:radio-list" ,
911
+ default_style : str = "class:radio" ,
912
+ selected_style : str = "class:radio-selected" ,
913
+ checked_style : str = "class:radio-checked" ,
914
+ number_style : str = "class:radio-number" ,
915
+ multiple_selection : bool = False ,
916
+ show_cursor : bool = True ,
873
917
) -> None :
874
918
if default is None :
875
919
default_values = None
876
920
else :
877
921
default_values = [default ]
878
922
879
- super ().__init__ (values , default_values = default_values )
923
+ super ().__init__ (
924
+ values ,
925
+ default_values = default_values ,
926
+ select_on_focus = select_on_focus ,
927
+ show_numbers = show_numbers ,
928
+ open_character = open_character ,
929
+ select_character = select_character ,
930
+ close_character = close_character ,
931
+ container_style = container_style ,
932
+ default_style = default_style ,
933
+ selected_style = selected_style ,
934
+ checked_style = checked_style ,
935
+ number_style = number_style ,
936
+ multiple_selection = False ,
937
+ show_cursor = show_cursor ,
938
+ )
880
939
881
940
882
941
class CheckboxList (_DialogList [_T ]):
@@ -886,13 +945,30 @@ class CheckboxList(_DialogList[_T]):
886
945
:param values: List of (value, label) tuples.
887
946
"""
888
947
889
- open_character = "["
890
- close_character = "]"
891
- container_style = "class:checkbox-list"
892
- default_style = "class:checkbox"
893
- selected_style = "class:checkbox-selected"
894
- checked_style = "class:checkbox-checked"
895
- multiple_selection = True
948
+ def __init__ (
949
+ self ,
950
+ values : Sequence [tuple [_T , AnyFormattedText ]],
951
+ default_values : Sequence [_T ] | None = None ,
952
+ open_character : str = "[" ,
953
+ select_character : str = "*" ,
954
+ close_character : str = "]" ,
955
+ container_style : str = "class:checkbox-list" ,
956
+ default_style : str = "class:checkbox" ,
957
+ selected_style : str = "class:checkbox-selected" ,
958
+ checked_style : str = "class:checkbox-checked" ,
959
+ ) -> None :
960
+ super ().__init__ (
961
+ values ,
962
+ default_values = default_values ,
963
+ open_character = open_character ,
964
+ select_character = select_character ,
965
+ close_character = close_character ,
966
+ container_style = container_style ,
967
+ default_style = default_style ,
968
+ selected_style = selected_style ,
969
+ checked_style = checked_style ,
970
+ multiple_selection = True ,
971
+ )
896
972
897
973
898
974
class Checkbox (CheckboxList [str ]):
0 commit comments