Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/prompt_toolkit/widgets/dialogs.py: 40%

35 statements  

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

1""" 

2Collection of reusable components for building full screen applications. 

3""" 

4from __future__ import annotations 

5 

6from typing import Sequence 

7 

8from prompt_toolkit.filters import has_completions, has_focus 

9from prompt_toolkit.formatted_text import AnyFormattedText 

10from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous 

11from prompt_toolkit.key_binding.key_bindings import KeyBindings 

12from prompt_toolkit.layout.containers import ( 

13 AnyContainer, 

14 DynamicContainer, 

15 HSplit, 

16 VSplit, 

17) 

18from prompt_toolkit.layout.dimension import AnyDimension 

19from prompt_toolkit.layout.dimension import Dimension as D 

20 

21from .base import Box, Button, Frame, Shadow 

22 

23__all__ = [ 

24 "Dialog", 

25] 

26 

27 

28class Dialog: 

29 """ 

30 Simple dialog window. This is the base for input dialogs, message dialogs 

31 and confirmation dialogs. 

32 

33 Changing the title and body of the dialog is possible at runtime by 

34 assigning to the `body` and `title` attributes of this class. 

35 

36 :param body: Child container object. 

37 :param title: Text to be displayed in the heading of the dialog. 

38 :param buttons: A list of `Button` widgets, displayed at the bottom. 

39 """ 

40 

41 def __init__( 

42 self, 

43 body: AnyContainer, 

44 title: AnyFormattedText = "", 

45 buttons: Sequence[Button] | None = None, 

46 modal: bool = True, 

47 width: AnyDimension = None, 

48 with_background: bool = False, 

49 ) -> None: 

50 self.body = body 

51 self.title = title 

52 

53 buttons = buttons or [] 

54 

55 # When a button is selected, handle left/right key bindings. 

56 buttons_kb = KeyBindings() 

57 if len(buttons) > 1: 

58 first_selected = has_focus(buttons[0]) 

59 last_selected = has_focus(buttons[-1]) 

60 

61 buttons_kb.add("left", filter=~first_selected)(focus_previous) 

62 buttons_kb.add("right", filter=~last_selected)(focus_next) 

63 

64 frame_body: AnyContainer 

65 if buttons: 

66 frame_body = HSplit( 

67 [ 

68 # Add optional padding around the body. 

69 Box( 

70 body=DynamicContainer(lambda: self.body), 

71 padding=D(preferred=1, max=1), 

72 padding_bottom=0, 

73 ), 

74 # The buttons. 

75 Box( 

76 body=VSplit(buttons, padding=1, key_bindings=buttons_kb), 

77 height=D(min=1, max=3, preferred=3), 

78 ), 

79 ] 

80 ) 

81 else: 

82 frame_body = body 

83 

84 # Key bindings for whole dialog. 

85 kb = KeyBindings() 

86 kb.add("tab", filter=~has_completions)(focus_next) 

87 kb.add("s-tab", filter=~has_completions)(focus_previous) 

88 

89 frame = Shadow( 

90 body=Frame( 

91 title=lambda: self.title, 

92 body=frame_body, 

93 style="class:dialog.body", 

94 width=(None if with_background is None else width), 

95 key_bindings=kb, 

96 modal=modal, 

97 ) 

98 ) 

99 

100 self.container: Box | Shadow 

101 if with_background: 

102 self.container = Box(body=frame, style="class:dialog", width=width) 

103 else: 

104 self.container = frame 

105 

106 def __pt_container__(self) -> AnyContainer: 

107 return self.container