C에서 터미널 너비를 얻습니까?
내 C 프로그램 내에서 터미널 너비를 얻는 방법을 찾고있었습니다. 내가 계속 생각하는 것은 다음과 같은 내용입니다.
#include <sys/ioctl.h>
#include <stdio.h>
int main (void)
{
struct ttysize ts;
ioctl(0, TIOCGSIZE, &ts);
printf ("lines %d\n", ts.ts_lines);
printf ("columns %d\n", ts.ts_cols);
}
하지만 내가 시도 할 때마다
austin@:~$ gcc test.c -o test
test.c: In function ‘main’:
test.c:6: error: storage size of ‘ts’ isn’t known
test.c:7: error: ‘TIOCGSIZE’ undeclared (first use in this function)
test.c:7: error: (Each undeclared identifier is reported only once
test.c:7: error: for each function it appears in.)
이것이 가장 좋은 방법입니까, 아니면 더 나은 방법이 있습니까? 그렇지 않다면 어떻게 작동시킬 수 있습니까?
편집 : 고정 코드는
#include <sys/ioctl.h>
#include <stdio.h>
int main (void)
{
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
printf ("lines %d\n", w.ws_row);
printf ("columns %d\n", w.ws_col);
return 0;
}
getenv () 사용을 고려해 보셨습니까 ? 터미널 열과 행을 포함하는 시스템의 환경 변수를 가져올 수 있습니다.
또는 방법을 사용하여 커널이 터미널 크기로 보는 것을 확인하려면 (터미널 크기가 조정 된 경우 더 좋음) 다음과 같이 TIOCGSIZE가 아닌 TIOCGWINSZ를 사용해야합니다.
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
그리고 전체 코드 :
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
int main (int argc, char **argv)
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
printf ("lines %d\n", w.ws_row);
printf ("columns %d\n", w.ws_col);
return 0; // make sure your main returns int
}
This example is a bit on the lengthy side, but I believe it's the most portable way of detecting the terminal dimensions. This also handles resize events.
As tim and rlbond suggests, I'm using ncurses. It guarantees a great improvement in terminal compatability as compared to reading environment variables directly.
#include <ncurses.h>
#include <string.h>
#include <signal.h>
// SIGWINCH is called when the window is resized.
void handle_winch(int sig){
signal(SIGWINCH, SIG_IGN);
// Reinitialize the window to update data structures.
endwin();
initscr();
refresh();
clear();
char tmp[128];
sprintf(tmp, "%dx%d", COLS, LINES);
// Approximate the center
int x = COLS / 2 - strlen(tmp) / 2;
int y = LINES / 2 - 1;
mvaddstr(y, x, tmp);
refresh();
signal(SIGWINCH, handle_winch);
}
int main(int argc, char *argv[]){
initscr();
// COLS/LINES are now set
signal(SIGWINCH, handle_winch);
while(getch() != 27){
/* Nada */
}
endwin();
return(0);
}
#include <stdio.h>
#include <stdlib.h>
#include <termcap.h>
#include <error.h>
static char termbuf[2048];
int main(void)
{
char *termtype = getenv("TERM");
if (tgetent(termbuf, termtype) < 0) {
error(EXIT_FAILURE, 0, "Could not access the termcap data base.\n");
}
int lines = tgetnum("li");
int columns = tgetnum("co");
printf("lines = %d; columns = %d.\n", lines, columns);
return 0;
}
Needs to be compiled with -ltermcap
. There is a lot of other useful information you can get using termcap. Check the termcap manual using info termcap
for more details.
If you have ncurses installed and are using it, you can use getmaxyx()
to find the dimensions of the terminal.
Assuming you are on Linux, I think you want to use the ncurses library instead. I am pretty sure the ttysize stuff you have is not in stdlib.
So not suggesting an answer here, but:
linux-pc:~/scratch$ echo $LINES
49
linux-pc:~/scratch$ printenv | grep LINES
linux-pc:~/scratch$
Ok, and I notice that if I resize the GNOME terminal, the LINES and COLUMNS variables follow that.
Kinda seems like GNOME terminal is creating these environment variables itself?
Here are the function calls for the already suggested environmental variable thing:
int lines = atoi(getenv("LINES"));
int columns = atoi(getenv("COLUMNS"));
참고URL : https://stackoverflow.com/questions/1022957/getting-terminal-width-in-c
'programing tip' 카테고리의 다른 글
Rails 4.2 개발 서버의 기본 바인딩 IP를 변경하는 방법은 무엇입니까? (0) | 2020.09.19 |
---|---|
미디어 쿼리에서 작동하지 않는 CSS 네이티브 변수 (0) | 2020.09.19 |
EnableEurekaClient와 EnableDiscoveryClient의 차이점은 무엇입니까? (0) | 2020.09.19 |
Java에서 지역 변수가 스레드로부터 안전한 이유 (0) | 2020.09.19 |
android studio ctrl + space는 문서 창을 엽니 다. (0) | 2020.09.18 |