-
-
Notifications
You must be signed in to change notification settings - Fork 737
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f6f6b4f
Set console font automatically when selecting language
Softer e45995f
Support FONT environment variable for console font override
Softer 92c990d
CI checks fix
Softer 960bc1b
Try to restore original console font with setfont -O
Softer 81f62a0
Fix for pylint
Softer 7068a83
Merge branch 'master' into auto-console-font
Softer ceb8b2e
Restore console font before Textual exits application mode
Softer b723909
Fall back to language font mapping when FONT env var is invalid
Softer 3ed54eb
Use tempfile for console font backup files
Softer 511e848
Fix linter errors: use mkstemp, close fds, add @override
Softer 7a53618
Fix ruff formatting
Softer 2334e36
Move font state from module singleton into TranslationHandler
Softer 5d24244
Refactor font handling per review feedback
Softer 3c2c6c9
Merge remote-tracking branch 'origin/master' into auto-console-font
Softer 5f6f47e
Make font methods members of TranslationHandler, skip on non-ISO
Softer 0cf2541
ci: trigger tests
Softer 4c0773c
Move running_from_iso import to module level
Softer a973ca0
Add explicit ISO guard to restore_console_font
Softer 0ac29d3
Skip apply_console_font off-ISO
Softer 1ec5080
Use list form for setfont SysCommand calls
Softer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,18 @@ def json(self) -> str: | |
| return self.name_en | ||
|
|
||
|
|
||
| _DEFAULT_FONT = 'default8x16' | ||
| _ENV_FONT = os.environ.get('FONT') | ||
|
|
||
|
|
||
| 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 +57,63 @@ 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 _set_font(self, font_name: str | None) -> bool: | ||
| """Set the console font via setfont. Only runs on ISO. Returns True on success.""" | ||
| from archinstall.lib.utils.util import running_from_iso | ||
|
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. does this cause a circular dep otherwise can we move it to the top? |
||
|
|
||
| if not running_from_iso(): | ||
| return False | ||
|
|
||
| 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(self) -> None: | ||
| """Save the current console font (with unicode map) and console map to temp files.""" | ||
| from archinstall.lib.utils.util import running_from_iso | ||
|
|
||
| if not running_from_iso(): | ||
| return | ||
|
|
||
| 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) | ||
| self._font_backup = Path(font_path) | ||
| self._cmap_backup = Path(cmap_path) | ||
| SysCommand(f'setfont -O {self._font_backup} -om {self._cmap_backup}') | ||
| except SysCallError as err: | ||
| debug(f'Failed to save console font: {err}') | ||
| self._font_backup = None | ||
| self._cmap_backup = None | ||
|
|
||
| def restore_console_font(self) -> None: | ||
| """Restore console font (with unicode map) and console map from backup.""" | ||
| if self._font_backup is None or not self._font_backup.exists(): | ||
|
svartkanin marked this conversation as resolved.
|
||
| return | ||
|
|
||
| args = str(self._font_backup) | ||
| if self._cmap_backup is not None and self._cmap_backup.exists(): | ||
| args += f' -m {self._cmap_backup}' | ||
| self._set_font(args) | ||
|
|
||
| self._font_backup.unlink(missing_ok=True) | ||
| self._font_backup = None | ||
| if self._cmap_backup is not None: | ||
| self._cmap_backup.unlink(missing_ok=True) | ||
| self._cmap_backup = None | ||
|
|
||
| def _get_translations(self) -> list[Language]: | ||
| """ | ||
| Load all translated languages and return a list of such | ||
|
|
@@ -57,6 +128,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 +143,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 +199,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: | ||
| self._set_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 self._set_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: | ||
| self._set_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.
|
||
| self._set_font(self.active_font) | ||
| debug(f'Console font set from language mapping: {self.active_font}') | ||
|
|
||
| def _get_locales_dir(self) -> Path: | ||
| """ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.