commit 1c5b6d745bad4ba0b5a27090154d38e9e0800e8e Author: Olivier 'reivilibre Date: Mon Jan 16 21:39:03 2023 +0000 Add initial version of Sonowrite diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd79fa5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea +__pycache__ + diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..8e5ae64 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,14 @@ +[tool.poetry] +name = "sonowrite" +version = "0.1.0" +description = "" +authors = ["Olivier 'reivilibre'"] + +[tool.poetry.dependencies] +python = "^3.8" + +[tool.poetry.dev-dependencies] + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" diff --git a/sonowrite/__init__.py b/sonowrite/__init__.py new file mode 100644 index 0000000..4d6dea2 --- /dev/null +++ b/sonowrite/__init__.py @@ -0,0 +1,61 @@ +import subprocess +import sys, tty +from pathlib import Path +from subprocess import PIPE + + +def speak(text: str) -> None: + #print(f"TALK {text}") + proc = subprocess.Popen(f"espeak --stdin --stdout | paplay", stdin=PIPE, shell=True) + proc.stdin.write(text.encode("UTF-8") + b"\n") + proc.stdin.flush() + proc.stdin.close() + exit_code = proc.wait() + if exit_code != 0: + raise ValueError("speak failed") + + +def main(target: Path) -> None: + tty.setcbreak(sys.stdin.fileno()) + sys.stdout.write("\n\n\n--- Sonowrite ---\n\n\n") + + textbuf = "" + wordbuf = "" + linebuf = "" + + with open(target, "w") as fout: + while True: + c = sys.stdin.read(1) + + #sys.stdout.write(repr(c)) + if c == "\x7f": # Backspace + sys.stdout.write("\x1b[D\x1b[K") + elif c == "@": + pass + else: + sys.stdout.write(c) + fout.write(c) + + sys.stdout.flush() + + if c in (" ", ".", ",", "\n"): + # echo previous word + speak(textbuf.lstrip(" \n.,")) + textbuf = "" + elif c == "@": + # TODO echo previous line? + speak(linebuf) + continue + elif c == "\x7f": + textbuf = textbuf[:-1] + wordbuf = wordbuf[:-1] + linebuf = linebuf[:-1] + + + textbuf += c + wordbuf += c + linebuf += c + +if __name__ == '__main__': + the_target = Path(sys.argv[1]) + main(the_target)