HackingTheTerminal

Escape Codes to Animate your Terminal

{
  "Speaker": "Gaetano Checinski",
  "Company": "LoopPerfect"
  "Role": "Founder & Software Architect"
}

Reactive Terminal

How ?

Escape Sequences + FP = Magic

\e[Escape Sequences

  • Colors & Style
  • Moving The Cursor
  • Deletions

\e[Escape Sequences

"\e[CODE" general shape

"\e[0m" default style

Terminal is Stateful

Colors

\e[31m red text \e[43m and yellow background

Colors & Styles Combined

\e[5;43;31m blinking text

Moving the Cursor

\e[nA move n up

\e[nB move n down

\e[nC move n right

\e[nD move n left

Example

123456\e[4DXX\e[1A\e[2DYY\e[1B

Terminal is Stateful!

You should describe what you want

not how to get there

Solution

Example using RxTerm

  VirtualTerminal vt;

  auto superProgressBar = [](auto x, auto y, auto z) -> FlowLayout<> {
    return {
      Text("3 segment progressbar: "),
      {
        MaxWidth(20, Progress(x)),
        MaxWidth(20, Progress(y)),
        MaxWidth(20, Progress(z))
      }
    };
  };

  for (int i = 0; i < 101; ++i) {
    vt = renderToTerm(vt, superProgressBar(0.01 * i, 0.02 * i, 0.03 * i));
    std::this_thread::sleep_for(200ms);
  }

If Love Observables

using rxo = rxcpp::observable<>;

rxo::zip_with_latest(
  superProgressBar,
  rxo::interval(100ms),
  rxo::interval(200ms),
  rxo::interval(300ms))
.scan(VirtualTerminal{}, renderToTerm)
.subscribe();

Support

Linux & Mac

VT100 (since 1978)

Windows 10 (since June 2018)

Since you are here

slow builds? - talk to us

https://buildinfer.loopperfect.com