From fb7646fac7d2b0e17a805e7792655b6ef3c3b18c Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Mon, 14 Dec 2020 10:20:16 +0000 Subject: [PATCH] add tty_fd --- display_bar.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/display_bar.c b/display_bar.c index 36ba7ef..0771715 100644 --- a/display_bar.c +++ b/display_bar.c @@ -40,6 +40,11 @@ void ready_progress_bar(){ tty_fd = open(buf,O_RDONLY); } } +void quit_progress_bar(){ + if(tty_fd != STDIN_FILENO){ + close(tty_fd); + } +} void lock_scrolled(){ #ifdef DEBUG @@ -72,7 +77,6 @@ raw_progress_bar_t create_raw_progress_bar(){ unlock_scrolled(); return ret; } - void RawDisplayProgressBar(raw_progress_bar_t bar,size_t offset,size_t total,const char * filename){ char buf[PROGRESS_BAR_WIDTH]; size_t i; @@ -96,8 +100,8 @@ void RawDisplayProgressBar(raw_progress_bar_t bar,size_t offset,size_t total,con return; }//optimization. //if ioctl failed? what should i do... - ioctl(STDIN_FILENO,TIOCGWINSZ,(char *)&wnd_size); - if (wnd_size.ws_row < pos){ + ioctl(tty_fd,TIOCGWINSZ,(char *)&wnd_size); + if (wnd_size.ws_row < pos){//render nothing if render position is out of window size. unlock_scrolled(); return; } @@ -105,6 +109,7 @@ void RawDisplayProgressBar(raw_progress_bar_t bar,size_t offset,size_t total,con fflush(stdout); unlock_scrolled(); } + bool isatty_file(FILE * file){ return isatty(fileno(file)); } @@ -121,6 +126,18 @@ void myd_perror(const char * msg){ } } +int myd_vfprintf(int line,FILE * f,const char * msg, va_list arg){ + if(isatty_file(f)){ + lock_scrolled(); + add_scrolled_unlocked(line); + vfprintf(f,msg,arg); + unlock_scrolled(); + } + else{ + vfprintf(f,msg,arg); + } +} + void init_progress_bar(progress_bar_t * bar,int update_rate){ memset(bar,0,sizeof(*bar)); bar->bar = create_raw_progress_bar(); @@ -141,9 +158,3 @@ void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const ch bar->last_update = ts; RawDisplayProgressBar(bar->bar,offset,total,filename); } - -void quit_progress_bar(){ - if(tty_fd != STDIN_FILENO){ - close(tty_fd); - } -} \ No newline at end of file