Skip to content

Instantly share code, notes, and snippets.

@wasi-master
Created April 15, 2022 11:42
Show Gist options
  • Save wasi-master/e2cf93783536b1263c5480dfb479e6b4 to your computer and use it in GitHub Desktop.
Save wasi-master/e2cf93783536b1263c5480dfb479e6b4 to your computer and use it in GitHub Desktop.
Asciinema for rich
<html>
<head>
<link rel="stylesheet" type="text/css" href="./asciinema-player.css" />
</head>
<body>
<div id="demo"></div>
<script src="./asciinema-player.js"></script>
<script>
AsciinemaPlayer.create('./test.cast', document.getElementById('demo'));
</script>
</body>
</html>
import os, time, json
import rich
from rich.console import Console
class TestFile:
def __init__(self, width, height):
self.content = ""
self.start_time = time.time()
self.last_time = time.time()
self.asciicast = {
"version": 1,
"width": width,
"height": height,
"duration": 0, # Will be updated later
"command": "/bin/zsh",
"title": "",
"env": {
"TERM": os.environ.get("TERM"),
"SHELL": os.environ.get("SHELL")
},
"stdout": []
}
def read():
return self.content
def write(self, content):
self.content += content
current_time = time.time()
self.asciicast["stdout"].append(
[current_time-self.last_time, content]
)
self.last_time = current_time
def flush(self):
pass
console = Console(force_terminal=True)
file=TestFile(width=console.width, height=console.height)
console.file=file
if __name__ == "__main__":
with console.status("Loading"):
time.sleep(1)
console.print("[bold underline blink]hi[/]")
time.sleep(1)
console.print("[italic]hello[/]")
time.sleep(1)
file.asciicast['duration'] = time.time() - file.start_time
with open('test.cast', 'w') as f:
json.dump(file.asciicast, f, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment