From 44e09d166218e8f1374e8130c59368646ab44ce9 Mon Sep 17 00:00:00 2001 From: Mazar Farran Date: Fri, 21 Aug 2015 16:18:49 -0700 Subject: [PATCH] Add a prompt --- .gitignore | 1 + makefile | 9 ++++++--- prompt.c | 33 +++++++++++++++++++++++++++++++++ prompt.h | 17 +++++++++++++++++ text.c | 8 +++++++- text.h | 1 + 6 files changed, 65 insertions(+), 4 deletions(-) mode change 100644 => 100755 .gitignore create mode 100644 prompt.c create mode 100644 prompt.h diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index 31ee6fd..35f21c9 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.o *.txt text +text.exe diff --git a/makefile b/makefile index 32ad88f..e020004 100644 --- a/makefile +++ b/makefile @@ -2,13 +2,13 @@ CC=gcc CFLAGS=-Wall -g -OBJS=text.o page.o line.o +OBJS=text.o page.o line.o prompt.o LIBS=-lncurses text: $(OBJS) - $(CC) $(CFLAGS) -o text $(OBJS) $(LIBS) + $(CC) $(CFLAGS) -o text.exe $(OBJS) $(LIBS) -text.o: text.c text.h page.h line.h +text.o: text.c text.h page.h line.h prompt.h $(CC) $(CFLAGS) -c text.c page.o: page.c page.h line.h @@ -16,6 +16,9 @@ page.o: page.c page.h line.h line.o: line.c line.h $(CC) $(CFLAGS) -c line.c + +prompt.o: prompt.c prompt.h + $(CC) $(CFLAGS) -c prompt.c clean: rm -f $(OBJS) text diff --git a/prompt.c b/prompt.c new file mode 100644 index 0000000..a920507 --- /dev/null +++ b/prompt.c @@ -0,0 +1,33 @@ +#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) +{ + return (COLS - width) / 2; +} + +int center_y(int height) +{ + return (LINES - height) / 2; +} diff --git a/prompt.h b/prompt.h new file mode 100644 index 0000000..d0bab76 --- /dev/null +++ b/prompt.h @@ -0,0 +1,17 @@ +#ifndef PROMPT_H +#define PROMPT_H + +#include +#include + +#define PROMPT_LINES 5 +#define PROMPT_COLS 20 + +#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); + +#endif diff --git a/text.c b/text.c index 10352c5..bab84fa 100644 --- a/text.c +++ b/text.c @@ -53,6 +53,7 @@ int main(int argc, char *argv[]) print_page(&page, beg, end); getyx(stdscr, y, x); + char status[NAME_LIMIT + 10]; while(true) { beg = 0 + y_offset; @@ -66,10 +67,15 @@ int main(int argc, char *argv[]) break; case KEY_F(5): save_file(argc, argv, &page); - char status[NAME_LIMIT + 10]; sprintf(status, "Saved as \'%s\'", page.filename); update_status(status); break; + case KEY_F(6): + prompt_string("Save As:", page.filename); + save_file(argc, argv, &page); + sprintf(status, "Saved as \'%s\'", page.filename); + update_status(status); + break; case KEY_UP: move_up(&page, &x, &y); print_loc(x, y); diff --git a/text.h b/text.h index bdc38e3..adea391 100644 --- a/text.h +++ b/text.h @@ -8,6 +8,7 @@ #include // -lncurses. declared in page.h #include "page.h" +#include "prompt.h" void update_status(char *info);