[go: up one dir, main page]

File: ui.c

package info (click to toggle)
aewan 1.0.01-4
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 728 kB
  • sloc: ansic: 4,438; python: 122; makefile: 108; sh: 14
file content (141 lines) | stat: -rw-r--r-- 4,134 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Copyright (c) 2003 Bruno T. C. de Oliveira

LICENSE INFORMATION:
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
Copyright (c) 2002 Bruno T. C. de Oliveira

INFORMAES DE LICENA:
Este programa  um software de livre distribuio; voc pode
redistribu-lo e/ou modific-lo sob os termos da GNU General
Public License, conforme publicado pela Free Software Foundation,
pela verso 2 da licena ou qualquer verso posterior.

Este programa  distribudo na esperana de que ele ser til
aos seus usurios, porm, SEM QUAISQUER GARANTIAS; sem sequer
a garantia implcita de COMERCIABILIDADE ou DE ADEQUAO A
QUALQUER FINALIDADE ESPECFICA. Consulte a GNU General Public
License para obter mais detalhes (uma cpia acompanha este
programa, armazenada no arquivo COPYING).
*/

#include "bores/bores.h"
#include <ncurses.h>
#include <string.h>
#include <stdlib.h>

#include "ui.h"

int ui_cancel;  /* global cancel flag */

/* clears bottom line of screen and puts cursor at the beginning of that line.
 * Also lowers the global ui_cancel flag. */
static void ui_prepare_ex(int fg, int bg) {
   int w = kurses_width();
   kurses_move(0, kurses_height() - 1);
   kurses_color(fg, bg);
   while (w--) addch(' ');
   kurses_move(0, kurses_height() - 1);
   ui_cancel = 0;
}

static void ui_prepare(void) {
   ui_prepare_ex(15, 4);
}


int ui_ask_i(const char *prompt, int def, int min, int max) {
   static char buf[32]; int ans;
   
   do {
      ui_prepare();
      printw("%s [%d]: ", prompt, def);
      if (!kurses_line_input(buf, 32)) return (ui_cancel = -1);
      if (!*buf) return def;  /* user just pressed ENTER, so use default */
      ans = atoi(buf);
   } while (!(ans >= min && ans <= max));

   return ans;
}

char *ui_ask_s(const char *prompt, const char *def) {
   static char buf[256];
   ui_prepare();

   if (def) printw("%s [%s]: ", prompt, def);
   else     printw("%s: ", prompt);

   if (!kurses_line_input(buf, 256)) { ui_cancel = -1; return NULL; }

   /* if user didn't type anything and there is a default, return default;
    * otherwise just return whatever it is the user typed (even if it's an
    * empty string) */
   if (!*buf && def) return strdup(def);
   else return strdup(buf);
}

int ui_ask_color(const char *prompt) {
   int i;
   static char hexdigit[] = "0123456789abcdef";
   ui_prepare();
   printw("%s ", prompt);

   /* print the colors */
   for (i = 0; i <= 15; i++) {
      kurses_color(i, i ? 0 : 7);
      printw(" %c ", hexdigit[i]);
   }

   kurses_color(0, 7);
   printw(": ");

   while (1) {
      int ch, color_code;
      ch = getch(); color_code = (ch >= 'a' && ch <= 'f') ? ch - 'a' + 10 : 
                                 (ch >= 'A' && ch <= 'F') ? ch - 'A' + 10 :
                                                            ch - '0';
      if (ch == 27 || ch == 3 || ch == 7) return (ui_cancel = -1);
      if (color_code >= 0 && color_code <= 15) return color_code;
   }
}

int ui_ask_yn(const char *prompt, int defyes) {
   ui_prepare();
   printw("%s [%s] ", prompt, defyes ? "Yes" : "No");
   while (1) {
      int ch = getch();
      switch (ch) {
         case 10:            return defyes;
         
         case 'y': case 'Y': return 1;
         case 'n': case 'N': return 0;
         
         case 27: 
         case 3: 
         case 7: return (ui_cancel = -1);
      }
   }
}

void ui_message(const char *msg, int error) {
   if (error)
      ui_prepare_ex(15, 1);
   else
      ui_prepare();
      
   addstr(msg);
   getch();
}