More cleanups

This commit is contained in:
Kovid Goyal 2021-10-05 11:41:34 +05:30
parent 57152a8e29
commit 498d3d5906
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 25 additions and 26 deletions

View File

@ -11,7 +11,7 @@ import time
from functools import partial from functools import partial
from pprint import pformat from pprint import pformat
from typing import ( from typing import (
IO, Callable, Dict, Generator, Iterable, Optional, Set, Tuple IO, Callable, Dict, Generator, Iterable, Iterator, Optional, Set, Tuple
) )
from kittens.tui.operations import colored, styled from kittens.tui.operations import colored, styled
@ -182,37 +182,34 @@ class IssueData:
self.u = str(self.num_users) self.u = str(self.num_users)
self.U = self.u + ' user' + ('' if self.num_users == 1 else 's') self.U = self.u + ' user' + ('' if self.num_users == 1 else 's')
def translate_issue_char(self, char: str) -> str:
try:
return str(getattr(self, char)) if len(char) == 1 else char
except AttributeError:
return char
def translate_issue_char(ctx: IssueData, char: str) -> str: def parse_issue_file(self, issue_file: IO[str]) -> Iterator[str]:
try: last_char: Optional[str] = None
return str(getattr(ctx, char)) if len(char) == 1 else char while True:
except AttributeError: this_char = issue_file.read(1)
return char if not this_char:
break
if last_char == '\\':
yield self.translate_issue_char(this_char)
elif last_char is not None:
yield last_char
# `\\\a` should not match the last two slashes,
# so make it look like it was `\?\a` where `?`
# is some character other than `\`.
last_char = None if last_char == '\\' else this_char
if last_char is not None:
yield last_char
def format_tty_name(raw: str) -> str: def format_tty_name(raw: str) -> str:
return re.sub(r'^/dev/([^/]+)/([^/]+)$', r'\1\2', raw) return re.sub(r'^/dev/([^/]+)/([^/]+)$', r'\1\2', raw)
def print_issue(issue_file: IO[str], print_fn: Callable) -> None:
last_char: Optional[str] = None
issue_data = IssueData()
while True:
this_char = issue_file.read(1)
if not this_char:
break
if last_char == '\\':
print_fn(translate_issue_char(issue_data, this_char), end='')
elif last_char is not None:
print_fn(last_char, end='')
# `\\\a` should not match the last two slashes,
# so make it look like it was `\?\a` where `?`
# is some character other than `\`.
last_char = None if last_char == '\\' else this_char
if last_char is not None:
print_fn(last_char, end='')
def debug_config(opts: KittyOpts) -> str: def debug_config(opts: KittyOpts) -> str:
from io import StringIO from io import StringIO
out = StringIO() out = StringIO()
@ -224,7 +221,7 @@ def debug_config(opts: KittyOpts) -> str:
p(' '.join(subprocess.check_output(['sw_vers']).decode('utf-8').splitlines()).strip()) p(' '.join(subprocess.check_output(['sw_vers']).decode('utf-8').splitlines()).strip())
if os.path.exists('/etc/issue'): if os.path.exists('/etc/issue'):
with open('/etc/issue', encoding='utf-8', errors='replace') as f: with open('/etc/issue', encoding='utf-8', errors='replace') as f:
print_issue(f, p) p(end=''.join(IssueData().parse_issue_file(f)))
if os.path.exists('/etc/lsb-release'): if os.path.exists('/etc/lsb-release'):
with open('/etc/lsb-release', encoding='utf-8', errors='replace') as f: with open('/etc/lsb-release', encoding='utf-8', errors='replace') as f:
p(f.read().strip()) p(f.read().strip())

View File

@ -1,5 +1,7 @@
import subprocess import subprocess
from kitty.fast_data_types import num_users from kitty.fast_data_types import num_users
from . import BaseTest from . import BaseTest