From e353b1b17701e54a0d9f93e42b1956acdf253cc7 Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Tue, 23 Jul 2013 13:56:02 -0400 Subject: add supportfor multiple rows and piping between them --- pc.py | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 76 insertions(+), 19 deletions(-) diff --git a/pc.py b/pc.py index 1a721d9..48d51b5 100755 --- a/pc.py +++ b/pc.py @@ -34,19 +34,63 @@ input_buffer = "" cols = int(subprocess.check_output(["tput", "cols"])) rows = int(subprocess.check_output(["tput", "lines"])) -def call_process(process, string): - process = Popen(["sed", "-e", string], stdout=PIPE, stdin=PIPE, stderr=PIPE) - ret = process.communicate(input_buffer) +def call_process(process, pipe, stdin): + process = Popen(["sed", pipe], stdout=PIPE, stdin=PIPE, stderr=PIPE) + ret = process.communicate(stdin) if(process.returncode is not 0): - screen.addstr(ret[1][:-1].ljust(cols), curses.color_pair(2)); + return (ret[1], 1) else: - screen.addstr(ret[0]); + return (ret[0], 0) return +def call_pipes(a, pipe_array): + + # start first one with input buffer + ret = call_process("sed", pipe_array[0], input_buffer) + + if(len(pipe_array) == 1): + if(ret[1] != 0): + error = "Error on line 0:" + ret[0][:-1] + screen.addstr(error.ljust(cols), curses.color_pair(4)); + else: + screen.addstr(ret[0]) + return + + p = ret[0]; + for i in range(1, len(pipe_array)): + ret = call_process("sed", pipe_array[i], p); + if(ret[1] != 0): + error = "Error on line " + str(i) + ":" + ret[0][:-1] + screen.addstr(error.ljust(cols), curses.color_pair(4)); + else: + p = ret[0]; + + screen.addstr(p) + +def update(pipe_array, current_pipe): + draw_pipes(pipe_array, current_pipe) + call_pipes("sed", pipe_array) + screen.addstr("EOF") + +def draw_pipes(pipe_array, current_pipe): + + for pipe, num in map(None, pipe_array, range(0, len(pipe_array))): + color = 1 + if(num == current_pipe): + color = 3 + + screen.addstr(num, 0, str(num), curses.color_pair(2)) + screen.addstr(num, len(str(num)), " " + pipe.ljust(cols - len(str(num)) - 1), curses.color_pair(color)) + def main(argv): - input_string = "" + pipe_array = []; + command_array = []; + enable_array = []; + pipe_array.append(""); + current_pipe = 0; + global input_buffer try: @@ -66,24 +110,37 @@ def main(argv): while 1: key = screen.getch() screen.clear() - if(key > 256): + screen.addstr(rows - 1, 0, str(key) + " " + str(current_pipe)) + # UP + if(key == 259): + if(current_pipe is not 0): + current_pipe = current_pipe - 1; + update(pipe_array, current_pipe) continue; - if(key == 127): - input_string = input_string[:-1] - screen.addstr(input_string.ljust(cols), curses.color_pair(1)); - screen.addstr(input_buffer) - screen.addstr("EOF") + # DOWN + if(key == 258): + if(current_pipe == len(pipe_array) -1): + pipe_array.append(""); + current_pipe = current_pipe + 1; + update(pipe_array, current_pipe); continue; - elif(key == 13 or key == 10): - screen.addstr(input_string.ljust(cols), curses.color_pair(1)); - call_process("sed", input_string) + # BACKSPACE + if(key == 263): + if(len(pipe_array[current_pipe]) is not 0): + pipe_array[current_pipe] = pipe_array[current_pipe][:-1] + else: + if(current_pipe is not 0): + del pipe_array[current_pipe] + current_pipe = current_pipe - 1 + draw_pipes(pipe_array, current_pipe) + screen.addstr(input_buffer) screen.addstr("EOF") continue; + # ENTER else: - input_string+=chr(key) - screen.addstr(input_string.ljust(cols), curses.color_pair(1)); - screen.addstr(input_buffer) - screen.addstr("EOF") + if(key < 256): + pipe_array[current_pipe]+=chr(key) + update(pipe_array, current_pipe); finally: curses.endwin() -- cgit v1.2.1