~strahinja/poe

894ca28b00ec888e448a3d645489c7274d4f936b — Страхиња Радић 7 days ago 8197283
Reorganized key handling into a separate routine

Signed-off-by: Страхиња Радић <contact@strahinja.org>
3 files changed, 66 insertions(+), 64 deletions(-)

M draw.c
M poe.c
M util.h
M draw.c => draw.c +2 -1
@@ 23,7 23,8 @@ const char* errors[] = {
	[ERR_UNKNOWN_KEY] = "Unknown key (press H for help)",
	[ERR_NO_PREV]     = "No previous entry",
	[ERR_NO_NEXT]     = "No next entry",
	[ERR_EXIT_KEY]    = "Press q to quit"
	[ERR_EXIT_KEY]    = "Press q to quit",
	[ERR_DLG_OPEN]	  = "Press Esc to close dialog or q to quit"
};
const char* flag_strings[] = {
	[FL_C_FORMAT]	  = "c-format",

M poe.c => poe.c +62 -62
@@ 65,6 65,66 @@ format_msgs(char* result, size_t result_size, const char* format)
	snprintf(result, result_size, format, msgid_number, msgid_count);
}

void
handle_key_event(const struct tb_event* ev, int* running)
{
	if (ev->key == TB_KEY_ESC)
	{
		if (error[0])
			error[0] = 0;
		if (show_help)
			show_help = 0;
		else
			strcpy(error, errors[ERR_EXIT_KEY]);
	}
	else if (ev->ch == 'q')
		*running = 0;
	else if (show_help)
		strcpy(error, errors[ERR_DLG_OPEN]);
	else if (ev->key == TB_KEY_F1 
			|| ev->ch == 'H')
	{
		show_help = 1;
		error[0] = 0;
	}
	else if (ev->key == TB_KEY_HOME
			|| ev->ch == 'g')
	{
		error[0] = 0;
		msgid_number = msgid_count ? 1 : 0;
	}
	else if (ev->key == TB_KEY_ARROW_UP 
			|| ev->ch == 'k')
	{
		if (msgid_number < 2)
			strcpy(error, errors[ERR_NO_PREV]);
		else
		{
			error[0] = 0;
			msgid_number--;
		}
	}
	else if (ev->key == TB_KEY_ARROW_DOWN 
			|| ev->ch == 'j')
	{
		if (msgid_number == msgid_count)
			strcpy(error, errors[ERR_NO_NEXT]);
		else
		{
			error[0] = 0;
			msgid_number++;
		}
	}
	else if (ev->key == TB_KEY_END
			|| ev->ch == 'G')
	{
		error[0] = 0;
		msgid_number = msgid_count;
	}
	else
		strcpy(error, errors[ERR_UNKNOWN_KEY]);
}

int
main(int argc, char** argv)
{


@@ 100,68 160,8 @@ main(int argc, char** argv)
		maxy = tb_height();
		draw();
		tb_present();
		if (tb_peek_event(&ev, 20) > 0)
		{
			switch (ev.type)
			{
				case TB_EVENT_KEY:

					if (ev.key == TB_KEY_ESC)
					{
						if (show_help)
							show_help = 0;
						else if (error[0])
							error[0] = 0;
						else
							strcpy(error, errors[ERR_EXIT_KEY]);
					}
					else if (ev.key == TB_KEY_F1 
							|| ev.ch == 'H')
					{
						show_help = 1;
						error[0] = 0;
					}
					else if (ev.ch == 'q')
						running = 0;
					else if (ev.key == TB_KEY_HOME
							|| ev.ch == 'g')
					{
						error[0] = 0;
						msgid_number = msgid_count ? 1 : 0;
					}
					else if (ev.key == TB_KEY_ARROW_UP 
							|| ev.ch == 'k')
					{
						if (msgid_number < 2)
							strcpy(error, errors[ERR_NO_PREV]);
						else
						{
							error[0] = 0;
							msgid_number--;
						}
					}
					else if (ev.key == TB_KEY_ARROW_DOWN 
							|| ev.ch == 'j')
					{
						if (msgid_number == msgid_count)
							strcpy(error, errors[ERR_NO_NEXT]);
						else
                                                {
							error[0] = 0;
							msgid_number++;
						}
					}
					else if (ev.key == TB_KEY_END
							|| ev.ch == 'G')
					{
						error[0] = 0;
						msgid_number = msgid_count;
					}
					else
						strcpy(error, errors[ERR_UNKNOWN_KEY]);
					break;
			}
		}
		if (tb_peek_event(&ev, 20) > 0 && ev.type == TB_EVENT_KEY)
			handle_key_event(&ev, &running);
		if (running)
		{
			sleep_spec.tv_nsec = 500;

M util.h => util.h +2 -1
@@ 4,7 4,8 @@ enum {
	ERR_UNKNOWN_KEY,
	ERR_NO_PREV,
	ERR_NO_NEXT,
	ERR_EXIT_KEY
	ERR_EXIT_KEY,
	ERR_DLG_OPEN
};

#define MAXBUF 255