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.
This commit is contained in:
ridiculousfish 2012-05-19 16:59:56 -07:00
parent f1d22b6300
commit d09c9fba02
3 changed files with 22 additions and 16 deletions

View file

@ -1451,7 +1451,9 @@ static int builtin_functions( parser_t &parser, wchar_t **argv )
} }
/** The echo builtin. /** 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 ) 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++) if (! *argv++)
return STATUS_BUILTIN_ERROR; return STATUS_BUILTIN_ERROR;
/* Process -n */ /* Process options */
bool show_newline = true; bool print_newline = true, print_spaces = true;
if (*argv && ! wcscmp(*argv, L"-n")) { while (*argv) {
show_newline = false; if (! wcscmp(*argv, L"-n")) {
argv++; print_newline = false;
argv++;
} else if (! wcscmp(*argv, L"-s")) {
print_spaces = false;
argv++;
} else {
break;
}
} }
for (size_t idx = 0; argv[idx]; idx++) { for (size_t idx = 0; argv[idx]; idx++) {
if (idx > 0) if (print_spaces && idx > 0)
stdout_buffer.push_back(' '); stdout_buffer.push_back(' ');
stdout_buffer.append(argv[idx]); stdout_buffer.append(argv[idx]);
} }
if (show_newline) if (print_newline)
stdout_buffer.push_back('\n'); stdout_buffer.push_back('\n');
return STATUS_BUILTIN_OK; return STATUS_BUILTIN_OK;
} }

View file

@ -1,6 +1,3 @@
complete -c echo -s n --description "No newline" complete -c echo -s n --description "Do not output a newline"
complete -c echo -s e --description "Use backslash escaped characters" complete -c echo -s n --description "Do not separate arguments with spaces"
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 -u complete -c echo -u

View file

@ -25,7 +25,7 @@ function fish_prompt --description "Write out the prompt"
end end
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 '*' case '*'
@ -33,7 +33,7 @@ function fish_prompt --description "Write out the prompt"
set -g __fish_prompt_cwd (set_color $fish_color_cwd) set -g __fish_prompt_cwd (set_color $fish_color_cwd)
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" '> '
end end
end end