implement the fish_breakpoint_prompt function

This is another step to resolving issue #1310. It makes
`fish_breakpoint_prompt` a replacement for `fish_prompt` if it is defined
and we're presenting a prompt in the context of a `breakpoint` command.
This commit is contained in:
Kurtis Rader 2017-06-19 22:57:32 -07:00
parent bd299e96b2
commit df01547eab
3 changed files with 68 additions and 6 deletions

View file

@ -0,0 +1,30 @@
\section fish_breakpoint_prompt fish_breakpoint_prompt - define the appearance of the command line prompt when in the context of a `breakpoint` command
\subsection fish_breakpoint_prompt-synopsis Synopsis
\fish{synopsis}
function fish_breakpoint_prompt
...
end
\endfish
\subsection fish_breakpoint_prompt-description Description
By defining the `fish_breakpoint_prompt` function, the user can choose a custom prompt when asking for input in response to a `breakpoint` command. The `fish_breakpoint_prompt` function is executed when the prompt is to be shown, and the output is used as a prompt.
The exit status of commands within `fish_breakpoint_prompt` will not modify the value of <a href="index.html#variables-status">$status</a> outside of the `fish_breakpoint_prompt` function.
`fish` ships with a default version of this function that displays the function name and line number of the current execution context.
\subsection fish_breakpoint_prompt-example Example
A simple prompt that is a simplified version of the default debugging prompt:
\fish
function fish_breakpoint_prompt -d "Write out the debug prompt"
set -l function (status current-function)
set -l line (status current-line-number)
set -l prompt "$function:$line >"
echo -ns (set_color $fish_color_status) "BP $prompt" (set_color normal) ' '
end
\endfish

View file

@ -0,0 +1,23 @@
# Define the default debugging prompt command.
function fish_breakpoint_prompt --description "A right prompt to be used when `breakpoint` is executed"
set -l saved_status $status
set -l function (status current-function)
set -l line (status current-line-number)
# At the moment we don't include the filename because, even if we truncate it, it makes the
# prompt too long.
#set -l filename (status current-filename)
#set -l prompt "$filename:$function:$line >"
set -l prompt "$function:$line"
if test $saved_status -ne 0
set prompt "$prompt [!$saved_status]"
end
set prompt "$prompt > "
# Make sure the prompt doesn't consume more than half the terminal width.
set -l max_len (math "$COLUMNS / 2")
if test (string length -- $prompt) -gt $max_len
set prompt ...(string sub -s -(math $max_len - 3) -- $prompt)
end
echo -ns (set_color $fish_color_status) "BP $prompt" (set_color normal) ' '
end

View file

@ -92,6 +92,9 @@
/// The name of the function that prints the fish right prompt (RPROMPT).
#define RIGHT_PROMPT_FUNCTION_NAME L"fish_right_prompt"
/// The name of the function to use in place of the left prompt if we're in the debugger context.
#define DEBUG_PROMPT_FUNCTION_NAME L"fish_breakpoint_prompt"
/// The name of the function for getting the input mode indicator.
#define MODE_PROMPT_FUNCTION_NAME L"fish_mode_prompt"
@ -2251,15 +2254,20 @@ static int read_i(void) {
while ((!data->end_loop) && (!sanity_check())) {
event_fire_generic(L"fish_prompt");
if (function_exists(LEFT_PROMPT_FUNCTION_NAME))
reader_set_left_prompt(LEFT_PROMPT_FUNCTION_NAME);
else
reader_set_left_prompt(DEFAULT_PROMPT);
if (function_exists(RIGHT_PROMPT_FUNCTION_NAME))
if (is_breakpoint && function_exists(DEBUG_PROMPT_FUNCTION_NAME)) {
reader_set_left_prompt(DEBUG_PROMPT_FUNCTION_NAME);
} else if (function_exists(LEFT_PROMPT_FUNCTION_NAME)) {
reader_set_left_prompt(LEFT_PROMPT_FUNCTION_NAME);
} else {
reader_set_left_prompt(DEFAULT_PROMPT);
}
if (function_exists(RIGHT_PROMPT_FUNCTION_NAME)) {
reader_set_right_prompt(RIGHT_PROMPT_FUNCTION_NAME);
else
} else {
reader_set_right_prompt(L"");
}
// Put buff in temporary string and clear buff, so that we can handle a call to
// reader_set_buffer during evaluation.
@ -2287,6 +2295,7 @@ static int read_i(void) {
}
}
}
reader_pop();
return 0;
}