mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-02-04 04:06:06 -05:00
scripts: mypy fixes
This commit is contained in:
parent
cb1e152d99
commit
bbebe0f330
5 changed files with 77 additions and 51 deletions
|
|
@ -11,7 +11,7 @@ import termios
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main() -> None:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('files', type=argparse.FileType('rb'), nargs='+')
|
parser.add_argument('files', type=argparse.FileType('rb'), nargs='+')
|
||||||
parser.add_argument('--iterations', type=int, default=20)
|
parser.add_argument('--iterations', type=int, default=20)
|
||||||
|
|
@ -24,12 +24,12 @@ def main():
|
||||||
termios.TIOCGWINSZ,
|
termios.TIOCGWINSZ,
|
||||||
struct.pack('HHHH', 0, 0, 0, 0)))
|
struct.pack('HHHH', 0, 0, 0, 0)))
|
||||||
|
|
||||||
times = {name: [] for name in [f.name for f in args.files]}
|
times: dict[str, list[float]] = {name: [] for name in [f.name for f in args.files]}
|
||||||
|
|
||||||
for f in args.files:
|
for f in args.files:
|
||||||
bench_bytes = f.read()
|
bench_bytes = f.read()
|
||||||
|
|
||||||
for i in range(args.iterations):
|
for _ in range(args.iterations):
|
||||||
start = datetime.now()
|
start = datetime.now()
|
||||||
sys.stdout.buffer.write(bench_bytes)
|
sys.stdout.buffer.write(bench_bytes)
|
||||||
stop = datetime.now()
|
stop = datetime.now()
|
||||||
|
|
@ -48,4 +48,4 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
main()
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ import struct
|
||||||
import sys
|
import sys
|
||||||
import termios
|
import termios
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
class ColorVariant(enum.IntEnum):
|
class ColorVariant(enum.IntEnum):
|
||||||
NONE = enum.auto()
|
NONE = enum.auto()
|
||||||
|
|
@ -17,7 +19,7 @@ class ColorVariant(enum.IntEnum):
|
||||||
RGB = enum.auto()
|
RGB = enum.auto()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main() -> None:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'out', type=argparse.FileType(mode='w'), nargs='?', help='name of output file')
|
'out', type=argparse.FileType(mode='w'), nargs='?', help='name of output file')
|
||||||
|
|
@ -38,10 +40,16 @@ def main():
|
||||||
opts = parser.parse_args()
|
opts = parser.parse_args()
|
||||||
out = opts.out if opts.out is not None else sys.stdout
|
out = opts.out if opts.out is not None else sys.stdout
|
||||||
|
|
||||||
|
lines: int | None = None
|
||||||
|
cols: int | None = None
|
||||||
|
width: int | None = None
|
||||||
|
height: int | None = None
|
||||||
|
|
||||||
if opts.rows is None or opts.cols is None:
|
if opts.rows is None or opts.cols is None:
|
||||||
try:
|
try:
|
||||||
def dummy(*args):
|
def dummy(*args: Any) -> None:
|
||||||
"""Need a handler installed for sigwait() to trigger."""
|
"""Need a handler installed for sigwait() to trigger."""
|
||||||
|
_ = args
|
||||||
pass
|
pass
|
||||||
signal.signal(signal.SIGWINCH, dummy)
|
signal.signal(signal.SIGWINCH, dummy)
|
||||||
|
|
||||||
|
|
@ -53,6 +61,9 @@ def main():
|
||||||
termios.TIOCGWINSZ,
|
termios.TIOCGWINSZ,
|
||||||
struct.pack('HHHH', 0, 0, 0, 0)))
|
struct.pack('HHHH', 0, 0, 0, 0)))
|
||||||
|
|
||||||
|
assert width is not None
|
||||||
|
assert height is not None
|
||||||
|
|
||||||
if width > 0 and height > 0:
|
if width > 0 and height > 0:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
@ -71,9 +82,11 @@ def main():
|
||||||
|
|
||||||
if opts.rows is not None:
|
if opts.rows is not None:
|
||||||
lines = opts.rows
|
lines = opts.rows
|
||||||
|
assert lines is not None
|
||||||
height = 15 * lines # PGO helper binary hardcodes cell height to 15px
|
height = 15 * lines # PGO helper binary hardcodes cell height to 15px
|
||||||
if opts.cols is not None:
|
if opts.cols is not None:
|
||||||
cols = opts.cols
|
cols = opts.cols
|
||||||
|
assert cols is not None
|
||||||
width = 8 * cols # PGO help binary hardcodes cell width to 8px
|
width = 8 * cols # PGO help binary hardcodes cell width to 8px
|
||||||
|
|
||||||
if lines is None or cols is None or height is None or width is None:
|
if lines is None or cols is None or height is None or width is None:
|
||||||
|
|
@ -190,8 +203,8 @@ def main():
|
||||||
# The sixel 'alphabet'
|
# The sixel 'alphabet'
|
||||||
sixels = '?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
|
sixels = '?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
|
||||||
|
|
||||||
last_pos = None
|
last_pos: tuple[int, int] | None = None
|
||||||
last_size = None
|
last_size: tuple[int, int] = 0, 0
|
||||||
|
|
||||||
for _ in range(20):
|
for _ in range(20):
|
||||||
if last_pos is not None and random.randrange(2):
|
if last_pos is not None and random.randrange(2):
|
||||||
|
|
@ -254,4 +267,4 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
main()
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,10 @@
|
||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
|
||||||
|
|
||||||
from typing import Dict, Union
|
|
||||||
|
|
||||||
|
|
||||||
class Capability:
|
class Capability:
|
||||||
def __init__(self, name: str, value: Union[bool, int, str]):
|
def __init__(self, name: str, value: bool | int | str) -> None:
|
||||||
self._name = name
|
self._name = name
|
||||||
self._value = value
|
self._value = value
|
||||||
|
|
||||||
|
|
@ -18,30 +15,42 @@ class Capability:
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self) -> Union[bool, int, str]:
|
def value(self) -> bool | int | str:
|
||||||
return self._value
|
return self._value
|
||||||
|
|
||||||
def __lt__(self, other):
|
def __lt__(self, other: object) -> bool:
|
||||||
|
if not isinstance(other, Capability):
|
||||||
|
return NotImplemented
|
||||||
return self._name < other._name
|
return self._name < other._name
|
||||||
|
|
||||||
def __le__(self, other):
|
def __le__(self, other: object) -> bool:
|
||||||
|
if not isinstance(other, Capability):
|
||||||
|
return NotImplemented
|
||||||
return self._name <= other._name
|
return self._name <= other._name
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other: object) -> bool:
|
||||||
|
if not isinstance(other, Capability):
|
||||||
|
return NotImplemented
|
||||||
return self._name == other._name
|
return self._name == other._name
|
||||||
|
|
||||||
def __ne__(self, other):
|
def __ne__(self, other: object) -> bool:
|
||||||
return self._name != other._name
|
if not isinstance(other, Capability):
|
||||||
|
return NotImplemented
|
||||||
|
return bool(self._name != other._name)
|
||||||
|
|
||||||
def __gt__(self, other):
|
def __gt__(self, other: object) -> bool:
|
||||||
return self._name > other._name
|
if not isinstance(other, Capability):
|
||||||
|
return NotImplemented
|
||||||
|
return bool(self._name > other._name)
|
||||||
|
|
||||||
def __ge__(self, other):
|
def __ge__(self, other: object) -> bool:
|
||||||
|
if not isinstance(other, Capability):
|
||||||
|
return NotImplemented
|
||||||
return self._name >= other._name
|
return self._name >= other._name
|
||||||
|
|
||||||
|
|
||||||
class BoolCapability(Capability):
|
class BoolCapability(Capability):
|
||||||
def __init__(self, name: str):
|
def __init__(self, name: str) -> None:
|
||||||
super().__init__(name, True)
|
super().__init__(name, True)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -50,11 +59,11 @@ class IntCapability(Capability):
|
||||||
|
|
||||||
|
|
||||||
class StringCapability(Capability):
|
class StringCapability(Capability):
|
||||||
def __init__(self, name: str, value: str):
|
def __init__(self, name: str, value: str) -> None:
|
||||||
# see terminfo(5) for valid escape sequences
|
# see terminfo(5) for valid escape sequences
|
||||||
|
|
||||||
# Control characters
|
# Control characters
|
||||||
def translate_ctrl_chr(m):
|
def translate_ctrl_chr(m: re.Match[str]) -> str:
|
||||||
ctrl = m.group(1)
|
ctrl = m.group(1)
|
||||||
if ctrl == '?':
|
if ctrl == '?':
|
||||||
return '\\x7f'
|
return '\\x7f'
|
||||||
|
|
@ -83,10 +92,10 @@ class StringCapability(Capability):
|
||||||
|
|
||||||
|
|
||||||
class Fragment:
|
class Fragment:
|
||||||
def __init__(self, name: str, description: str):
|
def __init__(self, name: str, description: str) -> None:
|
||||||
self._name = name
|
self._name = name
|
||||||
self._description = description
|
self._description = description
|
||||||
self._caps = {}
|
self._caps = dict[str, Capability]()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
|
|
@ -97,18 +106,18 @@ class Fragment:
|
||||||
return self._description
|
return self._description
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def caps(self) -> Dict[str, Capability]:
|
def caps(self) -> dict[str, Capability]:
|
||||||
return self._caps
|
return self._caps
|
||||||
|
|
||||||
def add_capability(self, cap: Capability):
|
def add_capability(self, cap: Capability) -> None:
|
||||||
assert cap.name not in self._caps
|
assert cap.name not in self._caps
|
||||||
self._caps[cap.name] = cap
|
self._caps[cap.name] = cap
|
||||||
|
|
||||||
def del_capability(self, name: str):
|
def del_capability(self, name: str) -> None:
|
||||||
del self._caps[name]
|
del self._caps[name]
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main() -> None:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('source_entry_name')
|
parser.add_argument('source_entry_name')
|
||||||
parser.add_argument('source', type=argparse.FileType('r'))
|
parser.add_argument('source', type=argparse.FileType('r'))
|
||||||
|
|
@ -121,15 +130,15 @@ def main():
|
||||||
source = opts.source
|
source = opts.source
|
||||||
target = opts.target
|
target = opts.target
|
||||||
|
|
||||||
lines = []
|
lines = list[str]()
|
||||||
for l in source.readlines():
|
for line in source.readlines():
|
||||||
l = l.strip()
|
line = line.strip()
|
||||||
if l.startswith('#'):
|
if line.startswith('#'):
|
||||||
continue
|
continue
|
||||||
lines.append(l)
|
lines.append(line)
|
||||||
|
|
||||||
fragments = {}
|
fragments = dict[str, Fragment]()
|
||||||
cur_fragment = None
|
cur_fragment: Fragment | None = None
|
||||||
|
|
||||||
for m in re.finditer(
|
for m in re.finditer(
|
||||||
r'(?P<name>(?P<entry_name>[-+\w@]+)\|(?P<entry_desc>.+?),)|'
|
r'(?P<name>(?P<entry_name>[-+\w@]+)\|(?P<entry_desc>.+?),)|'
|
||||||
|
|
@ -148,17 +157,20 @@ def main():
|
||||||
|
|
||||||
elif m.group('bool_cap') is not None:
|
elif m.group('bool_cap') is not None:
|
||||||
name = m.group('bool_name')
|
name = m.group('bool_name')
|
||||||
|
assert cur_fragment is not None
|
||||||
cur_fragment.add_capability(BoolCapability(name))
|
cur_fragment.add_capability(BoolCapability(name))
|
||||||
|
|
||||||
elif m.group('int_cap') is not None:
|
elif m.group('int_cap') is not None:
|
||||||
name = m.group('int_name')
|
name = m.group('int_name')
|
||||||
value = int(m.group('int_val'), 0)
|
int_value = int(m.group('int_val'), 0)
|
||||||
cur_fragment.add_capability(IntCapability(name, value))
|
assert cur_fragment is not None
|
||||||
|
cur_fragment.add_capability(IntCapability(name, int_value))
|
||||||
|
|
||||||
elif m.group('str_cap') is not None:
|
elif m.group('str_cap') is not None:
|
||||||
name = m.group('str_name')
|
name = m.group('str_name')
|
||||||
value = m.group('str_val')
|
str_value = m.group('str_val')
|
||||||
cur_fragment.add_capability(StringCapability(name, value))
|
assert cur_fragment is not None
|
||||||
|
cur_fragment.add_capability(StringCapability(name, str_value))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
assert False
|
assert False
|
||||||
|
|
@ -167,6 +179,9 @@ def main():
|
||||||
for frag in fragments.values():
|
for frag in fragments.values():
|
||||||
for cap in frag.caps.values():
|
for cap in frag.caps.values():
|
||||||
if cap.name == 'use':
|
if cap.name == 'use':
|
||||||
|
assert isinstance(cap, StringCapability)
|
||||||
|
assert isinstance(cap.value, str)
|
||||||
|
|
||||||
use_frag = fragments[cap.value]
|
use_frag = fragments[cap.value]
|
||||||
for use_cap in use_frag.caps.values():
|
for use_cap in use_frag.caps.values():
|
||||||
frag.add_capability(use_cap)
|
frag.add_capability(use_cap)
|
||||||
|
|
@ -188,7 +203,7 @@ def main():
|
||||||
entry.add_capability(IntCapability('RGB', 8)) # 8 bits per channel
|
entry.add_capability(IntCapability('RGB', 8)) # 8 bits per channel
|
||||||
entry.add_capability(StringCapability('query-os-name', os.uname().sysname))
|
entry.add_capability(StringCapability('query-os-name', os.uname().sysname))
|
||||||
|
|
||||||
terminfo_parts = []
|
terminfo_parts = list[str]()
|
||||||
for cap in sorted(entry.caps.values()):
|
for cap in sorted(entry.caps.values()):
|
||||||
name = cap.name
|
name = cap.name
|
||||||
value = str(cap.value)
|
value = str(cap.value)
|
||||||
|
|
@ -212,4 +227,4 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
main()
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
|
||||||
|
|
||||||
|
|
||||||
class Codepoint:
|
class Codepoint:
|
||||||
def __init__(self, start: int, end: None|int = None):
|
def __init__(self, start: int, end: None | int = None) -> None:
|
||||||
self.start = start
|
self.start = start
|
||||||
self.end = start if end is None else end
|
self.end = start if end is None else end
|
||||||
self.vs15 = False
|
self.vs15 = False
|
||||||
|
|
@ -15,7 +14,7 @@ class Codepoint:
|
||||||
return f'{self.start:x}-{self.end:x}, vs15={self.vs15}, vs16={self.vs16}'
|
return f'{self.start:x}-{self.end:x}, vs15={self.vs15}, vs16={self.vs16}'
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main() -> None:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('input', type=argparse.FileType('r'))
|
parser.add_argument('input', type=argparse.FileType('r'))
|
||||||
parser.add_argument('output', type=argparse.FileType('w'))
|
parser.add_argument('output', type=argparse.FileType('w'))
|
||||||
|
|
@ -100,4 +99,4 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
main()
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import math
|
import math
|
||||||
import sys
|
|
||||||
|
|
||||||
|
|
||||||
# Note: we use a pure gamma 2.2 function, rather than the piece-wise
|
# Note: we use a pure gamma 2.2 function, rather than the piece-wise
|
||||||
|
|
@ -17,7 +16,7 @@ def linear_to_srgb(f: float) -> float:
|
||||||
return math.pow(f, 1 / 2.2)
|
return math.pow(f, 1 / 2.2)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main() -> None:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('c_output', type=argparse.FileType('w'))
|
parser.add_argument('c_output', type=argparse.FileType('w'))
|
||||||
parser.add_argument('h_output', type=argparse.FileType('w'))
|
parser.add_argument('h_output', type=argparse.FileType('w'))
|
||||||
|
|
@ -68,4 +67,4 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
sys.exit(main())
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue