Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI improvements #127

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions src/rpc/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#ifdef HAVE_EDITLINE
# include "editline.h"
# define CONFIG_UNIQUE_HISTORY // don't store the new command to history if it is identical to the last command
# ifdef WIN32
# include <io.h>
# endif
Expand Down Expand Up @@ -56,6 +57,11 @@ void cli::send_notice( uint64_t callback_id, variants args /* = variants() */ )
void cli::start()
{
cli_commands() = get_method_names(0);

#ifdef HAVE_EDITLINE
el_hist_size = 256;
#endif

_run_complete = fc::async( [&](){ run(); } );
}

Expand Down Expand Up @@ -137,36 +143,52 @@ void cli::run()
*/
static char *my_rl_complete(char *token, int *match)
{
bool have_one = false;
std::string method_name;

auto& cmd = cli_commands();
const auto& cmds = cli_commands();
const size_t partlen = strlen (token); /* Part of token */

for (const std::string& it : cmd)
std::vector<std::reference_wrapper<const std::string>> matched_cmds;
for( const std::string& it : cmds )
{
if (it.compare(0, partlen, token) == 0)
if( it.compare(0, partlen, token) == 0 )
{
if (have_one) {
// we can only have 1, but we found a second
return NULL;
}
else
{
method_name = it;
have_one = true;
}
matched_cmds.push_back( it );
}
}

if (have_one)
if( matched_cmds.size() == 0 )
return NULL;

const std::string& first_matched_cmd = matched_cmds[0];
if( matched_cmds.size() == 1 )
{
*match = 1;
method_name += " ";
return strdup (method_name.c_str() + partlen);
std::string matched_cmd = first_matched_cmd + " ";
return strdup( matched_cmd.c_str() + partlen );
}

size_t first_cmd_len = first_matched_cmd.size();
size_t matched_len = partlen;
for( ; matched_len < first_cmd_len; ++matched_len )
{
char next_char = first_matched_cmd[matched_len];
bool end = false;
for( const std::string& s : matched_cmds )
{
if( s.size() <= matched_len || s[matched_len] != next_char )
{
end = true;
break;
}
}
if( end )
break;
}

return NULL;
if( matched_len == partlen )
return NULL;

std::string matched_cmd_part = first_matched_cmd.substr( partlen, matched_len - partlen );
return strdup( matched_cmd_part.c_str() );
}

/***
Expand Down