| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import sys |
| 16 |
import adonthell |
| 17 |
|
| 18 |
|
| 19 |
class console (adonthell.win_container): |
| 20 |
|
| 21 |
|
| 22 |
def __init__(self, ns): |
| 23 |
adonthell.win_container.__init__(self) |
| 24 |
|
| 25 |
self.namespace = ns |
| 26 |
self.history = [] |
| 27 |
self.hist_idx = 0 |
| 28 |
|
| 29 |
|
| 30 |
self.read_history () |
| 31 |
|
| 32 |
self.py_signal_connect (self.on_update, adonthell.win_event_UPDATE) |
| 33 |
|
| 34 |
|
| 35 |
self.font = adonthell.win_manager_get_font ("silverleaf") |
| 36 |
self.theme = adonthell.win_manager_get_theme ("silverleaf") |
| 37 |
|
| 38 |
self.move (10, 150) |
| 39 |
self.resize (300, 80) |
| 40 |
self.set_border (self.theme) |
| 41 |
self.set_background (self.theme) |
| 42 |
self.set_trans_background (True) |
| 43 |
|
| 44 |
self.info = adonthell.win_label () |
| 45 |
self.info.thisown = 0 |
| 46 |
self.info.move (5, 5) |
| 47 |
self.info.resize (290, 15) |
| 48 |
self.info.set_cursor_visible (False) |
| 49 |
self.info.set_cursor_moveable (False) |
| 50 |
self.info.set_font (adonthell.win_manager_get_font ("blue")) |
| 51 |
self.info.set_text ("Python console (type 'quit' to exit)") |
| 52 |
self.info.pack () |
| 53 |
|
| 54 |
self.entry = adonthell.win_write () |
| 55 |
self.entry.thisown = 0 |
| 56 |
|
| 57 |
self.entry.py_signal_connect (self.on_execute, adonthell.win_event_ACTIVATE_KEY) |
| 58 |
self.entry.move (5, 20) |
| 59 |
self.entry.resize (290, 55) |
| 60 |
|
| 61 |
|
| 62 |
self.entry.set_font (self.font) |
| 63 |
self.entry.set_cursor_visible (True) |
| 64 |
self.entry.set_cursor_moveable (True) |
| 65 |
self.entry.set_text ("") |
| 66 |
self.entry.pack () |
| 67 |
|
| 68 |
self.add (self.info) |
| 69 |
self.add (self.entry) |
| 70 |
self.set_focus_object (self.entry) |
| 71 |
|
| 72 |
self.set_visible_background (True); |
| 73 |
self.set_visible_border (True); |
| 74 |
self.set_visible_all (True); |
| 75 |
self.set_activate (True) |
| 76 |
|
| 77 |
self.entry.set_focus (True) |
| 78 |
self.entry.set_activate (True) |
| 79 |
|
| 80 |
|
| 81 |
def on_execute (self): |
| 82 |
text = self.entry.text_char () |
| 83 |
|
| 84 |
|
| 85 |
if text != None: |
| 86 |
|
| 87 |
|
| 88 |
if self.hist_idx == 0 or text != self.history[-1]: |
| 89 |
self.history.append (text + '\n') |
| 90 |
self.hist_idx = len (self.history) |
| 91 |
|
| 92 |
|
| 93 |
if text == "quit" or text == "exit": |
| 94 |
self.write_history () |
| 95 |
adonthell.gamedata_engine ().main_quit () |
| 96 |
|
| 97 |
|
| 98 |
try: |
| 99 |
result = eval (text, self.namespace) |
| 100 |
self.entry.set_text (str (result)) |
| 101 |
except: |
| 102 |
type, value = sys.exc_info()[:2] |
| 103 |
error = "Error:\n " + str (type) + ":\n \"" + str (value) + "\"" |
| 104 |
self.entry.set_text (error) |
| 105 |
|
| 106 |
|
| 107 |
def on_update (self): |
| 108 |
|
| 109 |
if adonthell.input_has_been_pushed (adonthell.SDLK_TAB): |
| 110 |
self.write_history () |
| 111 |
adonthell.gamedata_engine ().main_quit () |
| 112 |
|
| 113 |
|
| 114 |
elif adonthell.input_has_been_pushed (adonthell.SDLK_DELETE): |
| 115 |
self.entry.set_text ("") |
| 116 |
|
| 117 |
|
| 118 |
elif adonthell.input_has_been_pushed (adonthell.SDLK_UP): |
| 119 |
if self.hist_idx > 0: |
| 120 |
self.hist_idx = self.hist_idx - 1 |
| 121 |
self.entry.set_text (self.history[ self.hist_idx ][:-1]) |
| 122 |
|
| 123 |
|
| 124 |
elif adonthell.input_has_been_pushed (adonthell.SDLK_DOWN): |
| 125 |
if self.hist_idx < len (self.history) - 1: |
| 126 |
self.hist_idx = self.hist_idx + 1 |
| 127 |
self.entry.set_text (self.history[ self.hist_idx ][:-1]) |
| 128 |
|
| 129 |
|
| 130 |
def read_history (self): |
| 131 |
dir = adonthell.gamedata_user_data_dir () |
| 132 |
dir = dir + "/history" |
| 133 |
|
| 134 |
|
| 135 |
try: |
| 136 |
file = open (dir, 'r') |
| 137 |
except IOError: |
| 138 |
return |
| 139 |
|
| 140 |
if file != None: |
| 141 |
self.history = file.readlines () |
| 142 |
self.hist_idx = len (self.history) |
| 143 |
file.close () |
| 144 |
|
| 145 |
|
| 146 |
def write_history (self): |
| 147 |
dir = adonthell.gamedata_user_data_dir () |
| 148 |
dir = dir + "/history" |
| 149 |
|
| 150 |
|
| 151 |
file = open (dir, 'w') |
| 152 |
if file != None: |
| 153 |
file.writelines (self.history[-50:]) |
| 154 |
file.close () |