Skip to content

Commit

Permalink
Add ascii demo infra; add demo-ping.gif to README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Jan 1, 2024
1 parent 8797d41 commit 57f29e6
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/result
/demos/*.cast
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions demos/build-all.sh
Original file line number Diff line number Diff line change
@@ -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
Binary file added demos/demo-ping.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions demos/demo-ping.py
Original file line number Diff line number Diff line change
@@ -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 <<END > 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()

0 comments on commit 57f29e6

Please sign in to comment.