top | item 3899626

(no title)

freshbreath | 14 years ago

What if a program prints text without a newline at the end, then exits? It's possible and it would be confusing to not see that last line.

    cout >> "Hello world"; // with no endl.
Prepending the prompt with a new line might be a better idea than over-writing the current line.

discuss

order

arjunnarayan|14 years ago

Yes. I have a two line bash prompt: the first line with a whole bunch of info like the exit status of the last command, the number of background jobs, the hostname, current username (very useful when you have 5 different ssh sessions to different machines), and the current working directory. In real world usage I have experienced almost no loss of utility from only having 50% of my command history on screen. In fact, given how many commands are just ls,whoami,jobs,pwd, that info gets condensed into one line.

pavel_lishin|14 years ago

Mine's very similar, and includes the current date and time; it also wraps onto the next line and starts with a # (mostly because I'm an idiot, and have burned myself by copying-and-pasting an entire-line-plus-newline and ran a command instead of just copying it in order to edit it.)

    [\[\033[1;32m\]\D{%Y-%m-%d %H:%M:%S}\[\033[0m\]] \[\033[1;32m\]\u\[\033[0m\]\[\033[1;32m\]@\h\[\033[0m\]:\[\033[1;36m\]\w\[\033[0m\]\n#
It's actually dynamically generated by my .bashrc so that different servers have different colors; at a glance, I can generally tell whether I'm on one of my machines, or on a work build server, or an amazon ec2 instance.

TimothyFitz|14 years ago

Cool! Do you mind posting your PS1 so other people can try out your config?

india|14 years ago

Could you share your prompt?

AngryParsley|14 years ago

Zsh handles this case pretty well. If there's no newline when a process exits, it prints a color-inverted % and a newline.

air-lemming|14 years ago

My prompt puts a newline if the cursor is not in the first column, but getting the cursor position complicates things.

https://github.com/dylanahsmith/dotfiles/blob/master/.config...

keithwinstein|14 years ago

zsh's approach is clever and doesn't require bidirectional communication. They always print their inverted "%" and then they print as many spaces as you have columns in the terminal (minus one). If the last process ended with a CRLF, they will now be in the last column of the same line; otherwise they will have wrapped around to a new line (assuming the terminal is in auto wrap mode, which is the default). Then they print a carriage return and the prompt as normal.

This does break if the terminal has auto wrap turned off (echo -e "\033[?7l"), but otherwise it's very clever.

adavies42|14 years ago

looks interesting, but it doesn't seem to be compatible with either my setup in general (bash 3.2.25 inside screen inside PuTTY) or my current prompt, which comes out of a shell function and embeds the number of files in pwd, username@hostname:pwd, and previous status, with coloring applied to various parts.

specifically your function just sort of sits there doing nothing when i try it....

statico|14 years ago

Agreed. This is the reason I tried this tip many years ago and removed it.