add tty_fd

This commit is contained in:
ubuntu201711081 2020-12-14 10:20:16 +00:00
parent f2e8bf2376
commit fb7646fac7

View File

@ -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);
}
}