asciimation

An ASCII animation interpreter for the terminal.


asciimation

/

src

/

asciimation.cc

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
#include "asciimation.hh"
#include "term.hh"

#include "ansi_escape_codes.hh"
namespace AEC = OB::ANSI_Escape_Codes;

#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <regex>
#include <chrono>
#include <thread>

namespace OB
{

Asciimation::Asciimation()
{
}

Asciimation::~Asciimation()
{
}

Asciimation& Asciimation::set_debug(bool debug)
{
  debug_ = debug;
  return *this;
}

Asciimation& Asciimation::set_loop(size_t loop)
{
  loop_ = loop;
  return *this;
}

Asciimation& Asciimation::set_delay(size_t delay)
{
  delay_ = delay;
  return *this;
}

Asciimation& Asciimation::set_delim(std::string delim)
{
  delim_ = delim + "\n";
  return *this;
}

void Asciimation::run(std::string file_name)
{
  std::ifstream ifile {file_name};
  if (! ifile.is_open())
  {
    throw std::runtime_error("could not open input file");
  }

  // parse headers
  std::map<std::string, std::string> headers;
  std::string line;
  bool begin_found {false};
  while (std::getline(ifile, line))
  {
    if (line == begin_)
    {
      begin_found = true;
      break;
    }
    std::smatch m;
    if (std::regex_match(line, m, std::regex("^(.+?)\\s*:\\s*(.+)$")))
    {
      headers[std::string(m[1])] = std::string(m[2]);
    }
    else
    {
      throw std::runtime_error("invalid header syntax");
    }
  }

  if (! begin_found)
  {
    throw std::runtime_error("begin identifier not found");
  }

  check_window_size(headers);

  size_t offset = ifile.tellg();
  ifile.seekg(0, std::ios::end);
  size_t size = ifile.tellg();
  size -= offset;
  std::string content (size, ' ');
  ifile.seekg(offset);
  ifile.read(&content[0], size);
  ifile.close();

  auto frames = delimit(content, delim_);

  OB::Term term;

  main_loop(frames);
}

void Asciimation::main_loop(std::vector<std::string>& frames)
{
  std::cout << AEC::erase_screen << AEC::cursor_home;

  bool exit {false};
  size_t loop_count {loop_};
  size_t line_num {0};
  size_t frame_num {0};

  while (! exit && (loop_ == 0 || loop_count >= 1))
  {
    frame_num = 0;
    for (auto const& e : frames)
    {
      if (exit) break;

      line_num = 0;
      ++frame_num;
      std::stringstream out;
      if (debug_)
      {
        line_num += 2;
        if (loop_ == 0)
        {
          out << "L | ";
        }
        else
        {
          out << loop_count << " | ";
        }
        out
        << delay_ << " | "
        << frame_num << "/" << frames.size() << "\n\n";
      }

      auto line = delimit(e, "\n");
      if (line.size() > 1)
      {
        for (size_t i = 0; i < line.size() - 2; ++i)
        {
          out << line.at(i) << "\n";
          ++line_num;
        }
        out << line.at(line.size() - 2);
      }
      else if (line.size() == 1)
      {
        out << line.at(0);
        ++line_num;
      }
      std::cout << out.str() << std::flush;

      std::this_thread::sleep_for(std::chrono::milliseconds(delay_));

      // ----------------------------------------------------

      bool reset {false};
      int num_read {0};
      char c {0};

      while ((num_read = read(STDIN_FILENO, &c, 1)) == 1)
      {
        if (num_read == -1 && errno != EAGAIN)
        {
          throw std::runtime_error("read failed");
        }

        if (static_cast<int>(c) == (static_cast<int>('c') & 0x1f))
        {
          throw std::runtime_error("program interrupt");
        }
        else if (static_cast<int>(c) == (static_cast<int>('q') & 0x1f))
        {
          exit = true;
          break;
        }
        else if (static_cast<int>(c) == (static_cast<int>('d') & 0x1f))
        {
          reset = true;
          break;
        }
        else if (c == 'q')
        {
          exit = true;
          break;
        }
        else if (c == 'd')
        {
          debug_ = ! debug_;
        }
        else if (c == 'j')
        {
          if (delay_ > 5)
          {
            delay_ -= 5;
          }
        }
        else if (c == 'k')
        {
          if (delay_ < 1000)
          {
            delay_ += 5;
          }
        }
        else if (c == 'J')
        {
          if (delay_ > 50)
          {
            delay_ -= 50;
          }
        }
        else if (c == 'K')
        {
          if (delay_ < 1000)
          {
            delay_ += 50;
          }
        }
        else if (c == ' ')
        {
          size_t x = 0;
          size_t y = 0;
          Term::cursor_get(x, y);
          std::cout
          << AEC::cursor_set(0, 0)
          << AEC::wrap("||", {AEC::bold, AEC::reverse})
          << AEC::cursor_set(x, y);
          flush();
          while ((num_read = read(STDIN_FILENO, &c, 1)) != 1)
          {
            std::this_thread::sleep_for(std::chrono::milliseconds(50));
          }
        }
        else if (c == 'h' || c == '?')
        {
          clear_screen(line_num);
          std::cout
          << "Help:\n"
          << "h -> show the help text\n"
          << "q -> quit the asciimation\n"
          << "d -> toggle debug output\n"
          << "j -> decrease speed by 5\n"
          << "J -> decrease speed by 50\n"
          << "k -> increase speed by 5\n"
          << "K -> increase speed by 50\n"
          << "space -> pause the animation\n"
          << "Press any key to continue";
          flush();
          line_num += 9;
          while ((num_read = read(STDIN_FILENO, &c, 1)) != 1)
          {
            std::this_thread::sleep_for(std::chrono::milliseconds(50));
          }
        }
      }

      clear_screen(line_num);
      flush();

      if (reset) break;
    }
    --loop_count;
  }
}

void Asciimation::flush() const
{
  std::cout << std::flush;
}

void Asciimation::clear_screen(size_t num) const
{
  std::stringstream clr;
  clr << AEC::erase_line << AEC::cr;
  while (num > 0)
  {
    clr << AEC::cursor_up << AEC::erase_line;
    --num;
  }
  std::cout << clr.str();
}

size_t Asciimation::str_count(std::string const& str, std::string const& s) const
{
  size_t count {0};
  size_t pos {0};

  for (;;)
  {
    pos = str.find(s, pos);
    if (pos == std::string::npos) break;
    ++count;
    ++pos;
  }
  return count;
}

void Asciimation::check_window_size(std::map<std::string, std::string>& headers) const
{
  size_t twidth {0};
  size_t theight {0};
  OB::Term::size(twidth, theight);

  if (headers.find("x") != headers.end())
  {
    if (std::stoul(headers["x"]) > twidth)
    {
      throw std::runtime_error("screen width is too small, requires a minimum width of " + headers["x"]);
    }
  }

  if (headers.find("y") != headers.end())
  {
    if (std::stoul(headers["y"]) > theight)
    {
      throw std::runtime_error("screen height is too small, requires a minimum height of " + headers["y"]);
    }
  }
}

std::vector<std::string> Asciimation::delimit(std::string const& str, std::string const delim) const
{
  std::vector<std::string> vtok;
  size_t start {0};
  size_t end = str.find(delim);
  while (end != std::string::npos)
  {
    vtok.emplace_back(str.substr(start, end - start));
    start = end + delim.length();
    end = str.find(delim, start);
  }
  vtok.emplace_back(str.substr(start, end));
  return vtok;
}

} // namespace OB
Back to Top