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
dact_ui_update(void)43 void dact_ui_update(void) {
44 static int drawing=0;
45 float done;
46 char *bar1, *bar2, *clean="";
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 clean="\033[K";
76 } else {
77 fprintf(stderr, "=> [%s%s] %3i%%",bar1,bar2,percent);
78 }
79 fprintf(stderr, " [%c] | Status: %s%s\r",*("|/-\\"+(drawing&3)),dact_ui_statusvar, clean);
80 fflush(stderr);
81
82 free(bar2);
83 if (bar1!=(bar2+width)) free(bar1);
84
85 drawing++;
86 return;
87 }
88
dact_ui_init(void)89 void dact_ui_init(void /*for now*/) {
90 dact_ui_setopt(DACT_UI_OPT_COLOR, 0);
91 dact_ui_status(0, "Initialized.");
92 }
93
dact_ui_deinit(void)94 void dact_ui_deinit(void) {
95 if (dact_ui_getopt(DACT_UI_OPT_LEVEL)) fprintf(stderr, "\n");
96 }
97
dact_ui_percentdone(int percent)98 void dact_ui_percentdone(int percent) {
99 dact_ui_setopt(DACT_UI_OPT_PERCENT,percent);
100 dact_ui_update();
101 }
102
dact_ui_incrblkcnt(int n)103 void dact_ui_incrblkcnt(int n) {
104 static uint32_t blkcnt=0;
105 uint32_t blocks;
106
107 if (n==0) blkcnt=0;
108
109 blkcnt+=n;
110 blocks=dact_ui_getopt(DACT_UI_OPT_FILEBLKS);
111 if (blocks==0) {
112 dact_ui_percentdone(-1);
113 return;
114 }
115 dact_ui_percentdone((int) ((float) (((float) blkcnt/(float) blocks)*100)));
116 }
117
dact_ui_optmanip(int action,int opt,int32_t val)118 int32_t dact_ui_optmanip(int action, int opt, int32_t val) {
119 static int opts[10]={0,0,0,0,0,0,0,0,0};
120
121 if (opt>=(sizeof(opts)/sizeof(int))) return(-1);
122
123 switch (action) {
124 case DACT_UI_MANIP_GET:
125 return(opts[opt]);
126 break;
127 case DACT_UI_MANIP_SET:
128 return(opts[opt]=val);
129 break;
130 }
131 return(-1);
132 }
133
dact_ui_getopt(int opt)134 int32_t dact_ui_getopt(int opt) {
135 return(dact_ui_optmanip(DACT_UI_MANIP_GET, opt, 0));
136 }
137
dact_ui_setopt(int opt,int32_t val)138 int dact_ui_setopt(int opt, int32_t val) {
139 return(dact_ui_optmanip(DACT_UI_MANIP_SET, opt, val));
140 }
141
dact_ui_status(int level,const char * status)142 void dact_ui_status(int level, const char *status) {
143 if (level>dact_ui_getopt(DACT_UI_OPT_LEVEL)) return;
144 strncpy(dact_ui_statusvar,status,sizeof(dact_ui_statusvar)-1);
145 dact_ui_update();
146 }
147
dact_ui_status_append(int level,const char * status)148 void dact_ui_status_append(int level, const char *status) {
149 if (level>dact_ui_getopt(DACT_UI_OPT_LEVEL)) return;
150 strncat(dact_ui_statusvar,status,sizeof(dact_ui_statusvar)-strlen(dact_ui_statusvar)-2);
151 dact_ui_update();
152 }
153
dact_ui_setup(uint32_t blocks)154 void dact_ui_setup(uint32_t blocks) {
155 dact_ui_setopt(DACT_UI_OPT_FILEBLKS,blocks);
156 dact_ui_incrblkcnt(0);
157 }
158
dact_ui_getuserinput(char * prompt,unsigned int n,int password)159 char *dact_ui_getuserinput(char *prompt, unsigned int n, int password) {
160 char *ret;
161 FILE *fd;
162
163 if (password) {
164 if (n<128) return(NULL);
165 ret=getpass(prompt);
166 return(ret);
167 }
168 if (dact_ui_getopt(DACT_UI_OPT_PASSSTDIN)==1) {
169 fd=stdin;
170 } else {
171 fd=fopen("/dev/tty", "r");
172 }
173 if ((ret=malloc(n))==NULL) return(NULL);
174 fprintf(stderr, "%s", prompt);
175 fflush(stderr);
176 fgets(ret, n, fd);
177 ret=strsep(&ret, "\n\r");
178 if (fd!=stdin) fclose(fd);
179 return(ret);
180 }
181
182
183 /*
184 void dact_ui_debug()
185 */
186