Hacker News new | ask | show | jobs
by freshbreath 5165 days 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.
4 comments

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.
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.
Cool! Do you mind posting your PS1 so other people can try out your config?
Here's the PS1:

  PS1="\[\e[32;1m\]\[\e[37;1m\]\`if [ \$? = 0 ]; then echo \[\e[33m\] :\)\[\e[0m\]; else echo \[\e[31m\] X\(\[\e[0m\]; fi\`\[\e[32;1m\] \[\e[32;1m\](\[\e[37;1m\]\h:\u\[\e[32;1m\])-(\[\e[37;1m\]\j\[\e[32;1m\])-(\[\e[37;1m\]\w\[\e[32;1m\])\n\[\e[0m\]"
Here's how it appears:

   :) (seas456:narayana)-(0)-(~/personal)
  [this line is where your cursor blinks; notice no symbol at the start of this line]
You can't see the colors here. The smiley face turns into a bright red frowny face if the exit code of the last job is nonzero.
It looks like the your markup got mangled a little. Would you mind posting a gist?
Could you share your prompt?
Zsh handles this case pretty well. If there's no newline when a process exits, it prints a color-inverted % and a newline.
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...

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.

That solution sounds even more hacky. Just like I expected resizing my iterm2 window causes glitches because it's text reflowing. I love that feature, but I realize this isn't a problem for all terminals.
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....

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