diff --git a/README.md b/README.md index b31184a..5961d6b 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ Probably the most well-developed program I made this summer. ##How to use: - usage: text [file ] + usage: text [ file ] -* F4 quits (no prompt) +* F4 quits (after prompt) * F5 saves the current file (untitled.txt is the default name) * F6 saves the current file and allows you to specify the filename. (WARNING: no confirmation screen or error checking yet). diff --git a/prompt.c b/prompt.c index a920507..08144a6 100644 --- a/prompt.c +++ b/prompt.c @@ -1,33 +1,101 @@ #include "prompt.h" -void prompt_string(const char *message, char *name) -{ - echo(); - WINDOW *prompt = subwin(stdscr, PROMPT_LINES, PROMPT_COLS, - center_y(PROMPT_LINES), center_x(PROMPT_COLS)); - mvwprintw(prompt, 1, 1, message); - box(prompt, 0, 0); - wmove(prompt, PROMPT_OFFY, PROMPT_OFFX); - - wattron(prompt, A_REVERSE); - wprintw(prompt, " "); - wmove(prompt, PROMPT_OFFY, PROMPT_OFFX); - wgetstr(prompt, name); - wattroff(prompt, A_REVERSE); - - wborder(prompt, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '); - werase(prompt); - wrefresh(prompt); - delwin(prompt); - noecho(); -} - -int center_x(int width) +static int center_x(int width) { return (COLS - width) / 2; } -int center_y(int height) +static int center_y(int height) { return (LINES - height) / 2; } + +static WINDOW* create_prompt(const char *message, int height, int width) +{ + WINDOW *prompt = subwin(stdscr, height, width, + center_y(height), center_x(width)); + mvwprintw(prompt, 1, 1, message); + box(prompt, 0, 0); + wmove(prompt, PROMPT_OFFY, PROMPT_OFFX); + return prompt; +} + +static void dest_prompt(WINDOW *prompt) +{ + wborder(prompt, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '); + werase(prompt); + wrefresh(prompt); + delwin(prompt); +} + +void prompt_string(const char *message, char *name, int size) +{ + echo(); + WINDOW *prompt = create_prompt(message, + PROMPT_STRING_LINES, + PROMPT_STRING_COLS); + + wattron(prompt, A_REVERSE); + wprintw(prompt, " "); + wmove(prompt, PROMPT_OFFY, PROMPT_OFFX); + wgetnstr(prompt, name, size); + wattroff(prompt, A_REVERSE); + + dest_prompt(prompt); + noecho(); +} + +int prompt_yesno(const char *message) +{ + int prompt_width = strlen(message) + 2; + int yes_x = prompt_width / 2 - 6; + int yes_y = 3; + int no_x = prompt_width / 2 + 6; + int no_y = 3; + + WINDOW *prompt = create_prompt(message, + PROMPT_YESNO_LINES, + prompt_width); + keypad(prompt, TRUE); + + int choice = 0; + + + + while(1) + { + if(choice == 0) // highlight the current choice + { + wattron(prompt, A_REVERSE); + mvwprintw(prompt, yes_y, yes_x, "YES"); + wattroff(prompt, A_REVERSE); + mvwprintw(prompt, no_y, no_x, "NO"); + } + else + { + mvwprintw(prompt, yes_y, yes_x, "YES"); + wattron(prompt, A_REVERSE); + mvwprintw(prompt, no_y, no_x, "NO"); + wattroff(prompt, A_REVERSE); + } + + int ch = wgetch(prompt); + switch(ch) + { + case KEY_LEFT: + case KEY_RIGHT: + choice = !choice; + break; + case '\n': + goto end_prompt_yesno; + break; + default: + break; + } + } + +end_prompt_yesno: + dest_prompt(prompt); + return !choice; // yes is 0 +} + diff --git a/prompt.h b/prompt.h index d0bab76..d9aa85c 100644 --- a/prompt.h +++ b/prompt.h @@ -4,14 +4,16 @@ #include #include -#define PROMPT_LINES 5 -#define PROMPT_COLS 20 +#define PROMPT_STRING_LINES 5 +#define PROMPT_STRING_COLS 20 + +#define PROMPT_YESNO_LINES 5 +#define PROMPT_YESNO_COLS 30 #define PROMPT_OFFX 1 #define PROMPT_OFFY 3 -void prompt_string(const char *message, char *name); -int center_x(int width); -int center_y(int height); +void prompt_string(const char *message, char *name, int size); +int prompt_yesno(const char *message); #endif diff --git a/text.c b/text.c index cd38dda..a3c0afb 100644 --- a/text.c +++ b/text.c @@ -63,7 +63,8 @@ int main(int argc, char *argv[]) switch(ch) { case KEY_F(4): - goto endnc; + if(prompt_yesno("Are you sure you want to quit?")) + goto endnc; break; case KEY_F(5): save_file(argc, argv, &page); @@ -71,7 +72,7 @@ int main(int argc, char *argv[]) update_status(status); break; case KEY_F(6): - prompt_string("Save As:", page.filename); + prompt_string("Save As:", page.filename, NAME_LIMIT); save_file(argc, argv, &page); sprintf(status, "Saved as \'%s\'", page.filename); print_page(&page, beg, end);