Add some tests for easing function parsing

This commit is contained in:
Kovid Goyal
2024-07-17 11:11:39 +05:30
parent fc13b06b35
commit e927f8da62
2 changed files with 39 additions and 14 deletions

View File

@@ -1394,11 +1394,14 @@ def parse_font_spec(spec: str) -> FontSpec:
return FontSpec.from_setting(spec) return FontSpec.from_setting(spec)
JumpTypes = Literal['start', 'end', 'none', 'both']
class EasingFunction(NamedTuple): class EasingFunction(NamedTuple):
type: Literal['steps', 'linear', 'cubic-bezier', ''] = '' type: Literal['steps', 'linear', 'cubic-bezier', ''] = ''
num_steps: int = 0 num_steps: int = 0
jump_type: Literal['start', 'end', 'none', 'both'] = 'end' jump_type: JumpTypes = 'end'
linear_x: Tuple[float, ...] = () linear_x: Tuple[float, ...] = ()
linear_y: Tuple[float, ...] = () linear_y: Tuple[float, ...] = ()
@@ -1418,7 +1421,7 @@ class EasingFunction(NamedTuple):
if len(parts) != 4: if len(parts) != 4:
raise ValueError('cubic-bezier easing function must have four points') raise ValueError('cubic-bezier easing function must have four points')
return cls(type='cubic-bezier', cubic_bezier_points=( return cls(type='cubic-bezier', cubic_bezier_points=(
unit_float(parts[0]), float(parts[1]), unit_float(parts[2]), float(parts[2]))) unit_float(parts[0]), float(parts[1]), unit_float(parts[2]), float(parts[3])))
@classmethod @classmethod
def linear(cls, params: str) -> 'EasingFunction': def linear(cls, params: str) -> 'EasingFunction':
@@ -1432,12 +1435,16 @@ class EasingFunction(NamedTuple):
extra = len(yaxis) - len(xaxis) extra = len(yaxis) - len(xaxis)
if extra <= 0: if extra <= 0:
return return
start = xaxis[-1] if xaxis else 0 start = xaxis[-1] if xaxis else 0.
delta = (end - start) / (extra + 1) delta = (end - start) / max(1, extra - 1)
if delta <= 0: if delta <= 0.:
raise ValueError(f'Linear easing curve must have strictly increasing points: {params} does not') raise ValueError(f'Linear easing curve must have strictly increasing points: {params} does not')
for i in range(extra): if xaxis:
xaxis.append((i+1) * delta) for i in range(extra):
xaxis.append(start + (i+1) * delta)
else:
for i in range(extra):
xaxis.append(i * delta)
def add_point(y: float, x: Optional[float] = None) -> None: def add_point(y: float, x: Optional[float] = None) -> None:
if x is None: if x is None:
@@ -1463,18 +1470,18 @@ class EasingFunction(NamedTuple):
balance(1) balance(1)
return cls(type='linear', linear_x=tuple(xaxis), linear_y=tuple(yaxis)) return cls(type='linear', linear_x=tuple(xaxis), linear_y=tuple(yaxis))
@classmethod @classmethod
def steps(cls, params: str) -> 'EasingFunction': def steps(cls, params: str) -> 'EasingFunction':
parts = params.replace(',', ' ').split() parts = params.replace(',', ' ').split()
jump_type = 'end' jump_type: JumpTypes = 'end'
if len(parts) == 2: if len(parts) == 2:
n = int(parts[0]) n = int(parts[0])
jt = parts[1] jt = parts[1]
mapping: Dict[str, JumpTypes] = {
'jump-start': 'start', 'start': 'start', 'end': 'end', 'jump-end': 'end', 'jump-none': 'none', 'jump-both': 'both'
}
try: try:
jump_type = { jump_type = mapping[jt.lower()]
'jump-start': 'start', 'start': 'start', 'end': 'end', 'jump-end': 'end', 'jump-none': 'none', 'jump-both': 'both'
}[jt.lower()]
except KeyError: except KeyError:
raise KeyError(f'{jt} is not a valid jump type for a linear easing function') raise KeyError(f'{jt} is not a valid jump type for a linear easing function')
if jump_type == 'none': if jump_type == 'none':
@@ -1483,7 +1490,7 @@ class EasingFunction(NamedTuple):
n = max(1, n) n = max(1, n)
else: else:
n = max(1, int(parts[0])) n = max(1, int(parts[0]))
return cls(type='steps', jump_type=jump_type, num_steps=n) # type: ignore return cls(type='steps', jump_type=jump_type, num_steps=n)
def cursor_blink_interval(spec: str) -> Tuple[float, EasingFunction, EasingFunction]: def cursor_blink_interval(spec: str) -> Tuple[float, EasingFunction, EasingFunction]:

View File

@@ -3,7 +3,7 @@
from kitty.fast_data_types import Color from kitty.fast_data_types import Color
from kitty.options.utils import DELETE_ENV_VAR from kitty.options.utils import DELETE_ENV_VAR, EasingFunction
from kitty.utils import log_error from kitty.utils import log_error
from . import BaseTest from . import BaseTest
@@ -137,3 +137,21 @@ class TestConfParsing(BaseTest):
" \\ blue") " \\ blue")
self.ae(opts.font_size, 12.35) self.ae(opts.font_size, 12.35)
self.ae(opts.color25, Color(0, 0, 255)) self.ae(opts.color25, Color(0, 0, 255))
# cursor_blink_interval
def cb(src, interval=-1, first=EasingFunction(), second=EasingFunction()):
opts = p('cursor_blink_interval ' + src)
self.ae((float(interval), first, second), (float(opts.cursor_blink_interval[0]), opts.cursor_blink_interval[1], opts.cursor_blink_interval[2]))
cb('3', 3)
cb('-2.3', -2.3)
cb('linear', first=EasingFunction('cubic-bezier', cubic_bezier_points=(0, 0.0, 1.0, 1.0)))
cb('linear 19', 19, EasingFunction('cubic-bezier', cubic_bezier_points=(0, 0.0, 1.0, 1.0)))
cb('ease-in-out cubic-bezier(0.1, 0.2, 0.3, 0.4) 11', 11,
EasingFunction('cubic-bezier', cubic_bezier_points=(0.42, 0, 0.58, 1)),
EasingFunction('cubic-bezier', cubic_bezier_points=(0.1, 0.2, 0.3, 0.4))
)
cb('step-start', first=EasingFunction('steps', num_steps=1, jump_type='start'))
cb('steps(7, jump-none)', first=EasingFunction('steps', num_steps=7, jump_type='none'))
cb('linear(0, 0.25, 1)', first=EasingFunction('linear', linear_x=(0.0, 0.5, 1.0), linear_y=(0, 0.25, 1.0)))
cb('linear(0, 0.25 75%, 1)', first=EasingFunction('linear', linear_x=(0.0, 0.75, 1.0), linear_y=(0, 0.25, 1.0)))
cb('linear(0, 0.25 25% 75%, 1)', first=EasingFunction('linear', linear_x=(0.0, 0.25, 0.75, 1.0), linear_y=(0, 0.25, 0.25, 1.0)))