diff --git a/.gitignore b/.gitignore index d787b70..96ed0be 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /result +/demos/*.cast diff --git a/README.md b/README.md index fcea884..add3afb 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,9 @@ part of a pipe. When calling a program, it can decorate stdout and stderr differently. stdecor is specially useful when running multiple jobs in the same -shell. +shell. It can be used to build scripts like the following: + +![demo-ping](demos/demo-ping.gif) ## Installation diff --git a/demos/build-all.sh b/demos/build-all.sh new file mode 100755 index 0000000..6bd30fc --- /dev/null +++ b/demos/build-all.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +# agg=agg +agg=(docker run --rm -it -u "$(id -u):$(id -g)" -v "$PWD:/data" lpenz/agg:1.4.3) + +set -e -x + +for demofile in ./demos/*.py; do + name="${demofile##*/}" + name="${name//.py/}" + asciinema rec --overwrite \ + --rows 25 --cols 100 \ + -c "$demofile" "demos/${name}.cast" + "${agg[@]}" \ + --speed 1 \ + --theme asciinema \ + "demos/${name}.cast" "demos/${name}.gif" +done diff --git a/demos/demo-ping.gif b/demos/demo-ping.gif new file mode 100644 index 0000000..ca4421a Binary files /dev/null and b/demos/demo-ping.gif differ diff --git a/demos/demo-ping.py b/demos/demo-ping.py new file mode 100755 index 0000000..f30e021 --- /dev/null +++ b/demos/demo-ping.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +"""Generate stdecor ping demo using pexpect""" + +import argparse +import os +import sys +import time + +import pexpect + + +class Session(pexpect.spawn): + def __init__(self): + env = dict(os.environ) + env.update({"PS1": "\\$ ", "PS2": ""}) + pexpect.spawn.__init__( + self, + "bash", + ["--norc"], + encoding="utf-8", + timeout=10, + echo=False, + logfile=sys.stdout, + env=env, + ) + self._prompt = "[#$] $" + self.exp_prompt() + + def exp_prompt(self): + self.expect(self._prompt) + + def send(self, string, slow=None): + if slow: + prev = self.delaybeforesend + self.delaybeforesend = slow + for char in string: + if char == "\r": + time.sleep(slow * 5) + pexpect.spawn.send(self, char) + self.delaybeforesend = prev + else: + pexpect.spawn.send(self, string) + + def send_cmd(self, cmd, slow=None): + self.send(cmd, slow=slow) + if slow: + time.sleep(slow * 5) + self.send("\r") + self.exp_prompt() + + def done(self): + self.send("exit\r") + self.expect(pexpect.EOF) + self.wait() + + +def do_main(): + p = Session() + p.delaybeforesend = 0.01 + for line in [ + "cat < test.sh", + "set -x", + "stdecor -p '[google]' -- ping -nc8 www.google.com &", + "stdecor -p '[amazon]' -- ping -nc8 www.amazon.com &", + "wait", + "END", + ]: + p.send(line + "\r", slow=0.02) + p.expect("\n") + p.exp_prompt() + p.send_cmd("bash test.sh", slow=0.2) + p.send_cmd("rm -f test.sh") + p.done() + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.parse_args() + do_main() + + +if __name__ == "__main__": + main()