Implement and document new -P / --paging-mode flags to commandline, to support new

pager
This commit is contained in:
ridiculousfish 2014-01-17 12:53:01 -08:00
parent c6e5201e15
commit 32054b6f32
4 changed files with 62 additions and 71 deletions

View File

@ -213,6 +213,7 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv)
int cursor_mode = 0; int cursor_mode = 0;
int line_mode = 0; int line_mode = 0;
int search_mode = 0; int search_mode = 0;
int paging_mode = 0;
const wchar_t *begin, *end; const wchar_t *begin, *end;
current_buffer = (wchar_t *)builtin_complete_get_temporary_buffer(); current_buffer = (wchar_t *)builtin_complete_get_temporary_buffer();
@ -251,71 +252,24 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv)
static const struct woption static const struct woption
long_options[] = long_options[] =
{ {
{ { L"append", no_argument, 0, 'a' },
L"append", no_argument, 0, 'a' { L"insert", no_argument, 0, 'i' },
} { L"replace", no_argument, 0, 'r' },
, { L"current-job", no_argument, 0, 'j' },
{ { L"current-process", no_argument, 0, 'p' },
L"insert", no_argument, 0, 'i' { L"current-token", no_argument, 0, 't' },
} { L"current-buffer", no_argument, 0, 'b' },
, { L"cut-at-cursor", no_argument, 0, 'c' },
{ { L"function", no_argument, 0, 'f' },
L"replace", no_argument, 0, 'r' { L"tokenize", no_argument, 0, 'o' },
} { L"help", no_argument, 0, 'h' },
, { L"input", required_argument, 0, 'I' },
{ { L"cursor", no_argument, 0, 'C' },
L"current-job", no_argument, 0, 'j' { L"line", no_argument, 0, 'L' },
} { L"search-mode", no_argument, 0, 'S' },
, { L"paging-mode", no_argument, 0, 'P' },
{ { 0, 0, 0, 0 }
L"current-process", no_argument, 0, 'p' };
}
,
{
L"current-token", no_argument, 0, 't'
}
,
{
L"current-buffer", no_argument, 0, 'b'
}
,
{
L"cut-at-cursor", no_argument, 0, 'c'
}
,
{
L"function", no_argument, 0, 'f'
}
,
{
L"tokenize", no_argument, 0, 'o'
}
,
{
L"help", no_argument, 0, 'h'
}
,
{
L"input", required_argument, 0, 'I'
}
,
{
L"cursor", no_argument, 0, 'C'
}
,
{
L"line", no_argument, 0, 'L'
}
,
{
L"search-mode", no_argument, 0, 'S'
}
,
{
0, 0, 0, 0
}
}
;
int opt_index = 0; int opt_index = 0;
@ -397,6 +351,10 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv)
case 'S': case 'S':
search_mode = 1; search_mode = 1;
break; break;
case 'P':
paging_mode = 1;
break;
case 'h': case 'h':
builtin_print_help(parser, argv[0], stdout_buffer); builtin_print_help(parser, argv[0], stdout_buffer);
@ -415,7 +373,7 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv)
/* /*
Check for invalid switch combinations Check for invalid switch combinations
*/ */
if (buffer_part || cut_at_cursor || append_mode || tokenize || cursor_mode || line_mode || search_mode) if (buffer_part || cut_at_cursor || append_mode || tokenize || cursor_mode || line_mode || search_mode || paging_mode)
{ {
append_format(stderr_buffer, append_format(stderr_buffer,
BUILTIN_ERR_COMBO, BUILTIN_ERR_COMBO,
@ -464,7 +422,7 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv)
/* /*
Check for invalid switch combinations Check for invalid switch combinations
*/ */
if ((search_mode || line_mode || cursor_mode) && (argc-woptind > 1)) if ((search_mode || line_mode || cursor_mode || paging_mode) && (argc-woptind > 1))
{ {
append_format(stderr_buffer, append_format(stderr_buffer,
@ -475,7 +433,7 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv)
return 1; return 1;
} }
if ((buffer_part || tokenize || cut_at_cursor) && (cursor_mode || line_mode || search_mode)) if ((buffer_part || tokenize || cut_at_cursor) && (cursor_mode || line_mode || search_mode || paging_mode))
{ {
append_format(stderr_buffer, append_format(stderr_buffer,
BUILTIN_ERR_COMBO, BUILTIN_ERR_COMBO,
@ -564,7 +522,12 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv)
if (search_mode) if (search_mode)
{ {
return !reader_search_mode(); return ! reader_search_mode();
}
if (paging_mode)
{
return ! reader_has_pager_contents();
} }

View File

@ -57,6 +57,16 @@ If \c commandline is called during a call to complete a given string
using <code>complete -C STRING</code>, \c commandline will consider the using <code>complete -C STRING</code>, \c commandline will consider the
specified string to be the current contents of the command line. specified string to be the current contents of the command line.
The following options output metadata about the commandline state:
- \c -L or \c --line print the line that the cursor is on, with the topmost
line starting at 1
- \c -S or \c --search-mode evaluates to true if the commandline is performing
a history search
- \c -P or \c --paging-mode evaluates to true if the commandline is showing
pager contents, such as tab completions
\subsection commandline-example Example \subsection commandline-example Example
<tt>commandline -j $history[3]</tt> replaces the job under the cursor with the <tt>commandline -j $history[3]</tt> replaces the job under the cursor with the

View File

@ -3872,7 +3872,17 @@ int reader_search_mode()
return -1; return -1;
} }
return !!data->search_mode; return !! data->search_mode;
}
int reader_has_pager_contents()
{
if (!data)
{
return -1;
}
return data->current_page_rendering.screen_data.empty() ? 1 : 0;
} }

View File

@ -239,10 +239,18 @@ int reader_shell_test(const wchar_t *b);
/** /**
Test whether the interactive reader is in search mode. Test whether the interactive reader is in search mode.
\return o if not in search mode, 1 if in search mode and -1 if not in interactive mode \return 0 if not in search mode, 1 if in search mode and -1 if not in interactive mode
*/ */
int reader_search_mode(); int reader_search_mode();
/**
Test whether the interactive reader has visible pager contents.
\return 0 if it has pager contents, 1 if it does not have pager contents, and -1 if not in interactive mode
*/
int reader_has_pager_contents();
/* Given a command line and an autosuggestion, return the string that gets shown to the user. Exposed for testing purposes only. */ /* Given a command line and an autosuggestion, return the string that gets shown to the user. Exposed for testing purposes only. */
wcstring combine_command_and_autosuggestion(const wcstring &cmdline, const wcstring &autosuggestion); wcstring combine_command_and_autosuggestion(const wcstring &cmdline, const wcstring &autosuggestion);