fltrdr

A TUI text reader for the terminal.


fltrdr

/

src

/

fltrdr

/

fltrdr.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
#ifndef FLTRDR_HH
#define FLTRDR_HH

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

#include <cstddef>

#include <string>
#include <vector>
#include <sstream>
#include <iostream>

class Fltrdr
{
public:

  // current rendered line
  struct Line
  {
    std::string prev {};
    std::string curr {};
    std::string next {};
  };

  Fltrdr() = default;

  void init();
  bool parse(std::istream& input);
  std::string content_id();

  Fltrdr& screen_size(std::size_t const width, std::size_t const height);

  bool eof();

  void begin();
  void end();

  OB::Text::View buf_prev(std::size_t offset = 0);
  OB::Text::View buf_next(std::size_t offset = 0);

  void set_focus_point();

  void set_line(std::size_t offset = 0);
  Line get_line();

  int get_wait();

  void set_index(std::size_t i);
  std::size_t get_index();

  int get_wpm();
  void set_wpm(int const i);
  void inc_wpm();
  void dec_wpm();

  void set_wpm_avg(int const i);
  int get_wpm_avg();

  void calc_wpm_avg();
  std::string get_stats();

  void set_show_line(bool const val);
  bool get_show_line();

  void set_show_prev(int const val);
  int get_show_prev();

  void set_show_next(int const val);
  int get_show_next();

  std::size_t progress();

  OB::Text::View word();
  void current_word();
  bool prev_word();
  bool next_word();

  void prev_sentence();
  void next_sentence();

  void prev_chapter();
  void next_chapter();

  bool search_next();
  bool search_prev();
  bool search(std::string const& rx, bool forward);

  void reset_timer();
  void reset_wpm_avg();

  OB::Timer timer;

private:

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

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

    // current word focus point
    double const focus {0.25};
    std::size_t focus_point {0};

    // current word display width in columns before focus point
    std::size_t prefix_width {0};

    // text buffer
    OB::Text::String text;

    // sha256 hash of the text buffer
    std::string content_id;

    // current rendered line
    Line line;

    // text position of leading space to current word
    std::size_t pos {0};

    // word index
    std::size_t index {1};

    // min word index
    std::size_t index_min {1};

    // max word index
    std::size_t index_max {1};

    // current word
    OB::Text::View word;

    OB::Text::View prev;
    OB::Text::View next;

    // words per minute
    int const wpm_diff {10};
    int const wpm_min {60};
    int const wpm_max {1200};
    int wpm {250};
    int wpm_avg {0};
    int wpm_count {0};
    int wpm_total {0};

    // wait time in milliseconds
    int ms {0};

    // toggle prev and next buffer surrounding current word in line
    bool show_line {false};
    int show_prev {1};
    int show_next {1};
    int show_min {0};
    int show_max {60};

    struct Search
    {
      OB::Text::Regex it;
      std::string rx;
      bool forward {true};
    } search;

    // sentence end characters
    OB::Text::String sentence_end {".!?"};
  } _ctx;

  bool search_forward();
  bool search_backward();
};

#endif // FLTRDR_HH
Back to Top