octavia

octobanana's customizable text-based audio visualization interactive application.


octavia

/

src

/

app

/

window.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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
/*
                                    88888888
                                  888888888888
                                 88888888888888
                                8888888888888888
                               888888888888888888
                              888888  8888  888888
                              88888    88    88888
                              888888  8888  888888
                              88888888888888888888
                              88888888888888888888
                             8888888888888888888888
                          8888888888888888888888888888
                        88888888888888888888888888888888
                              88888888888888888888
                            888888888888888888888888
                           888888  8888888888  888888
                           888     8888  8888     888
                                   888    888

                                   OCTOBANANA

Licensed under the MIT License

Copyright (c) 2020 Brett Robinson <https://octobanana.com/>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "app/window.hh"

#include "ob/text.hh"
#include "ob/term.hh"
#include "ob/prism.hh"
#include "ob/timer.hh"

#include <cmath>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdlib>

#include <tuple>
#include <deque>
#include <regex>
#include <chrono>
#include <limits>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include <complex>
#include <utility>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <functional>
#include <unordered_map>

Buffer::Buffer(Size const size, Cell const& cell) {
  this->size(size, cell);
}

void Buffer::operator()(Pos const pos, Cell const& cell) {
  // return on out of bounds
  if (pos.x > _size.x - 1 || pos.y > _size.y - 1) {return;}
  cursor(std::move(pos));
  operator()(cell);
}

void Buffer::operator()(Cell const& cell) {
  auto& val = _value.at(_size.y - _pos.y - 1).at(_pos.x);
  if (cell.style.type != Style::Type::Clear && cell.zidx >= val.zidx) {
    if (cell.style.type == Style::Type::Default) {
      val = cell;
    }
    else {
      val = Cell{cell.zidx, Style{cell.style.type, cell.style.attr, val.style.fg + cell.style.fg, val.style.bg + cell.style.bg}, cell.text != " " ? cell.text : (val.text.size() && cell.style.bg.a() != 255 ? val.text : " ")};
    }
  }
}

void Buffer::put(Pos const pos, Cell const& cell) {
  // return on out of bounds
  if (pos.x > _size.x - 1 || pos.y > _size.y - 1) {return;}
  cursor(std::move(pos));
  put(cell);
}

void Buffer::put(Cell const& cell) {
  OB::Text::View view {cell.text};
  for (auto const& e : view) {
    if (e.cols == 2) {
      if (_pos.x + 1 == _size.x - 1) {
        _pos.x = 0;
        if (++_pos.y >= _size.y) {
          _pos.y = 0;
        }
      }
      auto& val = _value.at(_size.y - _pos.y - 1).at(_pos.x);
      if (cell.style.type != Style::Type::Clear && cell.zidx >= val.zidx) {
        if (val.zidx == 0 || val.style.type == Style::Type::Clear) {
          val = Cell{cell.zidx, cell.style, std::string{e.str}};
        }
        else {
          val = Cell{cell.zidx, Style{cell.style.type, cell.style.attr, val.style.fg + cell.style.fg, val.style.bg + cell.style.bg}, cell.text != " " ? std::string{e.str} : (val.text.size() && cell.style.bg.a() != 255 ? val.text : " ")};
        }
      }
      if (_pos.x += 2 >= _size.x) {
        _pos.x = 0;
        if (++_pos.y >= _size.y) {
          _pos.y = 0;
        }
      }
    }
    else {
      auto& val = _value.at(_size.y - _pos.y - 1).at(_pos.x);
      if (cell.style.type != Style::Type::Clear && cell.zidx >= val.zidx) {
        if (cell.style.type == Style::Type::Default) {
          val = Cell{cell.zidx, cell.style, std::string{e.str}};
        }
        else {
          val = Cell{cell.zidx, Style{cell.style.type, cell.style.attr, val.style.fg + cell.style.fg, val.style.bg + cell.style.bg}, cell.text != " " ? std::string{e.str} : (val.text.size() && cell.style.bg.a() != 255 ? val.text : " ")};
        }
      }
      if (++_pos.x >= _size.x) {
        _pos.x = 0;
        if (++_pos.y >= _size.y) {
          _pos.y = 0;
        }
      }
    }
  }
}

Cell& Buffer::at(Pos const pos) {
  return _value.at(pos.y).at(pos.x);
}

Cell const& Buffer::at(Pos const pos) const {
  return _value.at(pos.y).at(pos.x);
}

std::vector<Cell>& Buffer::row(std::size_t const y) {
  return _value.at(_size.y - y - 1);
}

std::vector<Cell> const& Buffer::row(std::size_t const y) const {
  return _value.at(_size.y - y - 1);
}

Cell& Buffer::col(Pos const pos) {
  return _value.at(_size.y - pos.y - 1).at(pos.x);
}

Cell const& Buffer::col(Pos const pos) const {
  return _value.at(_size.y - pos.y - 1).at(pos.x);
}

Pos Buffer::cursor() const {
  return _pos;
}

void Buffer::cursor(Pos const pos) {
  _pos = pos;
}

Size Buffer::size() const {
  return _size;
}

void Buffer::size(Size const size, Cell const& cell) {
  _size = size;
  _pos = Pos();
  _value.clear();
  for (std::size_t h = 0; h < _size.y; ++h) {
    _value.emplace_back();
    for (std::size_t w = 0; w < _size.x; ++w) {
      _value.back().emplace_back(cell);
    }
  }
}

bool Buffer::empty() const {
  return _value.empty();
}

void Buffer::clear() {
  _pos = Pos();
  _size = Size();
  _value.clear();
}

void Window::winch() {
  clear = true;
  style = style_base;
  buf.size(size, Cell{0, style, " "});
  buf_prev = buf;
}

void Window::refresh() {
  clear = true;
  style = style_base;
  buf_prev = buf;
}

void Window::render() {
  if (clear) {
    clear = false;
    line += aec::cursor_set(1, buf.size().y);
    line += aec::clear;
    if (style.type != Style::Type::Clear) {
      if (style.attr != Style::Null) {
        if (style.attr & Style::Bold) {line += aec::bold;}
        if (style.attr & Style::Reverse) {line += aec::reverse;}
        if (style.attr & Style::Underline) {line += aec::underline;}
      }
      if (style.type == Style::Bit_24) {
        term_fg(line, style.fg);
        term_bg(line, style.bg);
      }
    }
    line += aec::screen_clear;
    write(line);
  }

  for (std::size_t y = 0; y < buf.size().y; ++y) {
    for (std::size_t x = 0; x < buf.size().x; ++x) {
      auto const& cell = buf.at(Pos(x, y));
      auto const& prev = buf_prev.at(Pos(x, y));

      if ((cell.text != prev.text) ||
          (cell.style.attr != prev.style.attr) ||
          (cell.style.type != prev.style.type) ||
          (cell.style.fg != prev.style.fg) ||
          (cell.style.bg != prev.style.bg)) {

        bool diff_attr {style.attr != cell.style.attr};
        bool diff_type {style.type != cell.style.type};
        bool diff_fg {style.fg != cell.style.fg};
        bool diff_bg {style.bg != cell.style.bg};

        line += aec::cursor_set(x + 1, y + 1);

        if (diff_type) {
          style.type = cell.style.type;
          if (style.type == Style::Type::Default) {
            line += aec::clear;
          }
        }

        if (diff_attr) {
          diff_fg = true;
          diff_bg = true;
          style.attr = cell.style.attr;
          line += aec::clear;
          if (style.attr != Style::Null) {
            if (style.attr & Style::Bold) {line += aec::bold;}
            if (style.attr & Style::Reverse) {line += aec::reverse;}
            if (style.attr & Style::Underline) {line += aec::underline;}
          }
        }

        if (cell.style.type == Style::Type::Clear) {
          style = Style();
          line += aec::clear;
        }
        else if (cell.style.type == Style::Bit_24) {
          if (diff_fg) {
            style.fg = cell.style.fg;
            term_fg(line, style.fg);
          }

          if (diff_bg) {
            style.bg = cell.style.bg;
            term_bg(line, style.bg);
          }
        }

        line += cell.text;
      }
    }
    bsize += line.size();
    write(line);
  }

  bsize = 0;
  buf_prev = buf;
  buf.size(buf.size(), Cell{0, style_base, " "});
  ++frames;
}

void Window::write(std::string& str) {
  if (str.empty()) {return;}
  int num {0};
  char const* ptr {str.data()};
  std::size_t size {str.size()};
  while (size > 0 && static_cast<std::size_t>(num = ::write(STDOUT_FILENO, ptr, size)) != size) {
    if (num < 0) {
      if (errno == EINTR || errno == EAGAIN) {continue;}
      throw std::runtime_error("write failed");
    }
    size -= static_cast<std::size_t>(num);
    ptr += static_cast<std::size_t>(num);
  }
  str.clear();
}

void Window::render_file(std::ofstream& file) {
  for (std::size_t y = 0; y < buf.size().y; ++y) {
    for (std::size_t x = 0; x < buf.size().x; ++x) {
      auto const& cell = buf.at(Pos(x, y));
      line += cell.text;
    }
    bsize += line.size();
    line += "\n";
    write(line);
    // write_file(file, line);
  }

  bsize = 0;
  buf_prev = buf;
  buf.size(buf.size(), Cell{0, style_base, " "});
  ++frames;
}

void Window::write_file(std::ofstream& file, std::string& str) {
  if (str.empty()) {return;}
  file.seekp(0);
  file << str << std::flush;
  if (!file) {throw std::runtime_error("write failed");}
  str.clear();
}

void Window::term_fg(std::string& str, OB::Prism::RGBA rgba) {
  str += "\x1b[38;2;";
  str += std::to_string(rgba.r());
  str += ";";
  str += std::to_string(rgba.g());
  str += ";";
  str += std::to_string(rgba.b());
  str += "m";
}

void Window::term_bg(std::string& str, OB::Prism::RGBA rgba) {
  str += "\x1b[48;2;";
  str += std::to_string(rgba.r());
  str += ";";
  str += std::to_string(rgba.g());
  str += ";";
  str += std::to_string(rgba.b());
  str += "m";
}
Back to Top