Add initial version of Sonowrite

This commit is contained in:
Olivier 'reivilibre' 2023-01-16 21:39:03 +00:00
commit 1c5b6d745b
3 changed files with 78 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/.idea
__pycache__

14
pyproject.toml Normal file
View File

@ -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"

61
sonowrite/__init__.py Normal file
View File

@ -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)