teditor  1.8.0@@fee5e94
Terminal based editor written in C++
terminal.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include "utils.h"
5 #include <string.h>
6 #include <termios.h>
7 #include "pos2d.h"
8 #include "keys.h"
9 #include "logger.h"
10 
11 namespace teditor {
12 
13 enum Func {
29 };
30 
31 enum EventType {
32  Event_Key = 1,
36 };
37 
38 const char* func2terminfo(Func f);
39 
40 
43 class Terminal {
44 public:
51 
52  int getWinchFd(int idx) const { return winchFds[idx]; }
53  int width() const { return tsize.x; }
54  int height() const { return tsize.y; }
55 
65  void puts(const char* data, size_t len);
66  void puts(const char* data) { puts(data, strlen(data)); }
67  void puts(const std::string& data) { puts(data.c_str(), data.length()); }
68  void puts(Func f) { puts(func(f)); }
70  void flush();
72  void updateTermSize();
80  void reset();
82  int waitAndFill(struct timeval* timeout);
84  const std::string& getOldSeq() const { return oldSeq; }
86  bool bufferResize() const { return buffResize; }
88  void disableResize() { buffResize = false; }
92  static Terminal& getInstance();
93 
95  static const int UndefinedSequence;
96 
97 private:
99  Strings funcs;
101  std::string termName;
103  std::string outbuff;
105  std::string ttyFile;
107  int inout;
109  struct termios tios, origTios;
111  Pos2di tsize;
113  std::string seq, oldSeq;
115  bool buffResize;
117  int winchFds[2];
118 
120  static Terminal* inst;
121  static const std::string EnterMouseSeq;
122  static const std::string ExitMouseSeq;
123  static const int BuffSize;
124 
125  enum ColorSupport {
126  CS_None = 0,
127  CS_256,
128  CS_True
129  };
130 
131  Terminal(const std::string& tty);
132  ~Terminal();
133  void setSignalHandler();
134  void setupTios();
135  ColorSupport colorSupported() const;
136  const char* func(int id) const { return funcs[id].c_str(); }
137  int readAndExtract();
138  int decodeChar(key_t ch);
139  int decodeEscSeq();
140  int readKey();
141 
142  template <typename A, typename B> friend class SingletonHandler;
143 };
144 
145 }; // end namespace teditor
teditor::Terminal::getWinchFd
int getWinchFd(int idx) const
Definition: terminal.h:52
teditor::Terminal::puts
void puts(Func f)
Definition: terminal.h:68
teditor::exitGracefully
void exitGracefully(int signum)
Definition: terminal.cpp:40
teditor::Event_Mouse
@ Event_Mouse
Definition: terminal.h:34
teditor::MetaKey
Definition: keys.h:186
teditor::EventType
EventType
Definition: terminal.h:31
teditor::Key_Tilde
static const key_t Key_Tilde
Definition: keys.h:180
teditor::Terminal::width
int width() const
Definition: terminal.h:53
logger.h
keys.h
teditor::Terminal::bufferResize
bool bufferResize() const
Definition: terminal.h:86
teditor::MetaKey::updateMeta
void updateMeta(key_t m)
Definition: keys.h:198
teditor::Meta_Ctrl
static const key_t Meta_Ctrl
Definition: keys.h:15
INFO
#define INFO(fmt,...)
Definition: logger.h:72
teditor::Event_Key
@ Event_Key
Definition: terminal.h:32
teditor::Terminal::mk
MetaKey mk
Definition: terminal.h:48
teditor::Terminal::puts
void puts(const char *data)
Definition: terminal.h:66
teditor::Event_None
@ Event_None
Definition: terminal.h:35
teditor::SingletonHandler
Definition: logger.h:11
teditor::Key_Enter
static const key_t Key_Enter
Definition: keys.h:62
teditor::Func_EnterCA
@ Func_EnterCA
Definition: terminal.h:14
teditor::KeyCombo
Definition: terminal.cpp:175
teditor::Terminal::puts
void puts(const char *data, size_t len)
append data at the end of the current buffer contents
Definition: terminal.cpp:62
teditor::Key_Tab
static const key_t Key_Tab
Definition: keys.h:57
teditor::Func_Blink
@ Func_Blink
Definition: terminal.h:23
teditor::InfoCmp
Definition: infocmp.h:8
teditor::Pos2d::y
T y
Definition: pos2d.h:16
teditor::Terminal::loc
Pos2d< uint16_t > loc
Definition: terminal.h:50
teditor::Key_Space
static const key_t Key_Space
Definition: keys.h:88
teditor::Func_HideCursor
@ Func_HideCursor
Definition: terminal.h:17
teditor::Terminal::puts
void puts(const std::string &data)
Definition: terminal.h:67
teditor::Strings
std::vector< std::string > Strings
Definition: utils.h:42
teditor::Func_FuncsNum
@ Func_FuncsNum
Definition: terminal.h:28
teditor::func2terminfo
const char * func2terminfo(Func f)
Definition: terminal.cpp:19
teditor::AllCombos::test
static const AllCombos test
Definition: terminal.cpp:186
infocmp.h
teditor::MetaKey::setKey
void setKey(key_t k)
Definition: keys.h:196
teditor::Terminal::disableResize
void disableResize()
Definition: terminal.h:88
teditor::AllCombos::AllCombos
AllCombos()
Definition: terminal.cpp:184
utils.h
teditor::Func_Italic
@ Func_Italic
Definition: terminal.h:21
teditor::Terminal::UndefinedSequence
static const int UndefinedSequence
Definition: terminal.h:95
teditor::Terminal::height
int height() const
Definition: terminal.h:54
teditor::Terminal::updateTermSize
void updateTermSize()
Definition: terminal.cpp:80
teditor::MetaKey::setMeta
void setMeta(key_t m)
Definition: keys.h:197
teditor::sigwinch_handler
void sigwinch_handler(int xxx)
Definition: terminal.cpp:42
teditor::Terminal::type
EventType type
Definition: terminal.h:46
teditor::key_t
uint32_t key_t
Definition: keys.h:9
teditor::Key_Esc
static const key_t Key_Esc
Definition: keys.h:77
teditor::Func
Func
Definition: terminal.h:13
teditor::Terminal
Definition: terminal.h:43
teditor::MetaKey::reset
void reset()
Definition: keys.h:195
teditor::Func_ExitKeypad
@ Func_ExitKeypad
Definition: terminal.h:25
teditor::InfoCmp::getStrCap
std::string getStrCap(const std::string &cap) const
Definition: infocmp.cpp:51
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
teditor::Terminal::flush
void flush()
Definition: terminal.cpp:71
teditor::Terminal::waitAndFill
int waitAndFill(struct timeval *timeout)
Definition: terminal.cpp:269
teditor::Event_Resize
@ Event_Resize
Definition: terminal.h:33
teditor::KeyCombo::escSeq
std::string escSeq
Definition: terminal.cpp:177
file_utils.h
teditor::AllCombos
Definition: terminal.cpp:183
teditor::AllCombos::allKeys
static std::unordered_map< std::string, MetaKey > allKeys
Definition: terminal.cpp:185
teditor::Func_EnterKeypad
@ Func_EnterKeypad
Definition: terminal.h:24
teditor::Func_EnterMouse
@ Func_EnterMouse
Definition: terminal.h:26
teditor::Pos2d< uint16_t >
teditor::Terminal::reset
void reset()
Definition: terminal.cpp:192
teditor::Func_ExitMouse
@ Func_ExitMouse
Definition: terminal.h:27
DEBUG
#define DEBUG(fmt,...)
Definition: logger.h:73
teditor::Func_ClearScreen
@ Func_ClearScreen
Definition: terminal.h:18
teditor::Meta_Alt
static const key_t Meta_Alt
Definition: keys.h:14
teditor::Func_ShowCursor
@ Func_ShowCursor
Definition: terminal.h:16
teditor::KeyCombo::mk
MetaKey mk
Definition: terminal.cpp:176
pos2d.h
teditor::Func_Sgr0
@ Func_Sgr0
Definition: terminal.h:19
teditor::Func_Bold
@ Func_Bold
Definition: terminal.h:22
teditor::KeyCombo::Combos
static const std::vector< KeyCombo > Combos
Definition: terminal.cpp:178
teditor::Func_ExitCA
@ Func_ExitCA
Definition: terminal.h:15
ULTRA_DEBUG
#define ULTRA_DEBUG(fmt,...)
Definition: logger.h:74
teditor::Key_Backspace2
static const key_t Key_Backspace2
Definition: keys.h:181
teditor::Terminal::getInstance
static Terminal & getInstance()
Definition: terminal.cpp:57
teditor::Func_Underline
@ Func_Underline
Definition: terminal.h:20
terminal.h
teditor::Pos2d::x
T x
Definition: pos2d.h:16
xterm_strokes.h
teditor::Terminal::getOldSeq
const std::string & getOldSeq() const
Definition: terminal.h:84
teditor::Meta_None
static const key_t Meta_None
Definition: keys.h:12
teditor::env
std::string env(const std::string &s)
Definition: utils.cpp:36
teditor
Definition: any.hpp:10