summaryrefslogtreecommitdiff
path: root/pc.py
blob: 48d51b53f446a1b453515c1c1c7f5328677d1065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
import curses
import os
import sys
import StringIO

from subprocess import *
import subprocess

# our screen
screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
curses.start_color()
screen.keypad(True);

# default entry color
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_CYAN)

# default error color
curses.init_pair(4, curses.COLOR_WHITE, curses.COLOR_RED) 

# current entry color
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLUE)

# line number color, invert of entry 
curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_WHITE)

# our stdin input
input_buffer = ""

# rows and columns of our screen
cols = int(subprocess.check_output(["tput", "cols"]))
rows = int(subprocess.check_output(["tput", "lines"]))

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):
    return (ret[1], 1)
  else:
    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):

  pipe_array = [];
  command_array = [];
  enable_array = [];
  pipe_array.append("");
  current_pipe = 0;

  global input_buffer

  try:
    input_fh = open(argv[1], "r")
    for line in input_fh:
      input_buffer = input_buffer + line
  except:
    curses.endwin()
    sys.exit("please specify a file")
  
  # init our screen
  screen.addstr("".ljust(cols), curses.color_pair(1));
  screen.addstr(input_buffer)
  screen.addstr("EOF")

  try: 
    while 1:
      key = screen.getch()
      screen.clear()
      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;
      # 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;
      # 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:
        if(key < 256):
          pipe_array[current_pipe]+=chr(key)
        update(pipe_array, current_pipe);
  finally:
    curses.endwin()

if __name__ == "__main__":
    sys.exit(main(sys.argv))