sources/interface.cpp

Sat, 13 Dec 2014 07:36:00 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 13 Dec 2014 07:36:00 +0200
changeset 14
33b8f428bacb
child 15
33da84af4bba
permissions
-rw-r--r--

- begin work on interface - input line works for the most part :)

/*
	Copyright 2014 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. Neither the name of the copyright holder nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
	PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
	OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <string.h>
#include "interface.h"

static String g_input;
static int g_cursor = 0;
static int g_pan = 0;
static bool g_needRefresh = false;

// -------------------------------------------------------------------------------------------------
//
static FUNCTION
interface_sessions_width() -> int
{
	return COLS / 3;
}

// -------------------------------------------------------------------------------------------------
//
static FUNCTION
interface_render_titlebar() -> void
{
	String versionText = format (APPNAME " %1 (%2)",
		full_version_string(), changeset_date_string());

	if (versionText.length() <= COLS)
		mvprintw (0, (COLS - versionText.length()) / 2, "%s", versionText.chars());
}

// -------------------------------------------------------------------------------------------------
//
static FUNCTION
interface_render_log_area() -> void
{

}

// -------------------------------------------------------------------------------------------------
//
static FUNCTION
interface_render_input() -> void
{
	static char prompt[] = "> ";
	int displaylength = COLS - strlen (prompt) - 1;
	int y = LINES - 2;

	// Ensure that the cursor is always in view, adjust panning if this is not the case
	if (g_cursor > g_pan + displaylength)
		g_pan = g_cursor - displaylength; // cursor went too far right
	else if (g_cursor < g_pan)
		g_pan = g_cursor; // cursor went past the pan value to the left

	int start = g_pan;
	int end = min<int> (g_input.length(), start + displaylength);
	assert (g_cursor >= start and g_cursor <= end);
	String displayTextBegin = g_input.mid (start, g_cursor);
	String displayTextEnd = g_input.mid (g_cursor, end);

	// Clear, but only as much as is necessary. I want to avoid clearing the entire line to save
	// bandwidth over SSH connections. Perhaps needlessly? I'm paranoid.
	int renderLength = strlen (prompt) + displayTextBegin.length() + displayTextEnd.length();
	static int lastRenderLength = 0;

	if (lastRenderLength > renderLength)
		mvhline (y, renderLength, ' ', lastRenderLength - renderLength);

	lastRenderLength = renderLength;

	// Render the input line, with the part of the input string that's before the cursor written
	// AFTER the part that comes afterwards. This is to ensure that the cursor is placed at the
	// position where our cursor is. It looks nice like that. :)
	mvprintw (y, 0, "%s", prompt);
	mvprintw (y, strlen (prompt) + displayTextBegin.length(), "%s", displayTextEnd.chars());
	mvprintw (y, strlen (prompt), "%s", displayTextBegin.chars());
	g_needRefresh = true;
}

// -------------------------------------------------------------------------------------------------
//
static FUNCTION
interface_render_statusbar() -> void
{

}

// -------------------------------------------------------------------------------------------------
//
static FUNCTION
interface_render_full() -> void
{
	interface_render_titlebar();
	interface_render_log_area();
	interface_render_statusbar();
	interface_render_input();
	g_needRefresh = true;
}

// -------------------------------------------------------------------------------------------------
//
FUNCTION
Interface::initialize() -> void
{
	::initscr();
	::start_color();
	::raw();
	::keypad (stdscr, true);
	::noecho();
	::refresh();
	::timeout (0);
	interface_render_full();
	refresh();
	g_needRefresh = false;
}

// -------------------------------------------------------------------------------------------------
//
FUNCTION
Interface::handle_input() -> void
{
	int ch = ::getch();

	if (ch == KEY_F(1))
	{
		endwin();
		exit (EXIT_SUCCESS);
	}
	else if (ch >= 0x20 and ch <= 0x7E)
	{
		g_input.insert (g_cursor++, char (ch));
		interface_render_input();
		refresh();
	}
	else if (ch == KEY_LEFT)
	{
		if (g_cursor > 0)
		{
			g_cursor--;
			interface_render_input();
		}
	}
	else if (ch == KEY_RIGHT)
	{
		if (g_cursor < g_input.length())
		{
			g_cursor++;
			interface_render_input();
		}
	}
	else if (ch == KEY_HOME)
	{
		if (g_cursor != 0)
		{
			g_cursor = 0;
			interface_render_input();
		}
	}
	else if (ch == KEY_END)
	{
		if (g_cursor != g_input.length())
		{
			g_cursor = g_input.length();
			interface_render_input();
		}
	}
	else if (ch == KEY_BACKSPACE)
	{
		if (not g_input.is_empty() and g_cursor > 0)
		{
			g_input.remove_at (--g_cursor);
			interface_render_input();
		}
	}

	if (g_needRefresh)
	{
		refresh();
		g_needRefresh = false;
	}
}

FUNCTION print_to_console (const String& a) -> void
{
	
}

mercurial