erebus

changeset 42:b9294cd6b9dc

console input history with up/down
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 10 Jun 2014 16:15:08 +0300
parents 2e817711d0f6
children ed18af9da8f7
files src/console.cc src/console.h
diffstat 2 files changed, 39 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/src/console.cc	Tue Jun 10 12:28:56 2014 +0300
     1.2 +++ b/src/console.cc	Tue Jun 10 16:15:08 2014 +0300
     1.3 @@ -19,6 +19,8 @@
     1.4  	max_hist_lines = 64;
     1.5  	max_output_lines = 128;
     1.6  
     1.7 +	hist_iter_valid = false;
     1.8 +
     1.9  	echo = true;
    1.10  	font = 0;
    1.11  	font_size = 0;
    1.12 @@ -142,6 +144,8 @@
    1.13  			if((int)hist.size() > max_hist_lines) {
    1.14  				hist.pop_front();
    1.15  			}
    1.16 +			hist_iter_valid = false;
    1.17 +
    1.18  			set_cursor(0);
    1.19  			must_redraw = true;
    1.20  			break;
    1.21 @@ -164,6 +168,36 @@
    1.22  			}
    1.23  			break;
    1.24  
    1.25 +		case KEY_UP:
    1.26 +			if(!hist.empty()) {
    1.27 +				if(!hist_iter_valid) {
    1.28 +					hist_iter = hist.rbegin();
    1.29 +					hist_iter_valid = true;
    1.30 +					input = *hist_iter;
    1.31 +				} else {
    1.32 +					if(++hist_iter == hist.rend()) {
    1.33 +						--hist_iter;
    1.34 +						break;
    1.35 +					}
    1.36 +					input = *hist_iter;
    1.37 +				}
    1.38 +				set_cursor(input.size());
    1.39 +			}
    1.40 +			break;
    1.41 +
    1.42 +		case KEY_DOWN:
    1.43 +			if(!hist.empty()) {
    1.44 +				if(!hist_iter_valid) {
    1.45 +					hist_iter = hist.rbegin();
    1.46 +					hist_iter_valid = true;
    1.47 +				}
    1.48 +				if(hist_iter != hist.rbegin()) {
    1.49 +					input = *--hist_iter;
    1.50 +					set_cursor(input.size());
    1.51 +				}
    1.52 +			}
    1.53 +			break;
    1.54 +
    1.55  		case KEY_LEFT:
    1.56  			if(cursor > 0) {
    1.57  				set_cursor(get_cursor() - 1);
    1.58 @@ -190,7 +224,7 @@
    1.59  
    1.60  		case KEY_PGUP:
    1.61  		case KEY_PGDOWN:
    1.62 -			// TODO history
    1.63 +			// TODO scroll output buffer
    1.64  			break;
    1.65  
    1.66  		default:
    1.67 @@ -284,8 +318,8 @@
    1.68  	float cursor_x = dtx_char_pos(inpline.c_str(), cursor - input_win + prompt.size());
    1.69  	glBegin(GL_QUADS);
    1.70  	glColor4f(1, 1, 1, 0.7);
    1.71 -	glVertex2f(cursor_x, 0);
    1.72 -	glVertex2f(cursor_x + 2, 0);
    1.73 +	glVertex2f(cursor_x, -2);
    1.74 +	glVertex2f(cursor_x + 2, -2);
    1.75  	glVertex2f(cursor_x + 2, line_height - 2);
    1.76  	glVertex2f(cursor_x, line_height - 2);
    1.77  	glEnd();
     2.1 --- a/src/console.h	Tue Jun 10 12:28:56 2014 +0300
     2.2 +++ b/src/console.h	Tue Jun 10 16:15:08 2014 +0300
     2.3 @@ -25,6 +25,8 @@
     2.4  	// input history
     2.5  	int max_hist_lines;
     2.6  	std::list<std::string> hist;
     2.7 +	bool hist_iter_valid;
     2.8 +	std::list<std::string>::const_reverse_iterator hist_iter;
     2.9  
    2.10  	// console output
    2.11  	int max_output_lines;