Skip to content

Commit

Permalink
Crafting GUI Filter - save history and go trough it with arrow keys (C…
Browse files Browse the repository at this point in the history
…leverRaven#27107)

* Add possibility to go through query history in any instance of  'string_input_popup' with arrow keys in addition to 'UiList' select menu
Make filter in crafting GUI to save history and go through it with arrow keys
  • Loading branch information
Forsari0 authored and kevingranade committed Jan 20, 2019
1 parent 16d9515 commit 378ee2c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/crafting_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 +741,16 @@ const recipe *select_crafting_recipe( int &batch_size )
prefix.key, prefix.example, padding, spaces, prefix.description );
}

description +=
_( "\nYou can use <color_white>arrow keys</color> to go through search history\n\n" );

string_input_popup()
.title( _( "Search:" ) )
.width( 85 )
.description( description )
.desc_color( c_light_gray )
.identifier( "craft_recipe_filter" )
.hist_use_uilist( false )
.edit( filterstring );
redraw = true;
} else if( action == "QUIT" ) {
Expand Down
68 changes: 67 additions & 1 deletion src/string_input_popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,61 @@ void string_input_popup::add_to_history( const std::string &value ) const
}
}

void string_input_popup::update_input_history( utf8_wrapper &ret, bool up )
{
if( _identifier.empty() ) {
return;
}

std::vector<std::string> &hist = uistate.gethistory( _identifier );

if( hist.empty() ) {
return;
}

if( hist.size() >= _hist_max_size ) {
hist.erase( hist.begin(), hist.begin() + ( hist.size() - _hist_max_size ) );
}

if( up ) {
if( _hist_str_ind >= static_cast<int>( hist.size() ) ) {
return;
} else {
if( _hist_str_ind == 0 ) {
if( ret.empty() ) {
_session_str_entered.erase( 0 );
} else {
_session_str_entered = ret.str();
}

//avoid showing the same result twice (after reopen filter window without reset)
if( hist.size() > 1 && ret.str() == hist[hist.size() - 1] ) {
_hist_str_ind += 1;
}
}
}
} else {
if( _hist_str_ind == 1 ) {
if( _session_str_entered.empty() ) {
ret.erase( 0 );
} else {
ret = _session_str_entered;
_position = _session_str_entered.length();
}
//show initial string entered and 'return'
_hist_str_ind = 0;
}
if( _hist_str_ind == 0 ) {
return;
}
}

_hist_str_ind += up ? 1 : -1;
ret = hist[hist.size() - _hist_str_ind];
_position = ret.length();

}

void string_input_popup::draw( const utf8_wrapper &ret, const utf8_wrapper &edit,
const int shift ) const
{
Expand Down Expand Up @@ -325,9 +380,20 @@ const std::string &string_input_popup::query_string( const bool loop, const bool
add_to_history( ret.str() );
_confirmed = true;
_text = ret.str();
if( !_hist_use_uilist ) {
_hist_str_ind = 0;
_session_str_entered.erase( 0 );
}
return _text;
} else if( ch == KEY_UP ) {
show_history( ret );
if( _hist_use_uilist ) {
show_history( ret );
} else {
update_input_history( ret, true );
}
redraw = true;
} else if( ch == KEY_DOWN && !_hist_use_uilist ) {
update_input_history( ret, false );
redraw = true;
} else if( ch == KEY_DOWN || ch == KEY_NPAGE || ch == KEY_PPAGE || ch == KEY_BTAB || ch == 9 ) {
/* absolutely nothing */
Expand Down
15 changes: 15 additions & 0 deletions src/string_input_popup.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class string_input_popup
std::string _text;
std::string _description;
std::string _identifier;
std::string _session_str_entered;
nc_color _title_color = c_light_red;
nc_color _desc_color = c_green;
nc_color _string_color = c_magenta;
Expand All @@ -55,10 +56,14 @@ class string_input_popup
int _width = 0;
int _max_length = -1;
bool _only_digits = false;
bool _hist_use_uilist = true;
int _startx = 0;
int _starty = 0;
int _endx = 0;
int _position = -1;
int _hist_str_ind = 0;
//Counts only when @_hist_use_uilist is false
const size_t _hist_max_size = 100;

catacurses::window w;

Expand All @@ -73,6 +78,7 @@ class string_input_popup

void show_history( utf8_wrapper &ret );
void add_to_history( const std::string &value ) const;
void update_input_history( utf8_wrapper &ret, bool up );
void draw( const utf8_wrapper &ret, const utf8_wrapper &edit, int shift ) const;

public:
Expand Down Expand Up @@ -139,6 +145,15 @@ class string_input_popup
_only_digits = value;
return *this;
}
/**
* Make any difference only if @identifier is used.
* If true, create UiList window with query history, otherwise use arrow keys at string input to move through history.
* Default is true.
*/
string_input_popup &hist_use_uilist( bool value ) {
_hist_use_uilist = value;
return *this;
}
/**
* Set the window area where to display the input text. If this is set,
* the class will not create a separate window and *only* the editable
Expand Down

0 comments on commit 378ee2c

Please sign in to comment.