1 /* 2 * Copyright (C) 2001, 2002, and 2003 Roy Keene 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 2 7 * of the License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 * email: dact@rkeene.org 19 */ 20 21 /* 22 * Default DACT UI, this will probably be made modulizable. 23 * 24 * -- Roy Keene <rkeene@RKeene.org> 25 * 26 */ 27 28 #include "dact.h" 29 #include "ui.h" 30 #ifdef HAVE_STDLIB_H 31 #include <stdlib.h> 32 #endif 33 #ifdef HAVE_STRING_H 34 #include <string.h> 35 #endif 36 #ifdef HAVE_UNISTD_H 37 #include <unistd.h> 38 #endif 39 #include <stdio.h> 40 41 char dact_ui_statusvar[128]; 42 43 void dact_ui_update(void) { 44 static int drawing=0; 45 float done; 46 char *bar1, *bar2; 47 int width=80,percent; 48 49 if (!dact_ui_getopt(DACT_UI_OPT_LEVEL)) return; /* LEVEL 0 is Quiet */ 50 percent=dact_ui_getopt(DACT_UI_OPT_PERCENT); 51 52 if (getenv("COLUMNS")!=NULL) width=atoi(getenv("COLUMNS")); 53 if (width<10) return; 54 width=(width>30?10:5); 55 56 if (percent>100) percent=100; 57 if (percent>-1) { 58 done=(((float) width)*(((float) percent)/100)); 59 60 bar1=malloc((int) done+2); 61 bar2=malloc((int) (width-done)+3); 62 memset(bar1,'#',(int) done); 63 memset(bar2,'.',(int) (width-done+0.9999999)); 64 bar1[(int) done]=0; 65 bar2[(int) (width-done+0.9999999)]=0; 66 } else { 67 bar2=malloc(width+1); 68 memset(bar2,'?',width); 69 bar2[width]=0; 70 bar1=bar2+width; 71 percent=0; 72 } 73 if (dact_ui_getopt(DACT_UI_OPT_COLOR)) { 74 fprintf(stderr, "=> \033[1;30m[\033[1;32m%s\033[1;37m%s\033[1;30m] \033[1;37m%03i\033[0;31m%%\033[0m",bar1,bar2,percent); 75 } else { 76 fprintf(stderr, "=> [%s%s] %03i%%",bar1,bar2,percent); 77 } 78 fprintf(stderr, " [%c] | Status: %s\033[K\r",*("|/-\\"+(drawing&3)),dact_ui_statusvar); 79 fflush(stderr); 80 81 free(bar2); 82 if (bar1!=(bar2+width)) free(bar1); 83 84 drawing++; 85 return; 86 } 87 88 void dact_ui_init(void /*for now*/) { 89 dact_ui_setopt(DACT_UI_OPT_COLOR, 0); 90 dact_ui_status(0, "Initialized."); 91 } 92 93 void dact_ui_deinit(void) { 94 if (dact_ui_getopt(DACT_UI_OPT_LEVEL)) fprintf(stderr, "\n"); 95 } 96 97 void dact_ui_percentdone(int percent) { 98 dact_ui_setopt(DACT_UI_OPT_PERCENT,percent); 99 dact_ui_update(); 100 } 101 102 void dact_ui_incrblkcnt(int n) { 103 static uint32_t blkcnt=0; 104 uint32_t blocks; 105 106 if (n==0) blkcnt=0; 107 108 blkcnt+=n; 109 blocks=dact_ui_getopt(DACT_UI_OPT_FILEBLKS); 110 if (blocks==0) { 111 dact_ui_percentdone(-1); 112 return; 113 } 114 dact_ui_percentdone((int) ((float) (((float) blkcnt/(float) blocks)*100))); 115 } 116 117 int32_t dact_ui_optmanip(int action, int opt, int32_t val) { 118 static int opts[10]={0,0,0,0,0,0,0,0,0}; 119 120 if (opt>=(sizeof(opts)/sizeof(int))) return(-1); 121 122 switch (action) { 123 case DACT_UI_MANIP_GET: 124 return(opts[opt]); 125 break; 126 case DACT_UI_MANIP_SET: 127 return(opts[opt]=val); 128 break; 129 } 130 return(-1); 131 } 132 133 int32_t dact_ui_getopt(int opt) { 134 return(dact_ui_optmanip(DACT_UI_MANIP_GET, opt, 0)); 135 } 136 137 int dact_ui_setopt(int opt, int32_t val) { 138 return(dact_ui_optmanip(DACT_UI_MANIP_SET, opt, val)); 139 } 140 141 void dact_ui_status(int level, const char *status) { 142 if (level>dact_ui_getopt(DACT_UI_OPT_LEVEL)) return; 143 strncpy(dact_ui_statusvar,status,sizeof(dact_ui_statusvar)-1); 144 dact_ui_update(); 145 } 146 147 void dact_ui_status_append(int level, const char *status) { 148 if (level>dact_ui_getopt(DACT_UI_OPT_LEVEL)) return; 149 strncat(dact_ui_statusvar,status,sizeof(dact_ui_statusvar)-strlen(dact_ui_statusvar)-2); 150 dact_ui_update(); 151 } 152 153 void dact_ui_setup(uint32_t blocks) { 154 dact_ui_setopt(DACT_UI_OPT_FILEBLKS,blocks); 155 dact_ui_incrblkcnt(0); 156 } 157 158 char *dact_ui_getuserinput(char *prompt, unsigned int n, int password) { 159 char *ret; 160 FILE *fd; 161 162 if (password) { 163 if (n<128) return(NULL); 164 ret=getpass(prompt); 165 return(ret); 166 } 167 if (dact_ui_getopt(DACT_UI_OPT_PASSSTDIN)==1) { 168 fd=stdin; 169 } else { 170 fd=fopen("/dev/tty", "r"); 171 } 172 if ((ret=malloc(n))==NULL) return(NULL); 173 fprintf(stderr, "%s", prompt); 174 fflush(stderr); 175 fgets(ret, n, fd); 176 ret=strsep(&ret, "\n\r"); 177 if (fd!=stdin) fclose(fd); 178 return(ret); 179 } 180 181 182 /* 183 void dact_ui_debug() 184 */ |