-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_editor.cpp
More file actions
178 lines (167 loc) · 4.45 KB
/
Copy pathtext_editor.cpp
File metadata and controls
178 lines (167 loc) · 4.45 KB
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
#include <iostream>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <vector>
#include <string>
#include <fstream>
class terminalSettings
{
public:
terminalSettings()
{
std::cout << "\033[2J";
std::cout << "\033[1;1H";
tcgetattr(STDIN_FILENO, &orig_termios);
newtermios = orig_termios;
newtermios.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG);
newtermios.c_lflag &= ~(OPOST);
newtermios.c_lflag &= ~(ICRNL | IXON | INPCK | ISTRIP);
newtermios.c_iflag &= ~(IXON);
tcsetattr(STDIN_FILENO, TCSANOW, &newtermios);
}
~terminalSettings()
{
std::cout << "\033[2J";
std::cout << "\033[1;1H";
tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
}
char getChar()
{
char c;
read(STDIN_FILENO, &c, 1);
return c;
}
void enableecho()
{
newtermios.c_lflag |= ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newtermios);
}
void disableecho()
{
newtermios.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newtermios);
}
void movecursorup(int n = 1)
{
std::cout << "\033[" << n << "A";
}
void movecursordown(int n = 1)
{
std::cout << "\033[" << n << "B";
}
void movecursorleft(int n = 1)
{
std::cout << "\033[" << n << "D";
}
void movecursorright(int n = 1)
{
std::cout << "\033[" << n << "C";
}
private:
struct termios newtermios, orig_termios;
};
void saveFile(std::string filename, std::vector<std::string>data)
{
std::cout << std::endl;
std::cout << "Enter the file name:- ";
std::cin >> filename;
std::ofstream saveFile(filename);
if (saveFile.is_open())
{
for (const auto &line : data)
{
saveFile << line << std::endl;
}
saveFile.flush();
saveFile.close();
std::cout << filename << " saved successfully" << std::endl;
}
else
{
std::cerr << "Failed to save file" << std::endl;
}
}
int main()
{
terminalSettings input;
char c;
std::cout << "Press any key (OR Press Q to exit)" << std::endl;
std::string currentline;
std::vector<std::string> data;
bool isEditorMode = false;
std::string filename;
while (true)
{
c = input.getChar();
if (isEditorMode)
{
if (c == 27)
{
c = input.getChar();
if (c == 'i')
{
std::cout << std::endl;
std::cout << "User is in editor mode" << std::endl;
}
else
{
isEditorMode = false;
input.disableecho();
std::cout << "User is in control mode" << std::endl;
}
}
else if (c == '\n')
{
data.push_back(currentline);
currentline.clear();
std::cout << std::endl;
}
else if (c == 19)
{
saveFile(filename, data);
}
else if (c == 127 || c == 8)
{
if (!currentline.empty())
{
currentline.pop_back();
std::cout << "\b \b";
}
}
else
{
currentline += c;
}
}
else
{
if (c == 27)
{
c = input.getChar();
if (c == 'i')
{
isEditorMode = true;
input.enableecho();
std::cout << "Switched to editor mode. Type your text." << std::endl;
}
}
else
{
if (c == 'q' || c == 'Q')
break;
if (c == 'w' || c == 'W')
input.movecursorup();
else if (c == 'a' || c == 'A')
input.movecursorleft();
else if (c == 's' || c == 'S')
input.movecursordown();
else if (c == 'd' || c == 'D')
input.movecursorright();
else
std::cout << "Unknown command: " << c << std::endl;
}
}
}
return 0;
}