Skip to content

Instantly share code, notes, and snippets.

@pastagatsan
Last active August 29, 2015 14:02
Show Gist options
  • Save pastagatsan/fd0d86f17ad98e0ed37f to your computer and use it in GitHub Desktop.
Save pastagatsan/fd0d86f17ad98e0ed37f to your computer and use it in GitHub Desktop.
C++ program using NCurses!
#include <ncurses.h>
#include <cstdarg>
#include <stdio.h>
char * help = "Q = Quit, CTRL+E = Cycle through modes, CTRL+H = Show help\n";
const char MODE_EDITOR = 0, MODE_KEYPRESS = 1;
const char * modes[] = {"[0] Editor\n", "[1] Keypress Detecting\n"};
char mode = MODE_EDITOR;
const int MODE_COUNT = 2;
bool show_help = false;
const char * files[3] = {"main.cpp", "dude.cpp", "pleb.cpp"};
const char * file_content[3] =
{
"#include <iostream>\nint main()\n{\n\tstd::cout << \"wot?\\n\"\n}",
"empty file\n",
"another empty file?\nlol\n"
};
char index = 0;
/* Refreshes the screen. */
void show(char b);
/* Writes some text and then fills the line with spaces. */
void fill(int ln, char * str);
/* Finds the length of /string/. */
int len(char * string);
int main()
{
initscr();
start_color();
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_BLACK, COLOR_WHITE);
show(' ');
char x = getchar();
while (true)
{
if (mode == MODE_EDITOR)
{
if (x == 65 && index > 0) index --;
else if (x == 66 && index < 2) index ++;
}
if (x == 0x05) // CTRL+E
{
mode++;
if (mode == MODE_COUNT) mode = 0;
}
else if (x == 0x08)
{
show_help = !show_help;
}
else if (x == 0x04) // CTRL+D for commands
{
wmove(stdscr, 1, 1);
}
show(x);
x = getchar();
}
endwin();
}
void show(char b)
{
erase();
attron(COLOR_PAIR(2));
wmove(stdscr, 0, 0);
int xx = 0, yy = 0;
getyx(stdscr, yy, xx);
char * top_string = "";
sprintf(top_string, "Shell || Mode: %s\n", modes[mode]);
fill(0, top_string);
attroff(COLOR_PAIR(2));
mvwprintw(stdscr, 1, 0, "$\n");
if (mode == MODE_EDITOR)
{
attron(COLOR_PAIR(1));
mvwprintw(stdscr, 3, 0, file_content[index]);
attroff(COLOR_PAIR(1));
}
else if (mode == MODE_KEYPRESS)
{
printw("Pressed Key: %c | %d", b, b);
}
if (show_help)
{
attron(COLOR_PAIR(2));
fill(20, help);
attroff(COLOR_PAIR(2));
}
mvwprintw(stdscr, 21, 0, "Buffer Li");
if (b == 0x04) // CTRL+D
{
wmove(stdscr, 1, 1);
}
refresh();
}
void fill(int ln, char * str)
{
int wd = 0, wh = 0;
getmaxyx(stdscr, wh, wd);
int l = len(str);
for (int i = l; i < wd; i++)
{
mvwprintw(stdscr, ln, i, " ");
}
}
int len(char * string)
{
int a = 0;
while (1)
{
if (string[a] == '\0') return a;
a++;
}
}
@pastagatsan
Copy link
Author

plebeian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment