Make font methods members of TranslationHandler, skip on non-ISO

This commit is contained in:
Softer 2026-04-14 17:15:15 +03:00
parent 3c2c6c940d
commit 5f6f47ed12
3 changed files with 60 additions and 59 deletions

View File

@ -41,56 +41,6 @@ _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:
"""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'
@ -113,6 +63,57 @@ class TranslationHandler:
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
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():
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
@ -207,7 +208,7 @@ class TranslationHandler:
self._active_language = language
if set_font and not self._using_env_font:
_set_console_font(language.console_font)
self._set_font(language.console_font)
def apply_console_font(self) -> None:
"""Apply console font from FONT env var or active language mapping.
@ -217,16 +218,16 @@ class TranslationHandler:
If FONT is not set, use active language font.
"""
if _ENV_FONT:
if _set_console_font(_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:
_set_console_font(self.active_font)
self._set_font(self.active_font)
debug(f'Console font set from language mapping: {self.active_font}')
elif self.active_font:
_set_console_font(self.active_font)
self._set_font(self.active_font)
debug(f'Console font set from language mapping: {self.active_font}')
def _get_locales_dir(self) -> Path:

View File

@ -16,7 +16,7 @@ from archinstall.lib.networking import ping
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 restore_console_font, save_console_font, tr
from archinstall.lib.translationhandler import tr, translation_handler
from archinstall.lib.utils.util import running_from_iso
from archinstall.tui.ui.components import tui
@ -95,7 +95,7 @@ def run() -> int:
print(tr('Archinstall requires root privileges to run. See --help for more.'))
return 1
save_console_font()
translation_handler.save_console_font()
_log_sys_info()
@ -161,7 +161,7 @@ def main() -> int:
_error_message(exc)
rc = 1
restore_console_font()
translation_handler.restore_console_font()
return rc

View File

@ -1268,9 +1268,9 @@ class _AppInstance(App[ValueT]):
@override
async def _on_exit_app(self) -> None:
from archinstall.lib.translationhandler import restore_console_font
from archinstall.lib.translationhandler import translation_handler
restore_console_font()
translation_handler.restore_console_font()
await super()._on_exit_app()
def action_trigger_help(self) -> None: