like editor
#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();
}