From d09c9fba02b09f7de231a6c4291f18d062d8382f Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 19 May 2012 16:59:56 -0700 Subject: [PATCH] Added a -s option to echo to mean "don't output spaces" Replaced default prompt to stop calling printf. I opened a bug to replace the default prompt entirely with one that's cheaper. --- builtin.cpp | 27 ++++++++++++++++++--------- share/completions/echo.fish | 7 ++----- share/functions/fish_prompt.fish | 4 ++-- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index d26cefb0a..2cfec0470 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -1451,7 +1451,9 @@ static int builtin_functions( parser_t &parser, wchar_t **argv ) } /** The echo builtin. - bash only respects -n if it's the first argument. We'll do the same. */ + bash only respects -n if it's the first argument. We'll do the same. + We also support a new option -s to mean "no spaces" +*/ static int builtin_echo( parser_t &parser, wchar_t **argv ) { @@ -1459,19 +1461,26 @@ static int builtin_echo( parser_t &parser, wchar_t **argv ) if (! *argv++) return STATUS_BUILTIN_ERROR; - /* Process -n */ - bool show_newline = true; - if (*argv && ! wcscmp(*argv, L"-n")) { - show_newline = false; - argv++; + /* Process options */ + bool print_newline = true, print_spaces = true; + while (*argv) { + if (! wcscmp(*argv, L"-n")) { + print_newline = false; + argv++; + } else if (! wcscmp(*argv, L"-s")) { + print_spaces = false; + argv++; + } else { + break; + } } - + for (size_t idx = 0; argv[idx]; idx++) { - if (idx > 0) + if (print_spaces && idx > 0) stdout_buffer.push_back(' '); stdout_buffer.append(argv[idx]); } - if (show_newline) + if (print_newline) stdout_buffer.push_back('\n'); return STATUS_BUILTIN_OK; } diff --git a/share/completions/echo.fish b/share/completions/echo.fish index 8a187a5ff..c61df7a7d 100644 --- a/share/completions/echo.fish +++ b/share/completions/echo.fish @@ -1,6 +1,3 @@ -complete -c echo -s n --description "No newline" -complete -c echo -s e --description "Use backslash escaped characters" -complete -c echo -s E --description "Do not use backslash escaped characters" -complete -c echo -l help --description "Display help and exit" -complete -c echo -l version --description "Display version and exit" +complete -c echo -s n --description "Do not output a newline" +complete -c echo -s n --description "Do not separate arguments with spaces" complete -c echo -u diff --git a/share/functions/fish_prompt.fish b/share/functions/fish_prompt.fish index 089517347..f4aab3442 100644 --- a/share/functions/fish_prompt.fish +++ b/share/functions/fish_prompt.fish @@ -25,7 +25,7 @@ function fish_prompt --description "Write out the prompt" end end - printf '%s@%s %s%s%s# ' $USER $__fish_prompt_hostname "$__fish_prompt_cwd" (prompt_pwd) "$__fish_prompt_normal" + echo -n -s "$USER" @ "$__fish_prompt_hostname" ' ' "$__fish_prompt_cwd" (prompt_pwd) "$__fish_prompt_normal" '# ' case '*' @@ -33,7 +33,7 @@ function fish_prompt --description "Write out the prompt" set -g __fish_prompt_cwd (set_color $fish_color_cwd) end - printf '%s@%s %s%s%s> ' $USER $__fish_prompt_hostname "$__fish_prompt_cwd" (prompt_pwd) "$__fish_prompt_normal" + echo -n -s "$USER" @ "$__fish_prompt_hostname" ' ' "$__fish_prompt_cwd" (prompt_pwd) "$__fish_prompt_normal" '> ' end end