From df01547eabcfb875910073f4b156e1d54fdcda80 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Mon, 19 Jun 2017 22:57:32 -0700 Subject: [PATCH] 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. --- doc_src/fish_breakpoint_prompt.txt | 30 +++++++++++++++++++++ share/functions/fish_breakpoint_prompt.fish | 23 ++++++++++++++++ src/reader.cpp | 21 ++++++++++----- 3 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 doc_src/fish_breakpoint_prompt.txt create mode 100644 share/functions/fish_breakpoint_prompt.fish diff --git a/doc_src/fish_breakpoint_prompt.txt b/doc_src/fish_breakpoint_prompt.txt new file mode 100644 index 000000000..b56c7e0ac --- /dev/null +++ b/doc_src/fish_breakpoint_prompt.txt @@ -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 $status 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 diff --git a/share/functions/fish_breakpoint_prompt.fish b/share/functions/fish_breakpoint_prompt.fish new file mode 100644 index 000000000..b41f6e095 --- /dev/null +++ b/share/functions/fish_breakpoint_prompt.fish @@ -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 diff --git a/src/reader.cpp b/src/reader.cpp index bbd43b0ba..380dcc174 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -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; }