-
-
Notifications
You must be signed in to change notification settings - Fork 738
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 14 commits
f6f6b4f
e45995f
92c990d
960bc1b
81f62a0
7068a83
ceb8b2e
b723909
3ed54eb
511e848
7a53618
2334e36
5d24244
3c2c6c9
5f6f47e
0cf2541
4c0773c
a973ca0
0ac29d3
1ec5080
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: | ||
|
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. The return value is never used, we can probably remove it
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. @Softer can we address this?
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. now used in apply_console_font:227 for FONT fallback |
||
| """ | ||
| 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: | ||
|
svartkanin marked this conversation as resolved.
Outdated
|
||
| """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: | ||
| """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,36 @@ 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 apply_console_font(self) -> None: | ||
| """Apply console font from FONT env var or active language mapping. | ||
|
|
||
| If FONT env var is set and valid, use it and skip language mapping. | ||
| If FONT is set but invalid, fall back to language font. | ||
| If FONT is not set, use active language font. | ||
| """ | ||
| if _ENV_FONT: | ||
| if _set_console_font(_ENV_FONT): | ||
| self._using_env_font = True | ||
| debug(f'Console font set from FONT env var: {_ENV_FONT}') | ||
| else: | ||
| debug(f'FONT={_ENV_FONT} could not be set, falling back to language font mapping') | ||
| if self.active_font: | ||
| _set_console_font(self.active_font) | ||
| debug(f'Console font set from language mapping: {self.active_font}') | ||
| elif self.active_font: | ||
|
svartkanin marked this conversation as resolved.
|
||
| _set_console_font(self.active_font) | ||
| debug(f'Console font set from language mapping: {self.active_font}') | ||
|
|
||
| def _get_locales_dir(self) -> Path: | ||
| """ | ||
|
|
||
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.
why is
set_fontfalse here?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.
from_config runs before the TUI starts. Calling setfont here would trigger color artifacts due to 256/512 glyph count transitions once Textual activates the alternate screen. Font application is deferred to on_mount so the transition happens inside the TUI lifecycle.