Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There are lots of extra spaces in terminal output (caused by PROMPT_CR) #127

Closed
mafredri opened this issue Jun 2, 2015 · 10 comments
Closed

Comments

@mafredri
Copy link
Collaborator

mafredri commented Jun 2, 2015

Defining cr in prompt_opts causes zsh to print a carriage return (basically #$COLUMN of spaces) after the last output line to make sure the prompt does not overwrite output in the terminal. Since pure uses a newline in it's pre-prompt this is a non-issue and the spaces are a nuisance when copying text from the terminal.

PROMPT_CR (+V)
Print a carriage return just before printing a prompt in the line editor.
See here

Sample with cr:

Sample without cr:

Between the two samples we see that if the output doesn't end with a newline as with echo -n then there's no breathing space between the output and the new prompt. In my opinion this is actually more correct behavior as it tells the user the output was missing a newline at the end.

I will try to run this for a while locally for a while and see if I spot any unwanted side-effects.

@sindresorhus
Copy link
Owner

I actually added because I was so annoyed that so many tools didn't print the trailing newline.

@mafredri
Copy link
Collaborator Author

mafredri commented Jun 2, 2015

Ah, I see. It should be possible to read the cursor position from the terminal though, if it's not the first character, print an extra newline.

@sindresorhus
Copy link
Owner

👍

@mafredri
Copy link
Collaborator Author

I found a solution to this, but I don't think I want to merge it as it requires toggling echo of the terminal (stty -echo, only required to prevent accidental output in tmux). It just feels a bit absurd that your zsh theme does this, not to mention potential repercussions (although I don't know of any).

diff --git a/pure.zsh b/pure.zsh
index f6fd34d..82ba2f4 100644
--- a/pure.zsh
+++ b/pure.zsh
@@ -98,6 +98,21 @@ prompt_pure_preprompt_render() {

    # if executing through precmd, do not perform fancy terminal editing
    if [[ "$1" == "precmd" ]]; then
+       {
+           local cursor_position
+           local IFS=";"
+           # stty -echo prevent output from being echoed to terminal
+           stty -echo
+           # printing \033[6n to the terminal outputs current cursor position
+           print -n "\033[6n"
+           # re-enable echo
+           stty echo
+           # read output from print command and prevent outputting to terminal
+           read -s -r -d "R" -A cursor_position
+           # if cursor position is not 1, then output is missing a newline at the end, print it
+           [[ "${cursor_position[2]}" != "1" ]] && print
+       }
+
        print -P "\n${prompt}"
    else
        # only redraw if prompt has changed
@@ -243,7 +258,7 @@ prompt_pure_setup() {
    # if output doesn't end with a newline
    export PROMPT_EOL_MARK=''

-   prompt_opts=(cr subst percent)
+   prompt_opts=(subst percent)

    zmodload zsh/datetime
    autoload -Uz add-zsh-hook

@sindresorhus
Copy link
Owner

Yeah, looks too hacky. Gonna hit us at some point. I guess it would be better to just remove cr then...

@forivall
Copy link
Contributor

I remember when I first switched to zsh and used other prompts, that ^M would be printed if the command didn't end with a newline nvm that's PROMPT_EOL_MARK. Can something smart be done with that?

@mafredri
Copy link
Collaborator Author

Or we could just close this issue because this can easily be achieved by setting

unsetopt prompt_cr

after

prompt pure

zmwangx added a commit to zmwangx/prezto that referenced this issue Jul 13, 2015
PROMPT_CR leaves too many extra spaces. Now that I have a blank line
separating the preprompt and the output of the previous command, I can
afford to throw out PROMPT_CR.

Added bonus: we also have a clear visual distinction of commands that
print a trailing newline and commands that do not (bad citizens, except
pbpaste).

See also sindresorhus/pure#127.
@mafredri
Copy link
Collaborator Author

@forivall sorry for not responding to your comment. Regarding PROMPT_EOL_MARK nothing smart can be done with it AFAIK, it doesn't get applied without cr and you can't e.g. put newlines in it :(.

@sindresorhus I'll leave it up to you to decide if we remove cr or leave it up to the user to use unsetopt prompt_cr.

@zmwangx
Copy link
Contributor

zmwangx commented Jul 15, 2015

Regarding PROMPT_EOL_MARK nothing smart can be done with it AFAIK, it doesn't get applied without cr and you can't e.g. put newlines in it :(.

I totally hate it. Probably worth submitting a patch to Zsh.

@sindresorhus
Copy link
Owner

Probably worth submitting a patch to Zsh.

👍 Or even just opening an issue over there ;)

I'll leave it up to you to decide if we remove cr

Remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants