aboutsummaryrefslogtreecommitdiff
path: root/Main.cpp
blob: 53e6cf0621f24be855eadc2af0b2a8d984e04f2a (plain)
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
// ====================
// The purpose of this project is to
// make a reader so that I can read
// novels even faster than my already
// amazing speed.(200k words/day) to (300k words/day)
//
// Rsearch objective:
// Terminal: RSVP (Rapid Serial Visual Presentation)
//
// Project Name: fastAsFKReader(FAFK)
// Author : Aakarsh Kashyap
// [ 2026-06-17 ]
// ====================

// Steps
//
// 1. Get to the raw mode
// 2. Get cursor to center of the terminal
// 3. Parse the whole file into the memory and save it into a vector
// of strings.
// 4. Figure out the central letter of the string
// 5. Print that word with the central letter coloured
// 6. We will take a timer args which will make word rendering fast or slow but
// it's fine for now
// 7. The size of the word is smt dependent on user terminal zoomin we will not
// try to render bigger font or anything
// 8. End of the program.

/* Include */
#include <asm-generic/ioctls.h>
#include <cerrno>
#include <cstdint>
#include <termios.h>
#include <unistd.h>
#include <cstdlib>
#include <cstdio>
#include <sys/ioctl.h>
#include <cstring>
#include <string>
#include <vector>

/* Define */
#define CTRL_KEY(k) ((k) & 0x1f)


/** data **/

using editorConfig_t = struct editorConfig
{
    int screenrows;
    int screencols;
    struct termios org_termios{};

};

editorConfig_t e;

/** terminal **/
void Die(const char *s)
{
    write(STDOUT_FILENO, "\x1b[2J", 4);
    write(STDOUT_FILENO, "\x1b[H", 3);

    perror(s);
    exit(1);
}

void DisableRawMode()
{
    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &e.org_termios) == -1) { Die("tcsetattr") ;
}
}

void EnableRawMode()
{
    if(tcgetattr(STDIN_FILENO, &e.org_termios) == -1) { Die("tcgetattr");
}
    // At exit(program exits) runs the function
    atexit(DisableRawMode);

    struct termios raw = e.org_termios;


    // Iflags are for input flags
    // XON is used to control and disable C-s and C-q they are used for software flow control
    // CR for carriage return NL for new line
    raw.c_iflag &= ~(IXON | ICRNL | BRKINT | INPCK | ISTRIP);

    // turning off the post processing of output
    raw.c_oflag &= ~(OPOST);

    raw.c_cflag |= (CS8);
    // lflags is for mislaneous things
    // ISIG flag disable SIGINT C-c and SIGSTP C-z
    // ECHO flag disable return output to screen as we type sudo uses this
    // ICANON is used to turn off the canonical/cooked mode so we can process per byte instead of per line
    // IEXTEN is used to diable C-v
    raw.c_lflag &= ~(ECHO | ICANON | IEXTEN |ISIG);


    // c_cc is a control character array;
    // vmin sets the number of bytes needed before read can return
    raw.c_cc[VMIN] = 0;
    // Sets max time before read returns
    raw.c_cc[VTIME] = 1;

    // TCSAFLUSH is used to flus all the things after thsi func call
    if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) { Die("tcsetattr") ;
}
}

char EditorReadKey()
{
    int nread;
    char c;
    while ((nread = read(STDIN_FILENO, &c, 1)) != 1)
    {
        if(nread == -1 && errno != EAGAIN) { Die("read");
}
    }
    return c;
}

int GetCursorPosition(int *rows, int *cols)
{
    char buf[32];
    unsigned int i = 0;
    // n cmd gives the device info 6 as args give us the cursor pos
  if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4) {
      return -1;
}
  while (i < sizeof(buf) - 1)
  {
    if (read(STDIN_FILENO, &buf[i], 1) != 1) {
        break;
}
    if (buf[i] == 'R') {
        break;
}
    i++;
  }
  buf[i] = '\0';

  if (buf[0] != '\x1b' || buf[1] != '[') {
      return -1;
}
  if (sscanf(&buf[2], "%d;%d", rows, cols) != 2) {
      return -1;
}

  return 0;
}


