How on earth can zsh know whether it's starting at the beginning of a line or not?


 
Thread Tools Search this Thread
Top Forums Programming How on earth can zsh know whether it's starting at the beginning of a line or not?
# 1  
Old 04-15-2010
How on earth can zsh know whether it's starting at the beginning of a line or not?

Hi everyone,
I'm currently developing my own shell and I've been wondering how this awesome "printing a reverse-video percent character and a newline when the prompt doesn't start at the beginning of a line" feature is implemented in zsh.

Although it is obvious what is needed to implement this (a way to figure out whether you are at the beginning of a line or not), how to get it is much more complicated. Very weirdly, there seems to exist no way to know the cursor position in a terminal without clearing it and keeping track of the cursor, which a shell obviously cannot do since it can't control the output of the programs it launches (that is why my theory that zsh uses ncurses doesn't seem to hold, although it is very possible that it uses some ncurses functions).

I checked the zsh source code but I'm not used to doing such things, and besides, it's quite like looking for a needle in a haystack (even though I'm fairly confident the code is situated in the zle part)...

I although figured there might be a way to check for the beginning of the line indirectly, I didn't find any yet, so would anyone be able to help me find how zsh does this, or a way to get similar results?

Thanks a lot in advance.
# 2  
Old 04-16-2010
Quote:
Originally Posted by AdrienE
Very weirdly, there seems to exist no way to know the cursor position in a terminal without clearing it and keeping track of the cursor, which a shell obviously cannot do since it can't control the output of the programs it launches (that is why my theory that zsh uses ncurses doesn't seem to hold, although it is very possible that it uses some ncurses functions).
Really? I thought this is what the ^[[6n escape sequence was for? Besides:
Code:
$ which zsh
/usr/bin/zsh
$ ldd -r $( which zsh )
        linux-gate.so.1 =>  (0xffffe000)
        libcap.so.1 => /lib/libcap.so.1 (0xb7f6b000)
        libdl.so.2 => /lib/libdl.so.2 (0xb7f67000)
        libnsl.so.1 => /lib/libnsl.so.1 (0xb7f50000)
        libncurses.so.5 => /lib/libncurses.so.5 (0xb7f1b000)
        libm.so.6 => /lib/libm.so.6 (0xb7ef6000)
        libc.so.6 => /lib/libc.so.6 (0xb7dc3000)
        /lib/ld-linux.so.2 (0xb7f89000)
$ zsh --version
zsh 4.3.4 (i686-suse-linux-gnu)

Quote:
Originally Posted by AdrienE
I checked the zsh source code but I'm not used to doing such things, and besides, it's quite like looking for a needle in a haystack (even though I'm fairly confident the code is situated in the zle part)...
That makes it sound like you just started programming, and are thinking that writing your own shell can't be that hard, since there are so many around. Well, the source code for KSH93 (which has far less features that zsh) comes in at roughly 2.5 MiB of code, and that's not counting any supporting libraries from the same archive.
# 3  
Old 04-16-2010
Quote:
Originally Posted by pludi
Really? I thought this is what the ^[[6n escape sequence was for?
Thanks, that does the trick (in a very ugly way though). No need to be ironical about it though, it's not as if hardcoded escape sequences were that much of an obvious solution (although it is true I could have found it myself, which I did, actually, a few hours after creating this thread).
Quote:
Originally Posted by pludi
Besides:
Code:
$ which zsh
/usr/bin/zsh
$ ldd -r $( which zsh )
        linux-gate.so.1 =>  (0xffffe000)
        libcap.so.1 => /lib/libcap.so.1 (0xb7f6b000)
        libdl.so.2 => /lib/libdl.so.2 (0xb7f67000)
        libnsl.so.1 => /lib/libnsl.so.1 (0xb7f50000)
        libncurses.so.5 => /lib/libncurses.so.5 (0xb7f1b000)
        libm.so.6 => /lib/libm.so.6 (0xb7ef6000)
        libc.so.6 => /lib/libc.so.6 (0xb7dc3000)
        /lib/ld-linux.so.2 (0xb7f89000)
$ zsh --version
zsh 4.3.4 (i686-suse-linux-gnu)

I did say zsh possibly used ncurses functions ("although it is very possible that it uses some ncurses functions"). It has, in fact, a ncurses module. But that doesn't mean it uses ncurses to get the cursor position. It can't do this because ncurses knows it by clearing the screen an keeping track of it, which is incompatible with launching external programs easily.
Quote:
Originally Posted by pludi
That makes it sound like you just started programming, and are thinking that writing your own shell can't be that hard, since there are so many around.
Well I'm sorry if it sounds like that but that's just not true.

As a matter of fact, I found a better solution to this problem than ^[[6n. The end is not to find the cursor position, this is the mean. The real purpose here is to be able to stay on the current line if we're at the beginning of it or print a % and get to the next line otherwise. To achieve this I thought the cursor position was needed but it's not.
You can get the same result by always printing a %, and number_of_columns_in_the_terminal - 1 spaces, then a carriage return and the 'ce' termcap. This is in fact what my version of zsh seems to do (I tried selecting the end of the last line to check for spaces).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

2. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

3. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

4. Shell Programming and Scripting

beginning less from line #

Hi from a script i want to to read a file beginning at line e.g. number 21 to the EOF. less +n21 temp.txt Bevor the result, it brings an empty page, so that i cant use for scripting. Any idea how the problem can be solved? Thanks in advance! IMPe (2 Replies)
Discussion started by: IMPe
2 Replies

5. Shell Programming and Scripting

comment a line of the patterns is a the beginning of the line

I need to comment the lines starting with pattern "exclude" or "exclude=". If the work exclude comes at any other part, ignore it. Also, ignore, excludes, excluded etc. Ie only comment the line starting with exclude. File contents. exclude exclude= hi I am excluded excludes excludes= ... (9 Replies)
Discussion started by: anil510
9 Replies

6. UNIX for Dummies Questions & Answers

vim copy line and paste at the beginning, middle, and end of another line

How would you do vim copy line and paste at the beginning, middle, and end of another line. I know yy copies the whole line and p pastes the whole line, but on its own separate line. Sometimes I would like to copy a line to the beginning, middle, or end of another line. I would think this would be... (3 Replies)
Discussion started by: cokedude
3 Replies

7. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

8. Shell Programming and Scripting

Space at beginning of the line

How can I delete spaces at the begining of all lines of my file ? (2 Replies)
Discussion started by: Sara_84
2 Replies

9. Shell Programming and Scripting

how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :- <bag name="mybag1" version="1.0"/> <contents id="coins"/> <bag name="mybag2" version="1.1"/> <contents id="clothes"/> <contents id="shoes"/> <bag name="mybag3" version="1.6"/> I want to delete line containing mybag2 and its subsequent... (5 Replies)
Discussion started by: repudi8or
5 Replies

10. Shell Programming and Scripting

Unix Script with line number at beginning of each line.

Could anybody help me. I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line. Thanks. (5 Replies)
Discussion started by: mascorro
5 Replies
Login or Register to Ask a Question