autotyper/typer.h

77 lines
1.7 KiB
C++

#ifndef TYPER_H
#define TYPER_H
#include <string>
#include <vector>
#include <array>
#include <memory>
#include <keyhandler.h>
#define SIMILARITY_LINE_COUNT 8
#define SIMILARITY_COPY_THRESHOLD 0.75f
#define BLOCK_UNSIMILAR_LIMIT 2
struct Line
{
int prio;
size_t index;
std::string text;
std::array<float, SIMILARITY_LINE_COUNT> similarity;
Line() { }
Line(int prio, size_t index, std::string text) : prio(prio), index(index), text(text) { }
};
class Typer
{
public:
Typer(std::unique_ptr<KeyHandler> keyHandler) : m_keyHandler(std::move(keyHandler)) { }
virtual ~Typer() { }
void ProcessFile(std::string fname);
void TogglePaused();
protected:
virtual void OnStart() { }
virtual void KeyUp();
virtual void KeyDown();
virtual void KeyLeft();
virtual void KeyRight();
virtual void KeyHome();
virtual void KeyEnd();
virtual void KeyNewline();
virtual void KeyString(std::string str);
private:
int GetLinePriority(std::string& line);
void ReadFile(std::string fname);
void ComputeDiffs();
// These methods type and also update the "screen"
void Newline();
void TypeString(std::string str);
void NavigateTo(size_t row, size_t col);
void NavigateToRow(size_t row);
void NavigateToCol(size_t col);
bool IsKeyShifted(char c);
// constants
static const size_t COL_END = static_cast<size_t>(-2);
static const size_t COL_HOME = static_cast<size_t>(-1);
protected:
// key handler
std::unique_ptr<KeyHandler> m_keyHandler;
private:
// lines to write
std::vector<Line> m_lines;
// current state of the screen
std::vector<std::pair<size_t, std::string>> m_scrOutput;
size_t m_scrCol, m_scrRow;
};
#endif // TYPER_H