int GetWindowSize(int *rows, int *cols)
{
    struct winsize ws;

    // TIOCGWINSZ stads for terminal ioctl get window size
    // returns -1 on error can even return row and cols as zero so we check both
    if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0)
    {
        if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) {
            return -1;
}
        return GetCursorPosition(rows, cols);
    }
    
            *cols = ws.ws_col;
        *rows = ws.ws_row;
        return 0;
   
}

/* append buffer */

class AppendBuf
{
private:
    char *m_b{nullptr};
    uint32_t m_len{0};

public:
    AppendBuf()  {}

    ~AppendBuf() = default;

    void Append(const char *s, int len)
    {
        char *num = (char*)realloc(m_b, m_len + len);

        if (num == nullptr) {
            return;
}
        memcpy(&num[m_len], s, len);
        m_b = num;
        m_len += len;
    }

    static AppendBuf &Instance()
    {
        static AppendBuf obj;
        return obj;
    }
    void Remove()
    {
        free(m_b);
        m_b = nullptr;
        m_len = 0;
    }
    void WriteOut()
    {
        write(STDOUT_FILENO, m_b, m_len);
    }
};

/* output */


void EditorRefreshScreen()
{
  // escape sequence starts with escape character \x1b is 27byte <esc> followed by [
  // is a escape sequence
  // Then we specify the command and it's args. args are given before the
  // command
  // J corresponds to clearning the screen. 2 args means all the screena 1 for
  // before the cursor and 0(default)
  // for after the cursor to EOS
    AppendBuf::Instance().Append("\x1b[2J", 4);
    // H for cursor pos control takes two args rowXcol. 12;4H args are seperated by ';'
    char tuff[32]; memset(tuff, 0, sizeof(tuff));
    snprintf(tuff, sizeof(tuff), "\x1b[%d;%dH", e.screenrows / 2, e.screencols / 2);
    AppendBuf::Instance().Append(tuff, strlen(tuff));
    AppendBuf::Instance().WriteOut();
    AppendBuf::Instance().Remove();
}

/* input */

void EditorProcessKeyProcess()
{
    char c = EditorReadKey();

    switch (c)
    {
    case CTRL_KEY('q'):
        write(STDOUT_FILENO, "\x1b[2J", 4);
        write(STDOUT_FILENO, "\x1b[H", 3);
        exit(0);
        break;
    }
}

/* Utils */
size_t Split(const std::string &txt, std::vector<std::string> &strs, char ch) {
  size_t pos = txt.find( ch );
  size_t initaiPos = 0;
  // strs.clear();

  while( pos != std::string::npos) {
    strs.push_back(txt.substr(initaiPos, pos - initaiPos));
    initaiPos = pos+1;

    pos = txt.find(ch, initaiPos);
  }
  strs.push_back( txt.substr(initaiPos));

  return strs.size();
}

/** Init  **/

void InitEditor()
{
  if (GetWindowSize(&e.screenrows, &e.screencols) == -1) {
      Die("getWindowSize");
}

}

int main(int argc, char *argv[])
{
    EnableRawMode();
    InitEditor();
    if (argc < 2) {
        Die("no file provided");
}

    int speed{250000};
    if (argc == 3) {
        speed = std::stoi(argv[2]);
}
    FILE *f = fopen(argv[1], "r");
    if (f == nullptr) { Die("fopen");
}

    fseek(f, 0, SEEK_END);
    long size = ftell(f);
    rewind(f);

    std::string content(size, '\0');
    fread(content.data(), 1, size, f);
    fclose(f);
    std::vector<std::string> lund;
    Split(content, lund, ' ');

    while (1)
    {
        for (std::string &k : lund)
        {
            EditorRefreshScreen();
            int mid = k.size() / 2;

            write(STDOUT_FILENO, k.substr(0, mid).c_str(), mid);

            write(STDOUT_FILENO, "\x1b[31m", 5);
            write(STDOUT_FILENO, k.substr(mid, 1).c_str(), 1);
            write(STDOUT_FILENO, "\x1b[m", 3);

            int remaining = k.size() - mid - 1;
            write(STDOUT_FILENO, k.substr(mid + 1, remaining).c_str(), remaining);

            usleep(speed);
        }
        EditorProcessKeyProcess();
    }
    return 0;
}

/* CONCLUSION */
// Experiment have failed. I do not see any particular increase int my
// reading speed, infact it has become difficult to
// keep previous context in mind
// There must be something else going on in that reel
// I will try to ask about it from sei or shashy maybe there is a
// better way
//
// [ 2026-06-17 ]