8000 fix: preserve Windows accented input and navigation by rafatahmed · Pull Request #235 · rafatahmed/Flahax · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ each release, with numbers, sources and decisions, are in

### Fixed

- Preserve accented Windows input such as à without consuming the next character in
`flahax mix`; retain arrow navigation, editing keys and held-key repeats (#127).

- Capture the PHREEQC product fixtures from the packaged catalogue (Micro Combi Mix 2 replaces the retired CH - micro; the citrate case is dropped as no catalogue product holds citrate), so the golden test runs the engine's label reconciliation without a patch (#222).

- Normalize POSIX mode-inspection/setup/restoration failures and attempt rollback
Expand Down
2 changes: 2 additions & 0 deletions docs/releases/0.7.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

The full text of every change in 0.7.0, as recorded when each was merged, newest first. The [changelog](../../CHANGELOG.md) groups them into a short summary. 0.7.0 is not yet released.

- Windows `flahax mix` reads Unicode console key events instead of ambiguous character prefixes (#127). Typing à no longer consumes the next character or turns H/P/K/M into arrow actions. Navigation, editing, held-key repeats and Alt+numpad characters remain supported. Native hidden-console tests exercise both the terminal reader and the mix text-entry and selection widgets; POSIX input is unchanged.

- A product label is reconciled with its compound instead of refused (decision G5, #214). `flahax.label_reconciliation` takes the charge the declared ions of the compound cannot balance from the excess side's declared nutrients in proportion to their charge contribution and reports it as `unassigned_percent`; chemistry uses the label minus that amount and the nutrient fit keeps the label. A label that leaves a charged ion unstated (a chelate's sodium, a carbonate) or states one side only (an acid, urea, KOH) is closed by that ion, so nothing is unassigned. A blend has no defined compound and keeps its label, with the note "has no defined compound: its label analysis is used as declared". Potassium nitrate at N 13.0 and K 38.2 leaves about 1.91 % K unassigned. The 1 % label charge gate (`materials._CHARGE_RESIDUAL_LIMIT_PERCENT`), the grade-range exclusion test and the `identity_inconsistent` refusals in `mix_cli` and `fertilizer_workflow` are removed; a label naming a nutrient the compound's ions do not carry is still refused.
- The pH needs no declaration (decisions G1–G4, #215). `calculate_equilibrium_ph(totals, *, phases, temperature_c=25.0, carbon_boundary='closed', residual_charge=0.0)` no longer takes `complete_analysis` and solves to the net charge `residual_charge` (eq/kg water) instead of zero. In the workflow, a water with neither an `HCO3` key nor a measured alkalinity gives the chemistry and pH stages `needs_input` with the message "measure alkalinity (HCO3) of the water"; an ionic strength above 0.1 mol/kg gives `not_calculated` with "ionic strength above 0.1 mol/kg: outside the activity model's range"; `HCO3: 0` is a measured zero and is calculated. The recipe is produced in all three cases. The summary lists the stated model conditions: closed carbon, 25 °C only, and 1 L of solution taken as 1 kg of water.
- A water analysis is taken as measured (decisions G10 and G11, #216). `balance_water_analysis` no longer rejects an imbalance (the 5 % limit and `charge_imbalance_exceeded`), no longer closes on Cl-, Na+ or an adjusted carbonate (`balance_ion`, `maximum_imbalance_percent`, `balance_ion_uncertainty_percent` and `fixed` are removed, as are `missing_balance_ion`, `fixed_balance_ion`, `reconciliation_out_of_bounds` and `closed_on_alkalinity`), and returns `imbalance_percent` and `residual_charge_eq`, which the pH solves, the pH-adjustment planner and the joint problem's reference charge carry. Carbon taken from a titrated alkalinity stays, because it comes from a measurement. `calculate_ec_from_totals` takes `residual_charge_eq_per_kgw` for its calculated-pH mode, and a reagent search that leaves the activity model's range gives the pH-adjustment stage `not_calculated`. A water file with `balance_ion` or `balance_ion_uncertainty_percent` is refused with `unknown_field`. The 0.1 % initial-charge gates of the acid-option and delivery planners (`charge_imbalance`) are removed because their doses are found from charge differences. The formula-imbalance flag `exceedsWarning` and `ION_BALANCE_WARNING_PCT` are removed; `recommend` states the imbalance as "The formula's declared ions are not balanced: cations {c} vs anions {a} meq/L ({p} %)." and never claims no salt combination meets the targets.
Expand Down
93 changes: 83 additions & 10 deletions src/flahax/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
# its results are calculations, not dosing instructions.
"""The console for the ``flahax mix`` full-screen wizard, from the standard library only.

Windows uses ``msvcrt`` for keys and ``ctypes`` to switch on ANSI output; Linux and macOS use
Windows uses Win32 key events and ``ctypes`` to switch on ANSI output; Linux and macOS use
``termios``, ``tty`` and ``select``. Keys come back as names: printable characters as
themselves, and UP, DOWN, LEFT, RIGHT, HOME, END, ENTER, ESC, TAB, SHIFT_TAB, BACKSPACE and
CTRL_U. Ctrl+C raises KeyboardInterrupt. The console is always restored on exit.
"""

import ctypes
from ctypes import wintypes
import os
import shutil
import sys
Expand Down Expand Up @@ -56,6 +58,14 @@
'O': 'END',
'\x0f': 'SHIFT_TAB',
}
WINDOWS_NAVIGATION = {
0x26: 'UP',
0x28: 'DOWN',
0x25: 'LEFT',
0x27: 'RIGHT',
0x24: 'HOME',
0x23: 'END',
}
CONTROL = {
'\r': 'ENTER',
'\n': 'ENTER',
Expand All @@ -82,6 +92,25 @@
}


class _KeyEvent(ctypes.Structure):
_fields_ = [
('down', wintypes.BOOL),
('repeat', wintypes.WORD),
('virtual', wintypes.WORD),
('scan', wintypes.WORD),
('char', wintypes.WCHAR),
('state', wintypes.DWORD),
]


class _InputEvent(ctypes.Union):
_fields_ = [('key', _KeyEvent), ('padding', ctypes.c_byte * 16)]


class _InputRecord(ctypes.Structure):
_fields_ = [('kind', wintypes.WORD), ('event', _InputEvent)]


class TerminalUnavailable(OSError):
"""The console cannot be put in the full-screen mode; use the line-by-line questions."""

Expand All @@ -100,8 +129,9 @@ def decode_windows(chars):


def read_windows_key(getwch):
"""One key from ``msvcrt.getwch``: after a prefix code the next code is always read.
"""Decode a legacy ``msvcrt.getwch`` stream, which cannot distinguish a-grave.

``Terminal`` reads Win32 events instead; this helper retains the old stream contract.
Windows sends arrows and other special keys as two codes, the first being 0x00 or 0xE0.
``kbhit`` cannot tell whether the second code is coming (in Windows Terminal it reports
False until it is read), so the letter a-grave, which is also 0xE0, cannot be told apart
Expand Down Expand Up @@ -163,6 +193,18 @@ def _kernel32():
kernel.GetConsoleMode.restype = wintypes.BOOL
kernel.SetConsoleMode.argtypes = [wintypes.HANDLE, wintypes.DWORD]
kernel.SetConsoleMode.restype = wintypes.BOOL
kernel.GetNumberOfConsoleInputEvents.argtypes = [
wintypes.HANDLE,
ctypes.POINTER(wintypes.DWORD),
]
kernel.GetNumberOfConsoleInputEvents.restype = wintypes.BOOL
kernel.ReadConsoleInputW.argtypes = [
wintypes.HANDLE,
ctypes.POINTER(_InputRecord),
wintypes.DWORD,
ctypes.POINTER(wintypes.DWORD),
]
kernel.ReadConsoleInputW.restype = wintypes.BOOL
return kernel


Expand Down Expand Up @@ -270,14 +312,7 @@ def read_key(self, timeout=0.1):
if self._pending:
return self._pending.pop(0)
if os.name == 'nt':
import msvcrt

deadline = time.monotonic() + timeout
while not msvcrt.kbhit():
if time.monotonic() >= deadline:
return None
time.sleep(0.01)
return read_windows_key(msvcrt.getwch)
return self._read_windows_event(timeout)
import select

fd = sys.stdin.fileno()
Expand All @@ -294,6 +329,44 @@ def read_key(self, timeout=0.1):
self._pending = decode_posix(data)
return self._pending.pop(0) if self._pending else None

def _read_windows_event(self, timeout):
kernel = _kernel32()
handle = kernel.GetStdHandle(-10 & 0xFFFFFFFF)
deadline = time.monotonic() + timeout
count = wintypes.DWORD()
while True:
if not kernel.GetNumberOfConsoleInputEvents(handle, ctypes.byref(count)):
raise OSError('cannot read console input events')
if count.value:
event = _InputRecord()
if not kernel.ReadConsoleInputW(
handle, ctypes.byref(event), 1, ctypes.byref(count)
):
raise OSError('cannot read console input event')
if not count.value:
raise EOFError('terminal input closed')
if event.kind == 1:
key = event.event.key
# Alt+numpad emits the composed character on Alt release.
if not key.down and not (key.virtual == 0x12 and key.char != '\0'):
if time.monotonic() >= deadline:
return None
continue
decoded = None
if key.virtual == 0x09 and key.state & 0x10:
decoded = 'SHIFT_TAB'
elif key.char != '\0':
decoded = _control(key.char)
else:
decoded = WINDOWS_NAVIGATION.get(key.virtual)
if decoded is not None:
self._pending.extend([decoded] * (max(1, key.repeat) - 1))
return decoded
if time.monotonic() >= deadline:
return None
if not count.value:
time.sleep(0.01)

def size(self):
"""(columns, rows) of the window."""
return tuple(shutil.get_terminal_size((80, 24)))
Expand Down
1 change: 1 addition & 0 deletions tests/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def test_windows_arrows_without_kbhit(self):
):
reads = iter([first, second])
self.assertEqual(read_windows_key(lambda: next(reads)), key)
self.assertEqual(read_windows_key(lambda: 'H'), 'H')

def test_decode_posix(self):
self.assertEqual(decode_posix(b'\x1b[A'), ['UP'])
Expand Down
105 changes: 103 additions & 2 deletions tests/test_terminal_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,13 @@ def test_windows_console_mode_failures_are_explicit(self):
Terminal(io.StringIO()).__enter__()

def test_windows_poll_timeout_yields_without_input(self):
keys = types.SimpleNamespace(kbhit=lambda: False)
kernel = types.SimpleNamespace(
GetStdHandle=lambda _: 1,
GetNumberOfConsoleInputEvents=lambda *_: True,
)
with (
patch('flahax.terminal.os.name', 'nt'),
patch.dict('sys.modules', {'msvcrt': keys}),
patch('flahax.terminal._kernel32', return_value=kernel),
patch('flahax.terminal.time.monotonic', side_effect=[0, 0.01, 0.2]),
patch('flahax.terminal.time.sleep') as sleep,
):
Expand All @@ -212,6 +215,33 @@ def test_unicode_unknown_escape_resize_and_cursor(self):
term.show_cursor(False)
self.assertEqual(term.stream.getvalue(), '\x1b[?25h\x1b[?25l')

def test_windows_input_failures_do_not_return_phantom_keys(self):
def available(handle, count):
count._obj.value = 1
return True

for poll, read, error in (
(lambda *_: False, lambda *_: True, OSError),
(available, lambda *_: False, OSError),
(
available,
lambda handle, event, size, count: setattr(count._obj, 'value', 0) or True,
EOFError,
),
):
kernel = types.SimpleNamespace(
GetStdHandle=lambda _: 1,
GetNumberOfConsoleInputEvents=poll,
ReadConsoleInputW=read,
)
with (
self.subTest(error=error, poll=poll),
patch('flahax.terminal.os.name', 'nt'),
patch('flahax.terminal._kernel32', return_value=kernel),
self.assertRaises(error),
):
Terminal(io.StringIO()).read_key(0)


class NativePlatformTests(unittest.TestCase):
@unittest.skipUnless(os.name == 'nt', 'requires a native Windows console')
Expand Down Expand Up @@ -412,6 +442,77 @@ class Input(ctypes.Structure):
)
assert written.value == 1
assert term.read_key(0.5) == 'م'
# Printable U+00E0 is not the legacy extended-key prefix.
for char in ('à', 'H'):
event = Input(1, Event(key=Key(True, 1, 0, 0, char, 0)))
assert kernel.WriteConsoleInputW(
handles[0], ctypes.byref(event), 1, ctypes.byref(written)
)
assert term.read_key(0.5) == 'à'
assert term.read_key(0.5) == 'H'
for virtual, char, state, expected in (
(0x26, '\0', 0, 'UP'),
(0x28, '\0', 0, 'DOWN'),
(0x25, '\0', 0, 'LEFT'),
(0x27, '\0', 0, 'RIGHT'),
(0x24, '\0', 0, 'HOME'),
(0x23, '\0', 0, 'END'),
(0x09, '\t', 0x10, 'SHIFT_TAB'),
(0x09, '\t', 0, 'TAB'),
(0x0D, '\r', 0, 'ENTER'),
(0x08, '\b', 0, 'BACKSPACE'),
(0x1B, '\x1b', 0, 'ESC'),
):
event = Input(1, Event(key=Key(True, 1, virtual, 0, char, state)))
assert kernel.WriteConsoleInputW(
handles[0], ctypes.byref(event), 1, ctypes.byref(written)
)
assert term.read_key(0.5) == expected, expected
event = Input(1, Event(key=Key(True, 3, 0, 0, 'à', 0)))
assert kernel.WriteConsoleInputW(
handles[0], ctypes.byref(event), 1, ctypes.byref(written)
)
assert [term.read_key(0.1) for _ in range(3)] == ['à', 'à', 'à']
# Windows delivers an Alt+numpad character on Alt release.
event = Input(1, Event(key=Key(False, 1, 0x12, 0, 'à', 0)))
assert kernel.WriteConsoleInputW(
handles[0], ctypes.byref(event), 1, ctypes.byref(written)
)
assert term.read_key(0.1) == 'à'
from flahax.mix_tui import TuiPrompts
from flahax.terminal import glyphs

# Exercise the real mix widgets through the native reader.
for char in 'àHPKMx\b\r':
event = Input(1, Event(key=Key(True, 1, 0, 0, char, 0)))
assert kernel.WriteConsoleInputW(
handles[0], ctypes.byref(event), 1, ctypes.byref(written)
)
prompts = TuiPrompts(term, glyphs('utf-8'))
assert prompts.text('Session name') == 'àHPKM'
for virtual, char in ((0x28, '\0'), (0x28, '\0'), (0x26, '\0'), (13, '\r')):
event = Input(1, Event(key=Key(True, 1, virtual, 0, char, 0)))
assert kernel.WriteConsoleInputW(
handles[0], ctypes.byref(event), 1, ctypes.byref(written)
)
assert prompts.choose('Formula', ['first', 'second', 'third']) == 2
# Releases and non-key records must not become text or stop polling.
event = Input(1, Event(key=Key(False, 1, 0x41, 0, 'a', 0)))
assert kernel.WriteConsoleInputW(
handles[0], ctypes.byref(event), 1, ctypes.byref(written)
)
assert term.read_key(0) is None
for kind, down, virtual, char in (
(1, False, 0x41, 'a'),
(0x10, False, 0, '\0'), # focus event
(1, True, 0x10, '\0'), # Shift alone
(1, True, 0, '\x15'),
):
event = Input(kind, Event(key=Key(down, 1, virtual, 0, char, 0)))
assert kernel.WriteConsoleInputW(
handles[0], ctypes.byref(event), 1, ctypes.byref(written)
)
assert term.read_key(0.5) == 'CTRL_U'
term.draw(['native console restore witness'])
raise RuntimeError('restore witness')
except RuntimeError as exc:
Expand Down
Loading
0