Debian Codegraph @

cylc/flow/tui/__init__.py

raw
1 #!/usr/bin/env python3
2 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
3 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 """The cylc terminal user interface (Tui)."""
18
19 from cylc.flow.task_state import (
20 TASK_STATUS_WAITING,
21 TASK_STATUS_EXPIRED,
22 TASK_STATUS_PREPARING,
23 TASK_STATUS_SUBMIT_FAILED,
24 TASK_STATUS_SUBMITTED,
25 TASK_STATUS_RUNNING,
26 TASK_STATUS_FAILED,
27 TASK_STATUS_SUCCEEDED
28 )
29
30 TUI = """
31 _,@@@@@@.
32 <=@@@, `@@@@@.
33 `-@@@@@@@@@@@'
34 :@@@@@@@@@@.
35 (.@@@@@@@@@@@
36 ( '@@@@@@@@@@@@.
37 ;.@@@@@@@@@@@@@@@
38 '@@@@@@@@@@@@@@@@@@,
39 ,@@@@@@@@@@@@@@@@@@@@'
40 :.@@@@@@@@@@@@@@@@@@@@@.
41 .@@@@@@@@@@@@@@@@@@@@@@@@.
42 '@@@@@@@@@@@@@@@@@@@@@@@@@.
43 ;@@@@@@@@@@@@@@@@@@@@@@@@@@@
44 .@@@@@@@@@@@@@@@@@@@@@@@@@@.
45 .@@@@@@@@@@@@@@@@@@@@@@@@@@,
46 .@@@@@@@@@@@@@@@@@@@@@@@@@'
47 .@@@@@@@@@@@@@@@@@@@@@@@@' ,
48 :@@@@@@@@@@@@@@@@@@@@@..''';,,,;::-
49 '@@@@@@@@@@@@@@@@@@@. `. `
50 .@@@@@@.: ,.@@@@@@@. `
51 :@@@@@@@, ;.@,
52 '@@@@@@. `@'
53 .@@@@@@; ;-,
54 ;@@@@@@. ...,
55 ,,; ,;; ; ; ;
56 """
57
58 # default foreground and background colours
59 # NOTE: set to default to allow user defined terminal theming
60 FORE = 'default'
61 BACK = 'default'
62
63 # workflow state colour
64 WORKFLOW_COLOURS = {
65 'running': ('light blue', BACK),
66 'paused': ('brown', BACK),
67 'stopping': ('light magenta', BACK),
68 'stopped': ('light red', BACK),
69 'error': ('light red', BACK, 'bold')
70 }
71
72 # unicode task icons
73 TASK_ICONS = {
74 f'{TASK_STATUS_WAITING}': '\u25cb',
75 f'{TASK_STATUS_PREPARING}': '\u229A',
76 f'{TASK_STATUS_SUBMITTED}': '\u2299',
77 f'{TASK_STATUS_RUNNING}': '\u2299',
78 f'{TASK_STATUS_RUNNING}:0': '\u2299',
79 f'{TASK_STATUS_RUNNING}:25': '\u25D4',
80 f'{TASK_STATUS_RUNNING}:50': '\u25D1',
81 f'{TASK_STATUS_RUNNING}:75': '\u25D5',
82 f'{TASK_STATUS_SUCCEEDED}': '\u25CF',
83 f'{TASK_STATUS_EXPIRED}': '\u25CC',
84 f'{TASK_STATUS_SUBMIT_FAILED}': '\u2298',
85 f'{TASK_STATUS_FAILED}': '\u2297'
86 }
87
88 # unicode modifiers for special task states
89 TASK_MODIFIERS = {
90 'held': '\u030E',
91 'queued': '\u033F',
92 'runahead': '\u0340',
93 'xtriggered': '\u1DC9',
94 'wallclock': '\u135D',
95 'retry': '\u20D7',
96 }
97
98 # unicode job icon
99 JOB_ICON = '\u25A0'
100
101 # job colour coding
102 JOB_COLOURS = {
103 'submitted': 'dark cyan',
104 'running': 'light blue',
105 'succeeded': 'dark green',
106 'failed': 'light red',
107 'submit-failed': 'light magenta',
108 }
109
110
111 class Bindings:
112 """Represets key bindings for the Tui app."""
113
114 def __init__(self):
115 self.bindings = []
116 self.groups = {}
117
118 def bind(self, keys, group, desc, callback):
119 """Register a key binding.
120
121 Args:
122 keys:
123 The keys to bind.
124 group:
125 The group to which this binding should belong.
126 desc:
127 Description for this binding, used to generate help.
128 callback:
129 The thing to call when this binding is pressed.
130
131 """
132 if group not in self.groups:
133 raise ValueError(f'Group {group} not registered.')
134 binding = {
135 'keys': keys,
136 'group': group,
137 'desc': desc,
138 'callback': callback
139 }
140 self.bindings.append(binding)
141 self.groups[group]['bindings'].append(binding)
142
143 def add_group(self, group, desc):
144 """Add a new binding group.
145
146 Args:
147 group:
148 The name of the group.
149 desc:
150 A description of the group, used to generate help.
151
152 """
153 self.groups[group] = {
154 'name': group,
155 'desc': desc,
156 'bindings': []
157 }
158
159 def __iter__(self):
160 return iter(self.bindings)
161
162 def list_groups(self):
163 """List groups and the bindings in them.
164
165 Yields:
166 (group_name, [binding, ...])
167
168 """
169 for name, group in self.groups.items():
170 yield (
171 group,
172 [
173 binding
174 for binding in self.bindings
175 if binding['group'] == name
176 ]
177 )