Last active
April 5, 2018 14:27
-
-
Save maekawatoshiki/ec4a17a6170952aa864b to your computer and use it in GitHub Desktop.
like editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <cstdio> | |
#include <cstdlib> | |
#include <termios.h> | |
#include <unistd.h> | |
using namespace std; | |
static struct termios t_orig; | |
void begin_getch(void) | |
{ | |
struct termios t; | |
tcgetattr(0, &t); | |
t_orig = t; | |
t.c_lflag &= ~(ICANON | ECHO); | |
t.c_cc[VMIN] = 0; | |
t.c_cc[VTIME] = 0; | |
tcsetattr(0, TCSANOW, &t); | |
system("clear"); | |
} | |
char getch(void) | |
{ | |
char c; | |
return (read(0, &c, 1) != 0) ? c : 0; | |
} | |
void putch(char c) | |
{ | |
write(1, &c, 1); | |
} | |
void end_getch(void) | |
{ | |
tcsetattr(0, TCSADRAIN, &t_orig); | |
} | |
int main() | |
{ | |
string str; | |
char c, cstr[0xFFFF] = { 0 }; | |
cout << "Things like Vim \nCopyright(C) 2015 MAEKAWATOSHIKI." << endl; | |
getchar(); | |
system("clear"); | |
begin_getch(); | |
while(1) | |
{ | |
usleep(20000); | |
if ( (c = getch()) != 0 ) | |
{ | |
if(c == 127) | |
{ | |
if(str.size() != 0) | |
{ | |
str.erase(--str.end()); | |
system("clear"); | |
sprintf(cstr, "%s", str.c_str()); | |
write(1, &cstr, str.size()+1); | |
} | |
} | |
else | |
{ | |
str += c; | |
sprintf(cstr, "%s", str.c_str()); | |
putch(c); | |
} | |
} | |
} | |
end_getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment