rmyorston / busybox-w32

WIN32 native port of BusyBox.
https://frippery.org/busybox
Other
670 stars 124 forks source link

when `cat` file without newline at end, shell does not wrap #404

Closed smalltalkman closed 4 months ago

smalltalkman commented 4 months ago

when cat file without newline at end, shell does not wrap.

C:/Users/lenovo # cd ~
~ #
~ # cat test.txt
line1
line2
line3~ # ls -l
total 4
-rw-rw-r--    1 root     root            19 Apr 08 16:34 test.txt
~ #
rmyorston commented 4 months ago

I think that's the normal behaviour, isn't it?

bash, dash and ksh on Linux all do the same thing.

smalltalkman commented 4 months ago

I think that's the normal behaviour, isn't it?

bash, dash and ksh on Linux all do the same thing.

Ah yes, I overlooked that. I should compare with the linux results, not with msys2.

smalltalkman commented 4 months ago

Oh, maybe that's the real question:

D:\gym\softwares\busybox>busybox_pre64.exe sh -l
C:/Users/lenovo #
C:/Users/lenovo # cd ~
~ #
~ # cat test.txt
line1
line2
line3~ # ls -l
total 4
-rw-rw-r--    1 root     root            17 Apr 08 17:28 test.txt
~ #
~ # cat test.txt
line1
line2
~ # cat test.txt

When I use the up and down arrows to turn pages after executing the cat test.txt command for the second time, the last line disappears.

rmyorston commented 4 months ago

Yes, the BusyBox line editing code isn't as clever as that used by other shells. This isn't unique to busybox-w32, the same problem is apparent in upstream BusyBox on Linux.

avih commented 4 months ago

As a workaround, and typically also convenient regardless, you could change the shell prompt to begin with a newline, so that it normally follows an empty line (this makes it easier to identify where a command ends and the next prompt starts), and, in the case where the program doesn't print a final newline, like your cat test, at least the prompt begins at the leftmost of the screen:

export PS1='\n\w \$ '

Or, if you feel bold, 3-lines prompt, maybe somethig like this (newline, bold yellow working directry, blue time, reset color, newline, prompt):

export PS1='\n\e[1;33m\w \e[34m\t\e[m\n\$ '

Check the bash manual on what you can put in PS1 (and other prompts), and this page for terminal colors.

smalltalkman commented 4 months ago

@avih Thank you so much! It works very well.

export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '
avih commented 4 months ago

You're welcome.