Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a scrollbar on Ncurses or make it like "more"

Tags:

c++

c

linux

ncurses

Basically I am writing a client program which receives response and logs from server, the client is also able to send request to server for different information. I used curses and output looks pretty good. It looks like VI, output at the top and user on Client end enter command at the bottom. Only thing is I am not able to scroll back.. My boss told me to make it like "more command in linux" and I want to stick with my solution and add a scroll bar on the side for output window... I was thinking Server sends logs randomly and it's nearly impossible (or too hard) to make it look like more...

like image 848
Shawn Avatar asked Jul 07 '11 21:07

Shawn


2 Answers

If you maintain a list or array of lines in your client and ask ncurses to paint a range of lines as a sliding window, you can slide your window up and down in response to ^F ^B ^U ^D ^Y ^E commands, which just repaints the screen with different indexes.

I would skip trying to draw a scrollbar though: It would look out of place on a Linux system. Not even mc has scrollbars. Just show a content summary in the bottom line, similar to vim's Top, Bot, All, N% when :set ruler is turned on, that'll feel most at home.

like image 84
sarnold Avatar answered Sep 19 '22 00:09

sarnold


I'm not entirely sure if you are asking about how to implement scrollback or how to draw a scrollbar with ncurses. My guess is the second.

Assuming your ncurses is compiled with Unicode support (remember to set the environment correctly when initializing ncurses, look into "setlang"), you can use following characters:

Unicode:
▲ - U+25B2 BLACK UP-POINTING TRIANGLE
▼ - U+25BC BLACK DOWN-POINTING TRIANGLE
▮ - U+25AE BLACK VERTICAL RECTANGLE

ASCII:
176 - ░ Light shaded block
177 - ▒ Medium shaded block
178 - ▓ Dark shaded block
219 - █ Block block

Writing the code to display the dark block at the right place should be quite straightforward.

If I understood your question wrong, my excuses.

like image 28
AVH Avatar answered Sep 21 '22 00:09

AVH