Cleanup complete_cmd_desc

This commit is contained in:
ridiculousfish 2012-02-02 12:04:04 -08:00
parent 62f49c55ce
commit 884b4026dd
2 changed files with 27 additions and 49 deletions

View file

@ -913,9 +913,6 @@ static void complete_cmd_desc( const wchar_t *cmd, std::vector<completion_t> &co
{ {
const wchar_t *cmd_start; const wchar_t *cmd_start;
int cmd_len; int cmd_len;
wchar_t *lookup_cmd=0;
array_list_t list;
hash_table_t lookup;
wchar_t *esc; wchar_t *esc;
int skip; int skip;
@ -964,14 +961,10 @@ static void complete_cmd_desc( const wchar_t *cmd, std::vector<completion_t> &co
} }
esc = escape( cmd_start, 1 ); wcstring lookup_cmd(L"__fish_describe_command ");
lookup_cmd.append(escape_string(cmd_start, 1));
lookup_cmd = wcsdupcat( L"__fish_describe_command ", esc );
free(esc);
al_init( &list );
hash_init( &lookup, &hash_wcs_func, &hash_wcs_cmp );
std::map<wcstring, wcstring> lookup;
/* /*
First locate a list of possible descriptions using a single First locate a list of possible descriptions using a single
@ -980,7 +973,8 @@ static void complete_cmd_desc( const wchar_t *cmd, std::vector<completion_t> &co
systems with a large set of manuals, but it should be ok systems with a large set of manuals, but it should be ok
since apropos is only called once. since apropos is only called once.
*/ */
if( exec_subshell( lookup_cmd, &list ) != -1 ) wcstring_list_t list;
if( exec_subshell2( lookup_cmd, list ) != -1 )
{ {
/* /*
@ -990,31 +984,28 @@ static void complete_cmd_desc( const wchar_t *cmd, std::vector<completion_t> &co
Should be reasonably fast, since no memory allocations are needed. Should be reasonably fast, since no memory allocations are needed.
*/ */
for( int i=0; i<al_get_count( &list); i++ ) for( size_t i=0; i < list.size(); i++ )
{ {
wchar_t *el = (wchar_t *)al_get( &list, i ); const wcstring &elstr = list.at(i);
wchar_t *key, *key_end, *val_begin;
if( !el ) const wcstring fullkey(elstr, wcslen(cmd_start));
size_t tab_idx = fullkey.find(L'\t');
if( tab_idx == wcstring::npos )
continue; continue;
key = el+wcslen(cmd_start); const wcstring key(fullkey, 0, tab_idx);
key_end = wcschr( key, L'\t' ); wcstring val(fullkey, tab_idx + 1);
if( !key_end )
continue;
*key_end = 0;
val_begin = key_end+1;
/* /*
And once again I make sure the first character is uppercased And once again I make sure the first character is uppercased
because I like it that way, and I get to decide these because I like it that way, and I get to decide these
things. things.
*/ */
val_begin[0]=towupper(val_begin[0]); if (! val.empty())
val[0]=towupper(val[0]);
hash_put( &lookup, key, val_begin ); lookup[key] = val;
} }
/* /*
@ -1026,27 +1017,17 @@ static void complete_cmd_desc( const wchar_t *cmd, std::vector<completion_t> &co
*/ */
for( size_t i=0; i<comp.size(); i++ ) for( size_t i=0; i<comp.size(); i++ )
{ {
completion_t &c = comp.at( i ); completion_t &completion = comp.at(i);
// const wchar_t *el = c.completion.empty()?NULL:c.completion.c_str(); const wcstring &el = completion.completion;
const wchar_t *el = c.completion.c_str(); if (el.empty())
continue;
wchar_t *new_desc; std::map<wcstring, wcstring>::iterator new_desc_iter = lookup.find(el);
if (new_desc_iter != lookup.end())
new_desc = (wchar_t *)hash_get( &lookup, completion.description = new_desc_iter->second;
el );
if( new_desc )
{
c.description = new_desc;
}
} }
} }
hash_destroy( &lookup );
al_foreach( &list,
&free );
al_destroy( &list );
free( lookup_cmd );
} }
/** /**
@ -1158,8 +1139,6 @@ static void complete_cmd( const wchar_t *cmd,
These return the original strings - don't free them These return the original strings - don't free them
*/ */
// al_init( &possible_comp );
if( use_function ) if( use_function )
{ {
//function_get_names( &possible_comp, cmd[0] == L'_' ); //function_get_names( &possible_comp, cmd[0] == L'_' );

View file

@ -146,8 +146,7 @@ public:
bool operator < (const completion_t& rhs) const { return this->completion < rhs.completion; } bool operator < (const completion_t& rhs) const { return this->completion < rhs.completion; }
bool operator == (const completion_t& rhs) const { return this->completion == rhs.completion; } bool operator == (const completion_t& rhs) const { return this->completion == rhs.completion; }
bool operator != (const completion_t& rhs) const { return ! (*this == rhs); } bool operator != (const completion_t& rhs) const { return ! (*this == rhs); }
} };
;
/** /**