install

installer
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/install

commit 3b0f1300f504453add966bae05607e9c7fe687bc
parent e744c1df952f47fa26b838c85ca263ffe9b6e99d
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 24 Oct 2016 19:03:09 +0200

Add(Input): Better input handling. Some bugs though

Diffstat:
Ainput.py | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmain.py | 20++++++++++----------
Mscreen.py | 35++++++++++++++++++++++-------------
Mscreens/config/config.py | 88+++++++++++++++----------------------------------------------------------------
Mscreens/main/main.py | 20++++++++++----------
5 files changed, 159 insertions(+), 105 deletions(-)

diff --git a/input.py b/input.py @@ -0,0 +1,101 @@ +################################### LICENSE #################################### +# Copyright 2016 Louis Solofrizzo # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +################################################################################ + +## +# input.py +# Created: 24/10/2016 +# By: Louis Solofrizzo <louis@morphux.org> +## + +import curses + +class Input: + + choices = ["ACCEPT", "CANCEL"] + current_choice = 0 + current_string = "" + current_f_callback = 0 + in_input = 0 + + def s_input(self, win, title, s_def, f_callback): + size = win.getmaxyx() + if self.in_input == 0: + self.current_f_callback = f_callback + self.current_string = s_def + self.in_input = 1 + self.current_choice = 0 + if self.current_choice == 0: + curses.curs_set(1) + self.center(win, 2, size[1], title, curses.A_BOLD) + self.show_choices(win, size[1], 6) + self.input_string(win, 4, size[1]) + + def title(self, x, y, title, win): + size = win.getmaxyx() + self.center(win, 2, size[1], title, curses.A_BOLD) + + def show_choices(self, win, x, y): + t_len = 0 + i = 0 + for s in self.choices: + t_len += len(s) + x = (x / 2) - ((t_len + 9) / 2) + for s in self.choices: + flag = 0 + win.addstr(y, x, "<") + if i == 0: + flag = curses.color_pair(3) + else: + flag = curses.color_pair(1) + if (i + 1 == self.current_choice): + flag |= curses.A_BOLD | curses.A_REVERSE + win.addstr(y, x + 1, s, flag) + x += 1 + len(s) + win.addstr(y, x, "> ") + x += 6 + i += 1 + + def input_string(self, win, y, x): + flag = 0 + start_x = int((x * 0.25) / 2) + if (self.current_choice == 0): + flag |= curses.color_pair(4) + win.addstr(y, start_x, self.current_string, curses.A_REVERSE | flag) + j = start_x + len(self.current_string) + while j < x - start_x: + win.addstr(y, j, " ", curses.A_REVERSE | flag) + j += 1 + win.move(y, start_x + len(self.current_string)) + + def center(self, win, y, x, string, attr = 0): + win.addstr(y, (x / 2) - len(string) / 2, string, attr) + + def input(self, key): + if (self.current_choice == 0): + if (key == curses.KEY_BACKSPACE or key == 127 or key == 0x7f): + self.current_string = self.current_string[:-1] + elif (key > 31 and key < 127): + self.current_string += str(chr(key)) + if (key == 9): + if (self.current_choice < 2): + curses.curs_set(0) + self.current_choice += 1 + else: + curses.curs_set(1) + self.current_choice = 0; + elif (key == curses.KEY_ENTER or key == 10 or key == 13): + if (self.current_choice == 1): + self.current_f_callback(self.current_string) diff --git a/main.py b/main.py @@ -1,18 +1,18 @@ #!/usr/bin/python ################################### LICENSE #################################### -# Copyright 2016 Louis Solofrizzo # +# Copyright 2016 Louis Solofrizzo # # # -# Licensed under the Apache License, Version 2.0 (the "License"); # -# you may not use this file except in compliance with the License. # -# You may obtain a copy of the License at # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # # # -# http://www.apache.org/licenses/LICENSE-2.0 # +# http://www.apache.org/licenses/LICENSE-2.0 # # # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # ################################################################################ ## diff --git a/screen.py b/screen.py @@ -1,18 +1,17 @@ -#!/usr/bin/python ################################### LICENSE #################################### -# Copyright 2016 Louis Solofrizzo # +# Copyright 2016 Louis Solofrizzo # # # -# Licensed under the Apache License, Version 2.0 (the "License"); # -# you may not use this file except in compliance with the License. # -# You may obtain a copy of the License at # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # # # -# http://www.apache.org/licenses/LICENSE-2.0 # +# http://www.apache.org/licenses/LICENSE-2.0 # # # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # ################################################################################ ## @@ -25,6 +24,7 @@ import curses import os +from input import Input class Screen: @@ -121,6 +121,7 @@ class Screen: def loop(self): quit = 0 key = 0 + input = Input() size = self.stdscr.getmaxyx() height = size[0] / 4 if height < 10: @@ -132,14 +133,22 @@ class Screen: self.stdscr.refresh() win.refresh() while quit != -1: - if self.curr_screen.config["type"] == "menu" or self.curr_screen.config["type"] == "inputs": - key = self.stdscr.getch() + key = self.stdscr.getch() + if self.curr_screen.config["type"] == "menu": self.curr_screen.input(key) + elif self.curr_screen.config["type"] == "input": + input.input(key) win.erase() + if self.curr_screen.config["type"] == "input": + conf = self.curr_screen.config["input"][0] + input.s_input(win, conf["title"], conf["default"], conf["function"]) quit = self.curr_screen.refresh(win) if (quit != self.curr_screen.config["id"]): win.erase() self.change_screen(quit) + if self.curr_screen.config["type"] == "input": + conf = self.curr_screen.config["input"][0] + input.s_input(win, conf["title"], conf["default"], conf["function"]) self.curr_screen.refresh(win) win.border() self.stdscr.refresh() diff --git a/screens/config/config.py b/screens/config/config.py @@ -1,17 +1,17 @@ ################################### LICENSE #################################### -# Copyright 2016 Louis Solofrizzo # +# Copyright 2016 Louis Solofrizzo # # # -# Licensed under the Apache License, Version 2.0 (the "License"); # -# you may not use this file except in compliance with the License. # -# You may obtain a copy of the License at # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # # # -# http://www.apache.org/licenses/LICENSE-2.0 # +# http://www.apache.org/licenses/LICENSE-2.0 # # # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # ################################################################################ ## @@ -26,23 +26,20 @@ class Config: key = 0 quit = 1 - is_input = 1 - current_string = "morphux" - choices = ["ACCEPT", "CANCEL"] - current_choice = 0 def init(self): self.config = { "id": 1, "title": "Config", - "type": "inputs", - "inputs": [ + "type": "input", + "input": [ { "name": "hostname", "title": "SET YOUR HOSTNAME", "default": "morphux", "config": "system.hostname", - "type": "text" + "type": "text", + "function": self.hostname }, ] } @@ -50,62 +47,9 @@ class Config: def input(self, key): self.key = key - if (self.is_input and self.current_choice == 0): - if (key == curses.KEY_BACKSPACE or key == 127 or key == 0x7f): - self.current_string = self.current_string[:-1] - elif (key > 31 and key < 127): - self.current_string += str(chr(key)) - if (key == 9): - if (self.current_choice < 2): - curses.curs_set(0) - self.current_choice += 1 - else: - curses.curs_set(1) - self.current_choice = 0; - - def center(self, win, y, x, string, attr = 0): - win.addstr(y, (x / 2) - len(string) / 2, string, attr) - - def input_s(self, win, y, x): - flag = 0 - start_x = int((x * 0.25) / 2) - if (self.current_choice == 0): - flag |= curses.color_pair(4) - win.addstr(y, start_x, self.current_string, curses.A_REVERSE | flag) - j = start_x + len(self.current_string) - while j < x - start_x: - win.addstr(y, j, " ", curses.A_REVERSE | flag) - j += 1 - win.move(y, start_x + len(self.current_string)) - - def show_choices(self, win, x, y): - t_len = 0 - i = 0 - for s in self.choices: - t_len += len(s) - x = (x / 2) - ((t_len + 9) / 2) - for s in self.choices: - flag = 0 - win.addstr(y, x, "<") - if i == 0: - flag = curses.color_pair(3) - else: - flag = curses.color_pair(1) - if (i + 1 == self.current_choice): - flag |= curses.A_BOLD | curses.A_REVERSE - win.addstr(y, x + 1, s, flag) - x += 1 + len(s) - win.addstr(y, x, "> ") - x += 6 - i += 1 + def hostname(self, string): + print "Okay !" def refresh(self, win): - if (self.current_choice == 0): - curses.curs_set(1) - size = win.getmaxyx() - self.center(win, 2, size[1], "SET YOUR HOSTNAME", curses.A_BOLD) - self.show_choices(win, size[1], 6) - if (self.is_input == 1): - self.input_s(win, 4, size[1]) return self.quit diff --git a/screens/main/main.py b/screens/main/main.py @@ -1,17 +1,17 @@ ################################### LICENSE #################################### -# Copyright 2016 Louis Solofrizzo # +# Copyright 2016 Louis Solofrizzo # # # -# Licensed under the Apache License, Version 2.0 (the "License"); # -# you may not use this file except in compliance with the License. # -# You may obtain a copy of the License at # +# Licensed under the Apache License, Version 2.0 (the "License"); # +# you may not use this file except in compliance with the License. # +# You may obtain a copy of the License at # # # -# http://www.apache.org/licenses/LICENSE-2.0 # +# http://www.apache.org/licenses/LICENSE-2.0 # # # -# Unless required by applicable law or agreed to in writing, software # -# distributed under the License is distributed on an "AS IS" BASIS, # -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # -# See the License for the specific language governing permissions and # -# limitations under the License. # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # ################################################################################ ##