fltrdr

A TUI text reader for the terminal.


fltrdr

/

src

/

fltrdr

/

tui.hh

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
#ifndef TUI_HH
#define TUI_HH

#include "fltrdr/readline.hh"
#include "fltrdr/fltrdr.hh"

#include "ob/string.hh"
#include "ob/term.hh"
namespace aec = OB::Term::ANSI_Escape_Codes;

#include <cstdio>
#include <cstddef>
#include <cstdlib>

#include <array>
#include <string>
#include <vector>
#include <sstream>
#include <utility>
#include <optional>

class Tui
{
public:

  Tui();

  Tui& init(std::string const& file_path = {});
  void config(std::string const& custom_path = {});
  void run();

private:

  enum Key
  {
    up = 1000,
    down,
    left,
    right,
    del,
  };
  int get_key() const;
  int ctrl_key(int const c) const;
  void get_input(int& wait);
  bool press_to_continue(std::string const& str = "ANY KEY", int val = 0);

  std::optional<std::pair<bool, std::string>> command(std::string const& input);
  void command_prompt();

  void event_loop();
  int screen_size();

  void clear();
  void refresh();

  void draw();
  void draw_content();
  void draw_border_top();
  void draw_border_bottom();
  void draw_progress_bar();
  void draw_status();
  void draw_prompt_message();
  void draw_keybuf();

  void play();
  void pause();

  void set_wait();

  void search_forward();
  void search_backward();

  OB::Term::Mode _term_mode;
  bool const _colorterm;
  Readline _readline;
  Readline _readline_search;
  Fltrdr _fltrdr;

  struct Ctx
  {
    struct File
    {
      // file to read from
      std::string path;

      // file name
      std::string name;
    } file;

    // current terminal size
    std::size_t width {0};
    std::size_t height {0};

    // minimum terminal size
    std::size_t width_min {20};
    std::size_t height_min {6};

    // output buffer
    std::ostringstream buf;

    // control when to exit the event loop
    bool is_running {true};

    // interval between reading a keypress
    int const input_interval {50};

    // horizontal offset from center
    std::size_t offset {0};
    int offset_value {2};

    struct State
    {
      bool play {false};

      int count_total {3};
      int count_down {0};
      bool counting_down {false};

      int wait {250};
      int refresh_rate {250};
    } state;

    // status
    struct Status
    {
      std::string str;
      std::string mode {"FLTRDR"};
    } status;

    // input char buffer
    std::array<char, 2> chars {'\0', '\0'};

    // command prompt
    struct Prompt
    {
      std::string str;
      int count {0};
      int timeout {12};
    } prompt;

    struct Show
    {
      bool border_top {true};
      bool border_bottom {true};
      bool progress {true};
      bool status {true};
    } show;

    struct Style
    {
      std::string primary {aec::fg_black};
      std::string secondary {aec::fg_cyan};
      std::string background {aec::bg_cyan};

      std::string border {aec::fg_cyan};

      std::string countdown {aec::bg_black};

      std::string progress_bar {aec::fg_black};
      std::string progress_fill {aec::fg_cyan};

      std::string prompt {aec::fg_cyan};
      std::string prompt_status {};
      std::string success {aec::fg_green};
      std::string error {aec::fg_red};

      std::string word_primary {aec::fg_white};
      std::string word_secondary {aec::fg_white};
      std::string word_highlight {aec::fg_cyan};
      std::string word_punct {aec::fg_white};
      std::string word_quote {aec::fg_white};
    } style;

    struct Sym
    {
      std::string border_top {"-"};
      std::string border_top_mark {"-"};

      std::string border_bottom {"-"};
      std::string border_bottom_mark {"^"};

      std::string progress {"_"};
    } sym;
  } _ctx;
};

#endif // TUI_HH
Back to Top