Skip to content

Commit 5d24244

Browse files
committed
Refactor font handling per review feedback
1 parent 2334e36 commit 5d24244

3 files changed

Lines changed: 30 additions & 23 deletions

File tree

archinstall/lib/translationhandler.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def _set_console_font(font_name: str | None) -> bool:
5858
return False
5959

6060

61-
def _save_console_font() -> None:
61+
def save_console_font() -> None:
6262
"""Save the current console font (with unicode map) and console map to temp files."""
6363
try:
6464
font_fd, font_path = tempfile.mkstemp(prefix='archinstall_font_')
@@ -74,7 +74,7 @@ def _save_console_font() -> None:
7474
translation_handler._cmap_backup = None
7575

7676

77-
def _restore_console_font() -> None:
77+
def restore_console_font() -> None:
7878
"""Restore console font (with unicode map) and console map from backup."""
7979
if translation_handler._font_backup is None or not translation_handler._font_backup.exists():
8080
return
@@ -209,6 +209,26 @@ def activate(self, language: Language, set_font: bool = True) -> None:
209209
if set_font and not self._using_env_font:
210210
_set_console_font(language.console_font)
211211

212+
def apply_console_font(self) -> None:
213+
"""Apply console font from FONT env var or active language mapping.
214+
215+
If FONT env var is set and valid, use it and skip language mapping.
216+
If FONT is set but invalid, fall back to language font.
217+
If FONT is not set, use active language font.
218+
"""
219+
if _ENV_FONT:
220+
if _set_console_font(_ENV_FONT):
221+
self._using_env_font = True
222+
debug(f'Console font set from FONT env var: {_ENV_FONT}')
223+
else:
224+
debug(f'FONT={_ENV_FONT} could not be set, falling back to language font mapping')
225+
if self.active_font:
226+
_set_console_font(self.active_font)
227+
debug(f'Console font set from language mapping: {self.active_font}')
228+
elif self.active_font:
229+
_set_console_font(self.active_font)
230+
debug(f'Console font set from language mapping: {self.active_font}')
231+
212232
def _get_locales_dir(self) -> Path:
213233
"""
214234
Get the locales directory path

archinstall/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from archinstall.lib.output import debug, error, info, warn
1717
from archinstall.lib.packages.util import check_version_upgrade
1818
from archinstall.lib.pacman.pacman import Pacman
19-
from archinstall.lib.translationhandler import _restore_console_font, _save_console_font, tr
19+
from archinstall.lib.translationhandler import restore_console_font, save_console_font, tr
2020
from archinstall.lib.utils.util import running_from_iso
2121
from archinstall.tui.ui.components import tui
2222

@@ -95,7 +95,7 @@ def run() -> int:
9595
print(tr('Archinstall requires root privileges to run. See --help for more.'))
9696
return 1
9797

98-
_save_console_font()
98+
save_console_font()
9999

100100
_log_sys_info()
101101

@@ -161,7 +161,7 @@ def main() -> int:
161161
_error_message(exc)
162162
rc = 1
163163

164-
_restore_console_font()
164+
restore_console_font()
165165

166166
return rc
167167

archinstall/tui/ui/components.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from textual.widgets.selection_list import Selection
2020
from textual.worker import WorkerCancelled
2121

22-
from archinstall.lib.output import debug, info
22+
from archinstall.lib.output import debug
2323
from archinstall.lib.translationhandler import tr
2424
from archinstall.tui.ui.menu_item import MenuItem, MenuItemGroup
2525
from archinstall.tui.ui.result import Result, ResultType
@@ -1238,9 +1238,9 @@ def __init__(self, main: InstanceRunnable[ValueT] | Callable[[], Awaitable[Value
12381238

12391239
@override
12401240
async def _on_exit_app(self) -> None:
1241-
from archinstall.lib.translationhandler import _restore_console_font
1241+
from archinstall.lib.translationhandler import restore_console_font
12421242

1243-
_restore_console_font()
1243+
restore_console_font()
12441244
await super()._on_exit_app()
12451245

12461246
def action_trigger_help(self) -> None:
@@ -1252,17 +1252,9 @@ def action_trigger_help(self) -> None:
12521252
_ = self.screen.mount(HelpPanel())
12531253

12541254
def on_mount(self) -> None:
1255-
import archinstall.lib.translationhandler as th
1255+
from archinstall.lib.translationhandler import translation_handler
12561256

1257-
if th._ENV_FONT:
1258-
if th._set_console_font(th._ENV_FONT):
1259-
th.translation_handler._using_env_font = True
1260-
else:
1261-
debug(f'FONT={th._ENV_FONT} could not be set, using language font mapping')
1262-
if th.translation_handler.active_font:
1263-
th._set_console_font(th.translation_handler.active_font)
1264-
elif th.translation_handler.active_font:
1265-
th._set_console_font(th.translation_handler.active_font)
1257+
translation_handler.apply_console_font()
12661258
self._run_worker()
12671259

12681260
@work
@@ -1293,14 +1285,9 @@ class TApp:
12931285
app: _AppInstance[Any] | None = None
12941286

12951287
def run(self, main: InstanceRunnable[ValueT] | Callable[[], Awaitable[ValueT]]) -> ValueT:
1296-
import archinstall.lib.translationhandler as th
1297-
12981288
TApp.app = _AppInstance(main)
12991289
result: ValueT | Exception | None = TApp.app.run()
13001290

1301-
if th._ENV_FONT and not th.translation_handler._using_env_font:
1302-
info(f'FONT={th._ENV_FONT} could not be set, using language font mapping')
1303-
13041291
if isinstance(result, Exception):
13051292
raise result
13061293

0 commit comments

Comments
 (0)