Use the current layout instead of the us layout (#2495)

* use the current layout instead of the us layout

* fix linting erros and add exception handling for SysCommand
This commit is contained in:
ngn 2024-05-15 11:41:42 +00:00 committed by GitHub
parent bab06e4d75
commit 39d096b79f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 2 deletions

View File

@ -1,7 +1,7 @@
from dataclasses import dataclass
from typing import Dict, Any, TYPE_CHECKING, Optional
from .utils import list_keyboard_languages, list_locales, set_kb_layout
from .utils import list_keyboard_languages, list_locales, set_kb_layout, get_kb_layout
from ..menu import Selector, AbstractSubMenu, MenuSelectionType, Menu
if TYPE_CHECKING:
@ -16,7 +16,10 @@ class LocaleConfiguration:
@staticmethod
def default() -> 'LocaleConfiguration':
return LocaleConfiguration('us', 'en_US', 'UTF-8')
layout = get_kb_layout()
if layout == "":
return LocaleConfiguration('us', 'en_US', 'UTF-8')
return LocaleConfiguration(layout, 'en_US', 'UTF-8')
def json(self) -> Dict[str, str]:
return {

View File

@ -44,6 +44,30 @@ def verify_x11_keyboard_layout(layout :str) -> bool:
return False
def get_kb_layout() -> str:
try:
lines = SysCommand(
"localectl --no-pager status",
environment_vars={'SYSTEMD_COLORS': '0'}
).decode().splitlines()
except:
return ""
vcline = ""
for line in lines:
if "VC Keymap: " in line:
vcline = line
if vcline == "":
return ""
layout = vcline.split(": ")[1]
if not verify_keyboard_layout(layout):
return ""
return layout
def set_kb_layout(locale :str) -> bool:
if len(locale.strip()):
if not verify_keyboard_layout(locale):