Skip to content

Instantly share code, notes, and snippets.

@pastagatsan
Created June 22, 2014 12:48
Show Gist options
  • Save pastagatsan/5deddeca39827826a0eb to your computer and use it in GitHub Desktop.
Save pastagatsan/5deddeca39827826a0eb to your computer and use it in GitHub Desktop.
#include <ncurses.h>
#include <string.h>
const char * words[] = {
"This", "is", "a", "list", "of", "words!"
};
const char * modes[] = {
"[0] Editor", "[1] Keypress"
};
const char BKSPACE = 127;
int mode;
const int MODE_COUNT = 2;
const int MODE_EDITOR = 0, MODE_KEYPRESS = 1;
unsigned int editor_pos = 14;
char editor_string[256] = "Hello world! :)";
/* All this function does is clear and show the screen again. */
void show_screen(char b);
/* Fills the line with spaces. */
void fill(int ln);
/* Keypress mode code. /b/ is the key pressed. */
void mode_keypress(char b);
/* Editor mode code. /b/ is the key pressed. */
void mode_editor(char b);
/* Finds the amount of newline characters within /string/. */
int lines(char * string);
int main()
{
initscr();
if (has_colors() == FALSE)
{
mvwprintw(stdscr, 0, 0, "Your terminal doesn't support color. Oh well.\n");
mvwprintw(stdscr, 1, 0, "~ Press any key to quit");
getch();
endwin();
return 1;
}
start_color();
init_pair(1, COLOR_WHITE, COLOR_BLACK);
init_pair(2, COLOR_BLACK, COLOR_WHITE);
while (true)
{
char b = getch();
show_screen(b);
}
endwin();
}
void show_screen(char b)
{
erase();
if (b == '`')
{
mode++;
if (mode == MODE_COUNT) mode = 0;
}
attron(COLOR_PAIR(2));
fill(0);
mvwprintw(stdscr, 0, 0, "~~ Mode: %s", modes[mode]);
attroff(COLOR_PAIR(2));
if (mode == MODE_KEYPRESS)
{
mode_keypress(b);
}
else if (mode == MODE_EDITOR)
{
mode_editor(b);
}
refresh();
}
void fill(int ln)
{
int wd = 0, wh = 0;
getmaxyx(stdscr, wh, wd);
for (int i = 0; i < wd; i++)
{
mvwprintw(stdscr, ln, i, " ");
}
}
void mode_keypress(char b)
{
attron(COLOR_PAIR(1));
mvwprintw(stdscr, 1, 0, "Character: %c | Decimal: %d", b, (int)b);
attroff(COLOR_PAIR(1));
}
void mode_editor(char b)
{
int length = strlen(editor_string);
if (b == BKSPACE)
{
if (length > 0)
{
editor_string[length-1] = '\0';
editor_pos--;
}
}
else if (b == 'D') // ?
{
}
else
{
editor_string[editor_pos] = b;
editor_string[editor_pos + 1] = '\0';
editor_pos++;
}
mvwprintw(stdscr, 1, 0, editor_string);
}
int lines(char * string)
{
int i = 0, j = 0;
while (true)
{
if (string[j] == '\n') i++;
if (string[j] == '\0') return i;
j++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment