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):
82 menu = [ "Exit", "Edit", "Delete" ]
83 actions = [ None, "edit", "delete" ]
89 self.show_item("0. Exit")
90 self.show_item("1. Add")
93 self.show_item(str(i) + ". " + l)
95 ans = self.get_string("Option: ")
98 if opt >= i or opt <= 0:
102 return (opt - 1, self.item())
108 self.show_item("0. Exit")
111 if f.get_type() == "string":
112 self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_value())
113 elif f.get_type() == "options":
114 self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_cur_display())
116 ans = self.get_string("Option: ")
120 # raise/show error/message about wrong option
127 self.show_form(fs[opt - 1])
128 def show_form(self, f):
129 if f.get_type() == "string":
130 self.show_item(f.get_name() + ": " + f.get_value())
131 s = self.get_string(f.get_name() + ": ")
133 elif f.get_type() == "options":
135 def show_options(self, f):
136 self.show_item(f.get_name() + ": " + f.get_cur_display())
137 for i in range(f.get_length()):
138 self.show_item(str(i) + ". " + f.get_display(i))
139 ans = self.get_string("Option: ")
143 # raise/show error/message about wrong option
147 f.set_value(f.get_value(opt))
150 # vim:tabstop=4:expandtab:smartindent