-
-
Notifications
You must be signed in to change notification settings - Fork 736
Set console font automatically when selecting language #4356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 12 commits
f6f6b4f
e45995f
92c990d
960bc1b
81f62a0
7068a83
ceb8b2e
b723909
3ed54eb
511e848
7a53618
2334e36
5d24244
3c2c6c9
5f6f47e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,15 @@ | |
| import gettext | ||
| import json | ||
| import os | ||
| import tempfile | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import override | ||
|
|
||
| from archinstall.lib.command import SysCommand | ||
| from archinstall.lib.exceptions import SysCallError | ||
| from archinstall.lib.output import debug | ||
|
|
||
|
|
||
| @dataclass | ||
| class Language: | ||
|
|
@@ -14,6 +19,7 @@ class Language: | |
| translation: gettext.NullTranslations | ||
| translation_percent: int | ||
| translated_lang: str | None | ||
| console_font: str | None = None | ||
|
|
||
| @property | ||
| def display_name(self) -> str: | ||
|
|
@@ -31,10 +37,68 @@ def json(self) -> str: | |
| return self.name_en | ||
|
|
||
|
|
||
| _DEFAULT_FONT = 'default8x16' | ||
| _ENV_FONT = os.environ.get('FONT') | ||
|
|
||
|
|
||
| def _set_console_font(font_name: str | None) -> bool: | ||
| """ | ||
| Set the console font via setfont. | ||
| If font_name is None, sets default8x16. | ||
| On failure, keeps the current font unchanged. | ||
| Returns True on success, False on failure. | ||
| """ | ||
| target = font_name or _DEFAULT_FONT | ||
|
|
||
| try: | ||
| SysCommand(f'setfont {target}') | ||
| return True | ||
| except SysCallError as err: | ||
| debug(f'Failed to set console font {target}: {err}') | ||
| return False | ||
|
|
||
|
|
||
| def _save_console_font() -> None: | ||
| """Save the current console font (with unicode map) and console map to temp files.""" | ||
| try: | ||
| font_fd, font_path = tempfile.mkstemp(prefix='archinstall_font_') | ||
| cmap_fd, cmap_path = tempfile.mkstemp(prefix='archinstall_cmap_') | ||
| os.close(font_fd) | ||
| os.close(cmap_fd) | ||
| translation_handler._font_backup = Path(font_path) | ||
| translation_handler._cmap_backup = Path(cmap_path) | ||
| SysCommand(f'setfont -O {translation_handler._font_backup} -om {translation_handler._cmap_backup}') | ||
| except SysCallError as err: | ||
| debug(f'Failed to save console font: {err}') | ||
| translation_handler._font_backup = None | ||
| translation_handler._cmap_backup = None | ||
|
|
||
|
|
||
| def _restore_console_font() -> None: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this is exported it should not be private same with |
||
| """Restore console font (with unicode map) and console map from backup.""" | ||
| if translation_handler._font_backup is None or not translation_handler._font_backup.exists(): | ||
| return | ||
|
|
||
| args = str(translation_handler._font_backup) | ||
| if translation_handler._cmap_backup is not None and translation_handler._cmap_backup.exists(): | ||
| args += f' -m {translation_handler._cmap_backup}' | ||
| _set_console_font(args) | ||
|
|
||
| translation_handler._font_backup.unlink(missing_ok=True) | ||
| translation_handler._font_backup = None | ||
| if translation_handler._cmap_backup is not None: | ||
| translation_handler._cmap_backup.unlink(missing_ok=True) | ||
| translation_handler._cmap_backup = None | ||
|
|
||
|
|
||
| class TranslationHandler: | ||
| def __init__(self) -> None: | ||
| self._base_pot = 'base.pot' | ||
| self._languages = 'languages.json' | ||
| self._active_language: Language | None = None | ||
| self._font_backup: Path | None = None | ||
| self._cmap_backup: Path | None = None | ||
| self._using_env_font: bool = False | ||
|
|
||
| self._total_messages = self._get_total_active_messages() | ||
| self._translated_languages = self._get_translations() | ||
|
|
@@ -43,6 +107,12 @@ def __init__(self) -> None: | |
| def translated_languages(self) -> list[Language]: | ||
| return self._translated_languages | ||
|
|
||
| @property | ||
| def active_font(self) -> str | None: | ||
| if self._active_language is not None: | ||
| return self._active_language.console_font | ||
| return None | ||
|
|
||
| def _get_translations(self) -> list[Language]: | ||
| """ | ||
| Load all translated languages and return a list of such | ||
|
|
@@ -57,6 +127,7 @@ def _get_translations(self) -> list[Language]: | |
| abbr = mapping_entry['abbr'] | ||
| lang = mapping_entry['lang'] | ||
| translated_lang = mapping_entry.get('translated_lang', None) | ||
| console_font = mapping_entry.get('console_font', None) | ||
|
|
||
| try: | ||
| # get a translation for a specific language | ||
|
|
@@ -71,7 +142,7 @@ def _get_translations(self) -> list[Language]: | |
| # prevent cases where the .pot file is out of date and the percentage is above 100 | ||
| percent = min(100, percent) | ||
|
|
||
| language = Language(abbr, lang, translation, percent, translated_lang) | ||
| language = Language(abbr, lang, translation, percent, translated_lang, console_font) | ||
| languages.append(language) | ||
| except FileNotFoundError as err: | ||
| raise FileNotFoundError(f"Could not locate language file for '{lang}': {err}") | ||
|
|
@@ -127,12 +198,16 @@ def get_language_by_abbr(self, abbr: str) -> Language: | |
| except Exception: | ||
| raise ValueError(f'No language with abbreviation "{abbr}" found') | ||
|
|
||
| def activate(self, language: Language) -> None: | ||
| def activate(self, language: Language, set_font: bool = True) -> None: | ||
| """ | ||
| Set the provided language as the current translation | ||
| """ | ||
| # The install() call has the side effect of assigning GNUTranslations.gettext to builtins._ | ||
| language.translation.install() | ||
| self._active_language = language | ||
|
|
||
| if set_font and not self._using_env_font: | ||
| _set_console_font(language.console_font) | ||
|
|
||
| def _get_locales_dir(self) -> Path: | ||
| """ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,7 @@ | |
| from archinstall.lib.output import debug, error, info, warn | ||
| from archinstall.lib.packages.util import check_version_upgrade | ||
| from archinstall.lib.pacman.pacman import Pacman | ||
| from archinstall.lib.translationhandler import tr | ||
| from archinstall.lib.translationhandler import _restore_console_font, _save_console_font, tr | ||
| from archinstall.lib.utils.util import running_from_iso | ||
| from archinstall.tui.ui.components import tui | ||
|
|
||
|
|
@@ -95,6 +95,8 @@ def run() -> int: | |
| print(tr('Archinstall requires root privileges to run. See --help for more.')) | ||
| return 1 | ||
|
|
||
| _save_console_font() | ||
|
|
||
| _log_sys_info() | ||
|
|
||
| if not arch_config_handler.args.offline: | ||
|
|
@@ -159,6 +161,8 @@ def main() -> int: | |
| _error_message(exc) | ||
| rc = 1 | ||
|
|
||
| _restore_console_font() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we sure that this will be execute on any exception otherwise it will not be reset?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's covered at two levels:
As far as I understand - only uncovered case is SIGKILL, which can't be handled at all. Am I missing any other scenario? |
||
|
|
||
| return rc | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
| from textual.widgets.selection_list import Selection | ||
| from textual.worker import WorkerCancelled | ||
|
|
||
| from archinstall.lib.output import debug | ||
| from archinstall.lib.output import debug, info | ||
| from archinstall.lib.translationhandler import tr | ||
| from archinstall.tui.ui.menu_item import MenuItem, MenuItemGroup | ||
| from archinstall.tui.ui.result import Result, ResultType | ||
|
|
@@ -1236,6 +1236,13 @@ def __init__(self, main: InstanceRunnable[ValueT] | Callable[[], Awaitable[Value | |
| super().__init__(ansi_color=True) | ||
| self._main = main | ||
|
|
||
| @override | ||
| async def _on_exit_app(self) -> None: | ||
| from archinstall.lib.translationhandler import _restore_console_font | ||
|
|
||
| _restore_console_font() | ||
| await super()._on_exit_app() | ||
|
|
||
| def action_trigger_help(self) -> None: | ||
| from textual.widgets import HelpPanel | ||
|
|
||
|
|
@@ -1245,6 +1252,17 @@ def action_trigger_help(self) -> None: | |
| _ = self.screen.mount(HelpPanel()) | ||
|
|
||
| def on_mount(self) -> None: | ||
| import archinstall.lib.translationhandler as th | ||
|
|
||
| if th._ENV_FONT: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic has nothing to do with the tui and should all be moved to the translation handler module exposed via a single function |
||
| if th._set_console_font(th._ENV_FONT): | ||
| th.translation_handler._using_env_font = True | ||
| else: | ||
| debug(f'FONT={th._ENV_FONT} could not be set, using language font mapping') | ||
| if th.translation_handler.active_font: | ||
| th._set_console_font(th.translation_handler.active_font) | ||
| elif th.translation_handler.active_font: | ||
| th._set_console_font(th.translation_handler.active_font) | ||
| self._run_worker() | ||
|
|
||
| @work | ||
|
|
@@ -1275,9 +1293,14 @@ class TApp: | |
| app: _AppInstance[Any] | None = None | ||
|
|
||
| def run(self, main: InstanceRunnable[ValueT] | Callable[[], Awaitable[ValueT]]) -> ValueT: | ||
| import archinstall.lib.translationhandler as th | ||
|
|
||
| TApp.app = _AppInstance(main) | ||
| result: ValueT | Exception | None = TApp.app.run() | ||
|
|
||
| if th._ENV_FONT and not th.translation_handler._using_env_font: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. such infos shouldn't be logged conditionally but rather the state should be part of the log message |
||
| info(f'FONT={th._ENV_FONT} could not be set, using language font mapping') | ||
|
|
||
| if isinstance(result, Exception): | ||
| raise result | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return value is never used, we can probably remove it