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):
37 def set_value(self, value):
40 class OptionsForm(BaseForm):
41 def __init__(self, name, options, value):
44 self.options = options
46 self.index = self.find_index()
48 return map(lambda x: x[0], self.options).index(self.value)
50 return len(self.options)
51 def get_display(self, index):
52 return self.options[index][1]
53 def get_value(self, index):
54 return self.options[index][0]
55 def get_cur_display(self):
56 return self.options[self.index][1]
57 def get_cur_value(self):
61 def set_value(self, value):
63 self.index = self.find_index()
66 def get_string(self, prompt=""):
67 return raw_input(prompt.encode("utf-8"))
71 self.show_item(str(i) + ". " + l)
73 ans = self.get_string("Option: ")
81 def show_item(self, s):
84 menu = [ "Exit", "Edit", "Delete" ]
85 actions = [ None, "edit", "delete" ]
91 self.show_item("0. Exit")
92 self.show_item("1. Add")
95 self.show_item(str(i) + ". " + l)
97 ans = self.get_string("Option: ")
100 if opt >= i or opt <= 0:
104 return (opt - 1, self.item())
110 self.show_item("0. Exit")
113 if f.get_type() == "string":
114 self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_value())
115 elif f.get_type() == "options":
116 self.show_item(str(i) + ". " + f.get_name() + ": " + f.get_cur_display())
118 ans = self.get_string("Option: ")
122 # raise/show error/message about wrong option
129 self.show_form(fs[opt - 1])
130 def show_form(self, f):
131 if f.get_type() == "string":
132 self.show_item(f.get_name() + ": " + f.get_value())
133 s = self.get_string(f.get_name() + ": ")
135 elif f.get_type() == "options":
137 def show_options(self, f):
138 self.show_item(f.get_name() + ": " + f.get_cur_display())
139 for i in range(f.get_length()):
140 self.show_item(str(i) + ". " + f.get_display(i))
141 ans = self.get_string("Option: ")
145 # raise/show error/message about wrong option
149 f.set_value(f.get_value(opt))
152 # vim:tabstop=4:expandtab:smartindent