Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/prompt_toolkit/key_binding/bindings/page_navigation.py: 30%

27 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

1""" 

2Key bindings for extra page navigation: bindings for up/down scrolling through 

3long pages, like in Emacs or Vi. 

4""" 

5from __future__ import annotations 

6 

7from prompt_toolkit.filters import buffer_has_focus, emacs_mode, vi_mode 

8from prompt_toolkit.key_binding.key_bindings import ( 

9 ConditionalKeyBindings, 

10 KeyBindings, 

11 KeyBindingsBase, 

12 merge_key_bindings, 

13) 

14 

15from .scroll import ( 

16 scroll_backward, 

17 scroll_forward, 

18 scroll_half_page_down, 

19 scroll_half_page_up, 

20 scroll_one_line_down, 

21 scroll_one_line_up, 

22 scroll_page_down, 

23 scroll_page_up, 

24) 

25 

26__all__ = [ 

27 "load_page_navigation_bindings", 

28 "load_emacs_page_navigation_bindings", 

29 "load_vi_page_navigation_bindings", 

30] 

31 

32 

33def load_page_navigation_bindings() -> KeyBindingsBase: 

34 """ 

35 Load both the Vi and Emacs bindings for page navigation. 

36 """ 

37 # Only enable when a `Buffer` is focused, otherwise, we would catch keys 

38 # when another widget is focused (like for instance `c-d` in a 

39 # ptterm.Terminal). 

40 return ConditionalKeyBindings( 

41 merge_key_bindings( 

42 [ 

43 load_emacs_page_navigation_bindings(), 

44 load_vi_page_navigation_bindings(), 

45 ] 

46 ), 

47 buffer_has_focus, 

48 ) 

49 

50 

51def load_emacs_page_navigation_bindings() -> KeyBindingsBase: 

52 """ 

53 Key bindings, for scrolling up and down through pages. 

54 This are separate bindings, because GNU readline doesn't have them. 

55 """ 

56 key_bindings = KeyBindings() 

57 handle = key_bindings.add 

58 

59 handle("c-v")(scroll_page_down) 

60 handle("pagedown")(scroll_page_down) 

61 handle("escape", "v")(scroll_page_up) 

62 handle("pageup")(scroll_page_up) 

63 

64 return ConditionalKeyBindings(key_bindings, emacs_mode) 

65 

66 

67def load_vi_page_navigation_bindings() -> KeyBindingsBase: 

68 """ 

69 Key bindings, for scrolling up and down through pages. 

70 This are separate bindings, because GNU readline doesn't have them. 

71 """ 

72 key_bindings = KeyBindings() 

73 handle = key_bindings.add 

74 

75 handle("c-f")(scroll_forward) 

76 handle("c-b")(scroll_backward) 

77 handle("c-d")(scroll_half_page_down) 

78 handle("c-u")(scroll_half_page_up) 

79 handle("c-e")(scroll_one_line_down) 

80 handle("c-y")(scroll_one_line_up) 

81 handle("pagedown")(scroll_page_down) 

82 handle("pageup")(scroll_page_up) 

83 

84 return ConditionalKeyBindings(key_bindings, vi_mode)