1313from rich .text import Text
1414from rich import box
1515from rich .color import ANSI_COLOR_NAMES
16- from apio .common .apio_styles import BORDER , EMPH1 , EMPH3
17- from apio .utils import util , cmd_util
18- from apio .utils .cmd_util import check_at_most_one_param
16+ from apio .common .apio_styles import BORDER , EMPH1 , EMPH3 , INFO
17+ from apio .utils import util
1918from apio .apio_context import ApioContext , ApioContextScope
2019from apio .utils .cmd_util import ApioGroup , ApioSubgroup , ApioCommand
21- from apio .common .apio_console import PADDING , cout , cstyle , ctable
22- from apio .common .apio_themes import THEMES_TABLE
20+ from apio .common .apio_themes import THEMES_TABLE , THEME_LIGHT
21+ from apio .common .apio_console import (
22+ PADDING ,
23+ cout ,
24+ cstyle ,
25+ ctable ,
26+ get_theme ,
27+ configure ,
28+ )
2329
2430
2531# ------ apio info system
@@ -162,95 +168,42 @@ def _platforms_cli():
162168# -- Text in the rich-text format of the python rich library.
163169APIO_INFO_COLORS_HELP = """
164170The command 'apio info colors' shows how ansi colors are rendered on \
165- the platform, and is typically used to diagnose color related issues. \
166- While the color name and styling is always handled by the Python Rich \
167- library, the output is done via three different libraries, based on \
168- the user's selection.
171+ the platform, and is typically used to diagnose color related issues.
169172
173+ The command shows the themes colors even if the current theme is 'no-colors'.
170174
171175Examples:[code]
172176 apio info colors # Rich library output (default)
173- apio info colors --rich # Same as above.
174- apio info colors --click # Click library output.
175- apio info colors --print # Python's print() output.
176177 apio inf col -p # Using shortcuts.[/code]
177178"""
178179
179- rich_option = click .option (
180- "rich_" , # Var name.
181- "-r" ,
182- "--rich" ,
183- is_flag = True ,
184- help = "Output using the rich lib." ,
185- cls = cmd_util .ApioOption ,
186- )
187-
188- click_option = click .option (
189- "click_" , # Var name.
190- "-c" ,
191- "--click" ,
192- is_flag = True ,
193- help = "Output using the click lib." ,
194- cls = cmd_util .ApioOption ,
195- )
196-
197- print_option = click .option (
198- "print_" , # Var name.
199- "-p" ,
200- "--print" ,
201- is_flag = True ,
202- help = "Output using python's print()." ,
203- cls = cmd_util .ApioOption ,
204- )
205-
206180
207181@click .command (
208182 name = "colors" ,
209183 cls = ApioCommand ,
210184 short_help = "Colors table." ,
211185 help = APIO_INFO_COLORS_HELP ,
212186)
213- @click .pass_context
214- @rich_option
215- @click_option
216- @print_option
217- def _colors_cli (
218- cmd_ctx : click .Context ,
219- # options
220- rich_ : bool ,
221- click_ : bool ,
222- print_ : bool ,
223- ):
187+ def _colors_cli ():
224188 """Implements the 'apio info colors' command."""
225189
226- # pylint: disable=too-many-locals
227-
228- # -- Make pylint happy.
229- _ = (rich_ ,)
230-
231- # -- Allow at most one of --click and --print.
232- check_at_most_one_param (cmd_ctx , ["rich_" , "click_" , "print_" ])
233-
234- # -- Select by output type.
235- if click_ :
236- mode = "CLICK"
237- output_func = click .echo
238- elif print_ :
239- mode = "PRINT"
240- output_func = print
241- else :
242- mode = "RICH"
243- output_func = cout
244-
245190 # -- Print title.
246- cout ("" , f "ANSI Colors [ { mode } mode] " , "" )
191+ cout ("" , "ANSI Colors" , "" )
247192
248193 # -- Create a reversed num->name map
249194 lookup = {}
250195 for name , num in ANSI_COLOR_NAMES .items ():
251196 assert 0 <= num <= 255
252197 lookup [num ] = name
253198
199+ # -- Make sure the current theme supports colors, otherwise they will
200+ # -- suppressed
201+ if get_theme ().colors_enabled :
202+ saved_theme_name = None
203+ else :
204+ saved_theme_name = get_theme ().name
205+ configure (theme_name = THEME_LIGHT .name )
206+
254207 # -- Print the table.
255208 num_rows = 64
256209 num_cols = 4
@@ -273,10 +226,14 @@ def _colors_cli(
273226 line = " " .join (values )
274227
275228 # -- Output the line.
276- output_func (line )
229+ cout (line )
277230
278231 cout ()
279232
233+ # -- Restore the original theme.
234+ if saved_theme_name :
235+ configure (theme_name = saved_theme_name )
236+
280237
281238# ------ apio info themes
282239
@@ -286,6 +243,8 @@ def _colors_cli(
286243 be used to select the theme that works the best for you. Type \
287244 'apio preferences -h' for information on our to select a theme.
288245
246+ The command shows colors even if the current theme is 'no-colors'.
247+
289248[code]
290249Examples:
291250 apio info themes # Show themes colors
@@ -322,9 +281,17 @@ def _themes_cli():
322281 title_justify = "left" ,
323282 )
324283
284+ # -- Get selected theme
285+ selected_theme = get_theme ()
286+ selected_theme_name = selected_theme .name
287+
325288 # -- Add the table columns, one per theme.
326- for theme_name in THEMES_TABLE :
327- table .add_column (theme_name .upper (), no_wrap = True , justify = "center" )
289+ for theme_name , theme in THEMES_TABLE .items ():
290+ assert theme_name == theme .name
291+ column_name = theme_name .upper ()
292+ if theme_name == selected_theme_name :
293+ column_name = f"*{ column_name } *"
294+ table .add_column (column_name , no_wrap = True , justify = "center" )
328295
329296 # -- Append the table rows
330297 for style_name in style_names :
@@ -347,9 +314,22 @@ def _themes_cli():
347314
348315 table .add_row (* row_values )
349316
317+ # -- Make sure the current theme supports colors, otherwise they will
318+ # -- suppressed
319+ if get_theme ().colors_enabled :
320+ saved_theme_name = None
321+ else :
322+ saved_theme_name = get_theme ().name
323+ configure (theme_name = THEME_LIGHT .name )
324+
350325 # -- Render the table.
351326 cout ()
352327 ctable (table )
328+
329+ if saved_theme_name :
330+ configure (theme_name = saved_theme_name )
331+
332+ cout ("To change your theme use 'apio preferences -t ...'" , style = INFO )
353333 cout ()
354334
355335
0 commit comments