Terminal Width (10)

1 Name: #!/usr/bin/anonymous : 2007-09-02 02:12 ID:ifQhpJ2E

Hey, I've built a toy hex file reader on the terminal, however it's using always 80 cols because I haven't found a way to get terminal width in c in linux... please someone help me (just tell me some function or library, I looked on termios' man page but found no useful info)

2 Name: dmpk2k!hinhT6kz2E : 2007-09-02 05:14 ID:Heaven

I don't know the answer.

It sounds like you're going to reinvent the wheel though. May I suggest using ncurses? Handling terminals manually is hell; don't go there.

3 Name: #!/usr/bin/anonymous : 2007-09-02 12:15 ID:MMWcilih

It's in the environment variable COLUMNS, read that out with whatever your language provides for that.

4 Name: #!/usr/bin/anonymous : 2007-09-02 16:24 ID:ifQhpJ2E

>>2
well, it's just a bunch of printfs so using ncurses is kind of overkill
>>3
Thanks, I'll lool into that =)

5 Name: #!/usr/bin/anonymous : 2007-09-02 20:31 ID:Heaven

>>4
When you're doing terminal handling, using ncurses will eventually pay off. Generally when the alternative would turn out to be having a minor nervous breakdown.

Also, it's good practice which I'm assuming you're after here anyway (given that xxd piped to less or whatever generally does the trick as far as viewing is concerned). Ncurses doesn't exactly have the most obvious API around.

Anyway... you can't actually rely on the COLUMNS variable existing. Oh god, terminals in Unix-workalikes... just implementing your own terminal emulator is an exercise in bonking yourself in a very uncomfortable place.

6 Name: #!/usr/bin/anonymous : 2007-09-02 22:23 ID:Heaven

>>2,5
You guys sound like Linux programmers. :( Come on, not every Unix is that bad, and not every programmer is as confused and despairing as you.

(I'll just preemptively add that I'm wrong, so nobody feels a need to argue with or respond to this.)

7 Name: #!/usr/bin/anonymous : 2007-09-03 12:42 ID:Heaven

>>1
once you've got it detecting the width, what are you going to do when your output is to a pipe or file redirection?

if it's interactive, use ncurses; if it's not, just pick a line with that makes sense and let other more suitable utils handle paging and wrapping if the user desires it.

8 Name: #!/usr/bin/anonymous : 2007-09-03 12:43 ID:Heaven

pick a line width, too

9 Name: #!/usr/bin/anonymous : 2007-09-03 23:36 ID:Heaven

>>6
Yeah. For some reason, outputting terminal control codes directly from an application (i.e. not through some library) is considered dirty by younger Unix types. IMO this has nothing to do with Linux though; xterm is xterm regardless of particulars.

I guess most of us took a look at what's inside termcap and lost a few points of sanity to the ghosts of perverse old VT100 variant terminals that perhaps someone was still using in the mid-eighties.

10 Name: #!/usr/bin/anonymous : 2007-09-04 12:46 ID:Heaven

>>9

> I guess most of us took a look at what's inside termcap and lost a few points of sanity to the ghosts of perverse old VT100 variant terminals that perhaps someone was still using in the mid-eighties.

hehe :)

This thread has been closed. You cannot post in this thread any longer.