1 # -*- mode: python; encoding: utf-8; -*-
3 # Copyright 2014 Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # -*- mode: python; encoding: utf-8; -*-
28 class StringForm(BaseForm):
29 def __init__(self, name, value):
35 def set_value(self, value):
38 class OptionsForm(BaseForm):
39 def __init__(self, name, options, value):
42 self.options = options
44 self.index = self.find_index()
46 return map(lambda x: x[0], self.options).index(self.value)
48 return len(self.options)
49 def get_display(self, index):
50 return self.options[index][1]
51 def get_value(self, index):
52 return self.options[index][0]
53 def get_cur_display(self):
54 return self.options[self.index][1]
55 def get_cur_value(self):
59 def set_value(self, value):
61 self.index = self.find_index()
64 def get_string(self, prompt=""):
65 return raw_input(prompt.encode("utf-8"))
69 self.show_item(str(i) + ". " + l)
71 ans = self.get_string("Option: ")
79 def show_item(self, s):
84 self.show_item("0. Exit")
87 if f.get_type() == "string":
88 self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_value())
89 elif f.get_type() == "options":
90 self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_cur_display())
92 ans = self.get_string("Option: ")
96 # raise/show error/message about wrong option
103 self.show_form(fs[opt - 1])
104 def show_form(self, f):
105 if f.get_type() == "string":
106 self.show_item(f.get_name() + ": " + f.get_value())
107 s = self.get_string(f.get_name() + ": ")
109 elif f.get_type() == "options":
111 def show_options(self, f):
112 self.show_item(f.get_name() + ": " + f.get_cur_display())
113 for i in range(f.get_length()):
114 self.show_item(str(i) + ". " + f.get_display(i))
115 ans = self.get_string("Option: ")
119 # raise/show error/message about wrong option
123 f.set_value(f.get_value(opt))
126 # vim:tabstop=4:expandtab:smartindent