Coverage for src / osiris_cli / input_normalizer.py: 0%
18 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-31 05:01 +0200
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-31 05:01 +0200
1from __future__ import annotations
3from typing import Optional
6def normalize_user_input(text: Optional[str]) -> str:
7 """Normalize user-provided input for consistent rendering and history."""
8 if text is None:
9 return ""
11 lines = text.splitlines(keepends=True)
12 normalized_lines: list[str] = []
14 for line in lines:
15 has_newline = line.endswith('\n') or line.endswith('\r')
16 stripped = line.rstrip('\r\n')
17 expanded = stripped.replace('\t', ' ' * 4)
18 trimmed = expanded.rstrip()
19 if has_newline:
20 normalized_lines.append(trimmed + '\n')
21 else:
22 normalized_lines.append(trimmed)
24 # Preserve trailing newline at end if original text ended with newline
25 if text.endswith('\n') and normalized_lines and not normalized_lines[-1].endswith('\n'):
26 normalized_lines[-1] += '\n'
28 return ''.join(normalized_lines)