Skip to content

Commit 5f6f47e

Browse files
committed
Make font methods members of TranslationHandler, skip on non-ISO
1 parent 3c2c6c9 commit 5f6f47e

3 files changed

Lines changed: 60 additions & 59 deletions

File tree

archinstall/lib/translationhandler.py

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -41,56 +41,6 @@ def json(self) -> str:
4141
_ENV_FONT = os.environ.get('FONT')
4242

4343

44-
def _set_console_font(font_name: str | None) -> bool:
45-
"""
46-
Set the console font via setfont.
47-
If font_name is None, sets default8x16.
48-
On failure, keeps the current font unchanged.
49-
Returns True on success, False on failure.
50-
"""
51-
target = font_name or _DEFAULT_FONT
52-
53-
try:
54-
SysCommand(f'setfont {target}')
55-
return True
56-
except SysCallError as err:
57-
debug(f'Failed to set console font {target}: {err}')
58-
return False
59-
60-
61-
def save_console_font() -> None:
62-
"""Save the current console font (with unicode map) and console map to temp files."""
63-
try:
64-
font_fd, font_path = tempfile.mkstemp(prefix='archinstall_font_')
65-
cmap_fd, cmap_path = tempfile.mkstemp(prefix='archinstall_cmap_')
66-
os.close(font_fd)
67-
os.close(cmap_fd)
68-
translation_handler._font_backup = Path(font_path)
69-
translation_handler._cmap_backup = Path(cmap_path)
70-
SysCommand(f'setfont -O {translation_handler._font_backup} -om {translation_handler._cmap_backup}')
71-
except SysCallError as err:
72-
debug(f'Failed to save console font: {err}')
73-
translation_handler._font_backup = None
74-
translation_handler._cmap_backup = None
75-
76-
77-
def restore_console_font() -> None:
78-
"""Restore console font (with unicode map) and console map from backup."""
79-
if translation_handler._font_backup is None or not translation_handler._font_backup.exists():
80-
return
81-
82-
args = str(translation_handler._font_backup)
83-
if translation_handler._cmap_backup is not None and translation_handler._cmap_backup.exists():
84-
args += f' -m {translation_handler._cmap_backup}'
85-
_set_console_font(args)
86-
87-
translation_handler._font_backup.unlink(missing_ok=True)
88-
translation_handler._font_backup = None
89-
if translation_handler._cmap_backup is not None:
90-
translation_handler._cmap_backup.unlink(missing_ok=True)
91-
translation_handler._cmap_backup = None
92-
93-
9444
class TranslationHandler:
9545
def __init__(self) -> None:
9646
self._base_pot = 'base.pot'
@@ -113,6 +63,57 @@ def active_font(self) -> str | None:
11363
return self._active_language.console_font
11464
return None
11565

66+
def _set_font(self, font_name: str | None) -> bool:
67+
"""Set the console font via setfont. Only runs on ISO. Returns True on success."""
68+
from archinstall.lib.utils.util import running_from_iso
69+
70+
if not running_from_iso():
71+
return False
72+
73+
target = font_name or _DEFAULT_FONT
74+
try:
75+
SysCommand(f'setfont {target}')
76+
return True
77+
except SysCallError as err:
78+
debug(f'Failed to set console font {target}: {err}')
79+
return False
80+
81+
def save_console_font(self) -> None:
82+
"""Save the current console font (with unicode map) and console map to temp files."""
83+
from archinstall.lib.utils.util import running_from_iso
84+
85+
if not running_from_iso():
86+
return
87+
88+
try:
89+
font_fd, font_path = tempfile.mkstemp(prefix='archinstall_font_')
90+
cmap_fd, cmap_path = tempfile.mkstemp(prefix='archinstall_cmap_')
91+
os.close(font_fd)
92+
os.close(cmap_fd)
93+
self._font_backup = Path(font_path)
94+
self._cmap_backup = Path(cmap_path)
95+
SysCommand(f'setfont -O {self._font_backup} -om {self._cmap_backup}')
96+
except SysCallError as err:
97+
debug(f'Failed to save console font: {err}')
98+
self._font_backup = None
99+
self._cmap_backup = None
100+
101+
def restore_console_font(self) -> None:
102+
"""Restore console font (with unicode map) and console map from backup."""
103+
if self._font_backup is None or not self._font_backup.exists():
104+
return
105+
106+
args = str(self._font_backup)
107+
if self._cmap_backup is not None and self._cmap_backup.exists():
108+
args += f' -m {self._cmap_backup}'
109+
self._set_font(args)
110+
111+
self._font_backup.unlink(missing_ok=True)
112+
self._font_backup = None
113+
if self._cmap_backup is not None:
114+
self._cmap_backup.unlink(missing_ok=True)
115+
self._cmap_backup = None
116+
116117
def _get_translations(self) -> list[Language]:
117118
"""
118119
Load all translated languages and return a list of such
@@ -207,7 +208,7 @@ def activate(self, language: Language, set_font: bool = True) -> None:
207208
self._active_language = language
208209

209210
if set_font and not self._using_env_font:
210-
_set_console_font(language.console_font)
211+
self._set_font(language.console_font)
211212

212213
def apply_console_font(self) -> None:
213214
"""Apply console font from FONT env var or active language mapping.
@@ -217,16 +218,16 @@ def apply_console_font(self) -> None:
217218
If FONT is not set, use active language font.
218219
"""
219220
if _ENV_FONT:
220-
if _set_console_font(_ENV_FONT):
221+
if self._set_font(_ENV_FONT):
221222
self._using_env_font = True
222223
debug(f'Console font set from FONT env var: {_ENV_FONT}')
223224
else:
224225
debug(f'FONT={_ENV_FONT} could not be set, falling back to language font mapping')
225226
if self.active_font:
226-
_set_console_font(self.active_font)
227+
self._set_font(self.active_font)
227228
debug(f'Console font set from language mapping: {self.active_font}')
228229
elif self.active_font:
229-
_set_console_font(self.active_font)
230+
self._set_font(self.active_font)
230231
debug(f'Console font set from language mapping: {self.active_font}')
231232

232233
def _get_locales_dir(self) -> 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 tr, translation_handler
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+
translation_handler.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+
translation_handler.restore_console_font()
165165

166166
return rc
167167

archinstall/tui/ui/components.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,9 +1268,9 @@ def __init__(self, main: InstanceRunnable[ValueT] | Callable[[], Awaitable[Value
12681268

12691269
@override
12701270
async def _on_exit_app(self) -> None:
1271-
from archinstall.lib.translationhandler import restore_console_font
1271+
from archinstall.lib.translationhandler import translation_handler
12721272

1273-
restore_console_font()
1273+
translation_handler.restore_console_font()
12741274
await super()._on_exit_app()
12751275

12761276
def action_trigger_help(self) -> None:

0 commit comments

Comments
 (0)