Day 10 — present can now play code!

Today I worked on the "pre-recorded playable code block" idea from yesterday. During a talk, it's hard for me to remember and type commands on the terminal while also explaining what I'm doing. So I usually record a gif earlier and play that instead. And since gifs don't render nicely on the terminal, I thought that a "pre-recorded playable code block" might be a useful feature to add to present.

Building on the idea of printing the input one character at a time, and then printing the output all at once, I wrote some Python code to see what that would look like.


  import sys
  import time

  prompt = ">>>"
  code = [
      {"in": "import os", "out": ""},
      {"in": "os.getcwd()", "out": "/home/vinayak/dev"}
  ]

  sys.stdout.write(f"{prompt} ")
  for c in code:
      for i in range(1, len(c["in"]) + 1):
          time.sleep(0.15)
          sys.stdout.write(f"\r{prompt} " + c["in"][:i])
          sys.stdout.flush()
      sys.stdout.write("\n" + c["out"])

The output looked nice but since the code above can't work with asciimatics, I started looking into extending its Effect class. Nick also pointed me to codio which is a media format to record and replay code. Pretty cool!

After spending some time looking at the asciimatics docs, code and examples; I found DynamicRenderer (which renders text on demand) and it looked like exactly what I needed. And after spending some more time trying to understand how a Screen/Scene calls DynamicRenderer I was able to extend it. Somewhere in the middle, I also got this fun output when I wasn't keeping the state of what was being printed properly.

Is this async?

But finally it looks like this on a slide! You can now play code on present.

I wanted to support syntax like below but ended up going with a yaml that can be added to a slide with ![codio](codio.yml).


  ```codio
  prompt=>>> input='import os'
  prompt=>>> input='os.getcwd()' output='/home/vinayak/dev'
  ```

Now I just need to add progress bars, and support pressing "r" to reset the state of a slide (or maybe just loop the codio).


Two weeks are already over! I think I should revisit my learning plan.