]>
git.ipfire.org Git - people/ms/bricklayer.git/blob - src/python/tui/__init__.py
ce241f8c934ada182f7d65d584ad54f1e36ab617
1 ###############################################################################
3 # Bricklayer - An Installer for IPFire #
4 # Copyright (C) 2021 IPFire Development Team #
6 # This program is free software; you can redistribute it and/or #
7 # modify it under the terms of the GNU General Public License #
8 # as published by the Free Software Foundation; either version 2 #
9 # of the License, or (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 ###############################################################################
27 log
= logging
.getLogger("bricklayer.tui")
30 def __init__(self
, bricklayer
):
31 self
.bricklayer
= bricklayer
33 # Placeholder for screen
36 # Make this class usable as context
39 log
.debug("Entering TUI context")
46 def __exit__(self
, type, value
, traceback
):
47 log
.debug("Leaving TUI context")
54 Refreshes what is written on the screen
59 def _setup_screen(self
):
63 self
.screen
= snack
.SnackScreen()
67 _("<Tab>/<Alt-Tab> between elements | <Space> selects | <F12> next screen")
73 def _finish_screen(self
):
81 def push_helpline(self
, helpline
):
83 Sets the helpline, but centers it first
90 helpline
= helpline
.center(self
.screen
.width
)
92 self
.screen
.pushHelpLine(helpline
)
94 def message(self
, title
, text
, buttons
=None, help=None, width
=40):
96 Shows a message to the user
100 # Set default buttons
102 buttons
= (_("OK"), _("Cancel"))
104 return snack
.ButtonChoiceWindow(self
.screen
, title
=title
, text
=text
,
105 buttons
=buttons
, width
=width
, help=help)
107 def error(self
, title
, text
, buttons
=None, width
=40):
109 buttons
= [_("Abort Installation")]
111 return self
.message(title
, text
, buttons
=buttons
, width
=width
)
113 def progress(self
, *args
, **kwargs
):
114 return ProgressWindow(self
, *args
, **kwargs
)
116 def select(self
, title
, text
, items
, buttons
=None, default
=None, help=None):
119 default
= items
.get(default
, None)
121 # Convert items into a list which is sorted by its values
122 items
= sorted(items
.items(), key
=lambda item
: item
[1])
124 # Set some default buttons
126 buttons
= (_("Select"), _("Cancel"))
129 button
, item
= snack
.ListboxChoiceWindow(self
.screen
, title
, text
,
130 [value
for key
, value
in items
], buttons
=buttons
, default
=default
, help=help)
132 # Find the selected item
133 key
, value
= items
[item
]
138 def multi_select(self
, title
, text
, items
, selection
=[], buttons
=None,
139 width
=40, height
=None, help=None):
145 # Set some default buttons
147 buttons
= (_("Select"), _("Cancel"))
149 button_bar
= snack
.ButtonBar(self
.screen
, buttons
)
150 text_box
= snack
.TextboxReflowed(width
, text
)
153 checkboxes
= snack
.CheckboxTree(height
, scroll
=len(items
) > height
)
155 checkboxes
.append(items
[key
], key
, key
in selection
)
158 grid
= snack
.GridFormHelp(self
.screen
, title
, help, 1, 3)
159 grid
.add(text_box
, 0, 0)
160 grid
.add(checkboxes
, 0, 1, padding
=(0, 1, 0, 1))
161 grid
.add(button_bar
, 0, 2, growx
=True)
166 # Return the selection
167 return checkboxes
.getSelection()
170 class ProgressWindow(object):
171 def __init__(self
, tui
, title
, text
, max_value
=1, width
=60, help=None):
175 textbox
= snack
.TextboxReflowed(width
, text
)
177 # Add the progressbar
178 scale
= snack
.Scale(width
, total
=max_value
)
179 self
.update_callback
= scale
.set
182 self
.grid
= snack
.GridFormHelp(tui
.screen
, title
, help, 1, 3)
183 self
.grid
.add(textbox
, 0, 0, padding
=(0, 0, 0, 1))
184 self
.grid
.add(scale
, 0, 1, growx
=1)
191 def __exit__(self
, type, value
, traceback
):
194 def update(self
, value
):
196 Updates the progressbar value
198 self
.update_callback(value
)