handle piped input for canned asciinema demos

This commit is contained in:
Paul Gauthier 2023-05-09 14:39:57 -07:00
parent 9b734cb149
commit 4c97799b42
2 changed files with 31 additions and 23 deletions

View file

@ -164,6 +164,8 @@ class Coder:
if self.num_control_c >= 2: if self.num_control_c >= 2:
break break
self.console.print("[bold red]^C again to quit") self.console.print("[bold red]^C again to quit")
except EOFError:
return
def run_loop(self): def run_loop(self):
if self.pretty: if self.pretty:
@ -336,6 +338,8 @@ class Coder:
for match in self.pattern.finditer(content): for match in self.pattern.finditer(content):
_, path, _, _, original, updated = match.groups() _, path, _, _, original, updated = match.groups()
path = path.strip()
if path not in self.fnames: if path not in self.fnames:
if not Path(path).exists(): if not Path(path).exists():
question = f"[red bold]Allow creation of new file {path}?" question = f"[red bold]Allow creation of new file {path}?"

View file

@ -5,6 +5,12 @@ from prompt_toolkit import prompt
from prompt_toolkit.completion import Completer, Completion from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.history import FileHistory from prompt_toolkit.history import FileHistory
from rich.console import Console
import sys
import time
import random
class FileContentCompleter(Completer): class FileContentCompleter(Completer):
def __init__(self, fnames): def __init__(self, fnames):
self.fnames = fnames self.fnames = fnames
@ -20,28 +26,29 @@ class FileContentCompleter(Completer):
with open(fname, "r") as f: with open(fname, "r") as f:
content = f.read() content = f.read()
for word in re.split(r'\W+', content): for word in re.split(r"\W+", content):
if word.startswith(last_word): if word.startswith(last_word):
yield Completion(word, start_position=-len(last_word)) yield Completion(word, start_position=-len(last_word))
from rich.console import Console def canned_input():
import sys
import time
import random
console = Console() console = Console()
def get_input(history_file, fnames):
if not sys.stdin.isatty():
input_line = input() input_line = input()
console.print("> ", end="", style="green") console.print("> ", end="", style="green")
for char in input_line: for char in input_line:
console.print(char, end="", style="green", flush=True) console.print(char, end="", style="green")
time.sleep(random.uniform(0.05, 0.1)) time.sleep(random.uniform(0.01, 0.15))
console.print() console.print()
console.print() console.print()
return input_line return input_line
def get_input(history_file, fnames):
if not sys.stdin.isatty():
return canned_input()
inp = "" inp = ""
multiline_input = False multiline_input = False
@ -54,15 +61,12 @@ def get_input(history_file, fnames):
else: else:
show = "> " show = "> "
try:
line = prompt( line = prompt(
show, show,
completer=completer_instance, completer=completer_instance,
history=FileHistory(history_file), history=FileHistory(history_file),
style=style, style=style,
) )
except EOFError:
return
if line.strip() == "{" and not multiline_input: if line.strip() == "{" and not multiline_input:
multiline_input = True multiline_input = True
continue continue