OSDN Git Service

cui mode 対応
[unagi/old-svn-converted.git] / client / trunk / anago / cui_gauge.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #ifdef WIN32
6   #include <windows.h>
7 #endif
8 #include "memory_manage.h"
9 #include "widget.h"
10
11 struct cui_gauge{
12         const char *name;
13         int value, range;
14         int lineback, lineforward;
15 };
16
17 static void range_set(void *obj, int value)
18 {
19         struct cui_gauge *t = obj;
20         t->range = value;
21 }
22
23 static void console_move(int line)
24 {
25         if(line == 0){
26                 return;
27         }
28 #ifdef WIN32
29         HANDLE c;
30         CONSOLE_SCREEN_BUFFER_INFO info;
31         c = GetStdHandle(STD_OUTPUT_HANDLE);
32         if(GetConsoleScreenBufferInfo(c, &info) == 0){
33                 //command.com, cygwin shell, mingw shell
34                 if(line < 0){
35                         printf("\x1b[%dA", -line);
36                 }else if(line == 1){
37                         printf("\n");
38                         fflush(stdout);
39                 }else{
40                         printf("\n");
41                         fflush(stdout);
42                         printf("\x1b[%dB", line - 1);
43 //                      printf("\x1b[%dB", line - 1);
44 //                      fflush(stdout);
45                 }
46         }else{
47                 //cmd.exe
48                 info.dwCursorPosition.X = 0;
49                 info.dwCursorPosition.Y += line;
50                 SetConsoleCursorPosition(c, info.dwCursorPosition);
51         }
52 #else
53         if(line < 0){
54                 printf("\x1b[%dA\x1b[35D", -line);
55         }else{
56                 printf("\x1b[%dB\x1b[35D", line);
57         }
58 #endif
59 //      fflush(stdout);
60 }
61
62 static void draw(const struct cui_gauge *t)
63 {
64         const int barnum = 16;
65         const int unit = t->range / barnum;
66         int igeta = t->value / unit;
67         char bar[barnum + 3 + 1];
68         char *text = bar;
69         int i;
70         assert(igeta <= barnum);
71         printf("%s 0x%06x/0x%06x ", t->name, (int) t->value, (int) t->range);
72         *text++ = '|';
73         for(i = 0; i < igeta; i++){
74                 if(i == barnum / 2){
75                         *text++ = '|';
76                 }
77                 *text++ = '#';
78         }
79         for(; i < barnum; i++){
80                 if(i == barnum / 2){
81                         *text++ = '|';
82                 }
83                 *text++ = ' ';
84         }
85         *text++ = '|';
86         *text = '\0';
87         printf(bar);
88 //      fflush(stdout);
89 }
90
91
92 static void value_set(void *obj, void *d, int value)
93 {
94         struct cui_gauge *t = (struct cui_gauge *) obj;
95         t->value = value;
96         if(t->range != 0){
97                 draw(t);
98         }
99         console_move(1);
100 }
101
102 static void value_add(void *obj, void *d, int value)
103 {
104         struct cui_gauge *t = (struct cui_gauge *) obj;
105         t->value += value;
106         console_move(t->lineback);
107         draw(t);
108         console_move(t->lineforward);
109 }
110
111 static void name_set(void *obj, const char *name, int lineforward, int lineback)
112 {
113         struct cui_gauge *t = (struct cui_gauge *) obj;
114         t->name = name;
115         t->lineforward = lineforward;
116         t->lineback = lineback;
117 }
118
119 static void label_set(void *obj, const char *format, ...)
120 {
121         va_list list;
122         const struct cui_gauge *t = (const struct cui_gauge *) obj;
123
124         va_start(list, format);
125         console_move(t->lineback);
126         printf("%s ", t->name);
127         vprintf(format, list);
128         console_move(t->lineforward);
129         va_end(list);
130 }
131
132 void cui_gauge_new(struct gauge *t, const char *name, int lineforward, int lineback)
133 {
134         t->bar =  Malloc(sizeof(struct cui_gauge));
135         t->label = t->bar;
136         name_set(t->bar, name, lineforward, lineback);
137         t->range_set = range_set;
138         t->value_set = value_set;
139         t->value_add = value_add;
140         t->label_set = label_set;
141 }
142
143 void cui_gauge_destory(struct gauge *t)
144 {
145         Free(t->bar);
146         t->bar = NULL;
147         t->label = NULL;
148         t->range_set = NULL;
149         t->value_set = NULL;
150         t->value_add = NULL;
151         t->label_set = NULL;
152 }