-
linux console "window"
I'm writing a really simple console chat client for a chat server I wrote. Basicly, what I need is a line at the bottom for user input and the rest for text coming from the server. I tried something with ncurses, but it was a bit too complex for now.
The client I have written this far dumps everything in stdout and that's inconvenient for many reasons. It gets the input from a separate thread.
I'm grateful for any suggestions and ideas.
-
Practice using ncurses in another program which you can test your knowledge of ncurses, and experiment with new ideas.
Then when you know some stuff, put that to use in your chat program.
-
as well, you can look at apps like licq a console icq client.
see how they designed the interface, and how well it functions.
then get ideas from it for your own app.
-
If you don't use something like ncurses, you will have to do something like the following:
Code:
- Determine how many rows and columns the tty has
- Determine how auto-wrap works on this tty
- Determine how to position the cursor on this tty
- Put the tty into raw mode, no echo
- clear the screen, home the cursor
- save the cursor position as the output cursor
- move the cursor to the bottom line of the screen
- clear the input buffer
- clear the line and display a prompt
- move the cursor to the end of the prompt
- if a message is available to display then
- save the position of the cursor in the input area
- restore the position of the cursor in the output area
- display the new output, handling scrolling of all but the last line of the screen if it is needed
- save the position of the cursor in the output area
- restore the cursor position in the input area
- endif
- if a character has been entered then
- if that character is a newline then
- send the accumulated message from the input area to the chat server
- goto step #7
- else
- append the typed character to the input buffer
- display the entered character in the input area and advance the cursor
- endif
- endif
- goto step #11
I'm assuming that the chat server sends the message back to you as well as to other chatters, otherwise this is more complicated. Also, I'm not adding the complexity of command processing, dealing with input that overflows the width of the screen, editing (delete and stuff like that), and how to clean up when you're done (taking the tty out of raw mode, restoring scrolling regions, etc.)
Also, where I put "goto step #7", in code, you should just duplicate the stuff from steps #7 through #10 (actually, put it in a separate function and call it from both places.)
I've oversimplified the stuff above. Please believe me, you really don't want to do this from scratch.
-
I decided to go with ncurses with forms(forms.h). This brought me a new problem: I know how to set a field buffer and I know how to get one, but I have no clue about how to assign anything I write to the buffer. When I type something, it shows in the field at the bottom of the window, but anything beyond that is pretty much a mystery to me.
EDIT:
now I have come this far:
buffer=field(field[0],0);
this should, according to the man pages, set the "buffer" to whatever is written in field[0], but all I get is buffer filled with whitespaces with the length of what is written in field[0].