Procházet zdrojové kódy

implement console history, disable reader thread

mikymod před 12 roky
rodič
revize
4ce30bc41c
1 změnil soubory, kde provedl 81 přidání a 27 odebrání
  1. 81 27
      tools/gui/console/console.py

+ 81 - 27
tools/gui/console/console.py

@@ -38,22 +38,60 @@ CMD_HELP    = "help"    # Console help
 CMD_VOID    = ""        
 
 
-class ReaderThread(threading.Thread):
+# class ReaderThread(threading.Thread):
+# #------------------------------------------------------------------------------
+#     def __init__(self, socket, error_buffer):
+#         threading.Thread.__init__(self)
+
+#         self.t_socket = socket
+#         self.t_error = error_buffer
+#         self.t_tmp = bytearray()
+
+# #------------------------------------------------------------------------------
+#     def run(self):
+#         while True:
+#             self.t_tmp = self.t_socket.recv(1024)
+#             self.t_error = self.t_tmp.decode('utf-8', 'ignore')
+#             print(self.t_error)
+
+class ConsoleHistory:
 #------------------------------------------------------------------------------
-    def __init__(self, socket, error_buffer):
-        threading.Thread.__init__(self)
+    def __init__(self):
+        self.m_list = list()
 
-        self.t_socket = socket
-        self.t_error = error_buffer
-        self.t_tmp = bytearray()
+        self.m_count = 0
+        self.m_index = 0
 
 #------------------------------------------------------------------------------
-    def run(self):
-        while True:
-            self.t_tmp = self.t_socket.recv(1024)
-            self.t_error = self.t_tmp.decode('utf-8', 'ignore')
-            print(self.t_error)
-         
+    def add(self, cmd):
+        self.m_list.append(cmd)
+        self.m_count += 1
+        self.m_index = self.m_count
+
+#------------------------------------------------------------------------------
+    def previous(self):
+        if self.m_count != 0:
+            self.m_index -= 1
+
+            if self.m_index < 0:
+                self.m_index = 0
+
+            return self.m_list[self.m_index]
+
+        return ""
+
+#------------------------------------------------------------------------------
+    def following(self):
+        if self.m_count != 0:
+            self.m_index += 1
+
+            if self.m_index > self.m_count-1:
+                self.m_index = self.m_count - 1
+
+            return self.m_list[self.m_index]
+
+        return ""
+
 
 class Console:
 #------------------------------------------------------------------------------
@@ -74,12 +112,14 @@ class Console:
         self.m_address = address
         self.m_error_buffer = ""
 
+        self.history = ConsoleHistory()
+
         self.m_sock = socket.create_connection((self.m_address, 10000))
 
-        self.m_thread = ReaderThread(self.m_sock, self.m_error_buffer)
-        self.m_lock = threading.Lock()
+        #self.m_thread = ReaderThread(self.m_sock, self.m_error_buffer)
+        #self.m_lock = threading.Lock()
 
-        self.m_thread.start()
+        #self.m_thread.start()
 
         Gtk.main()
 
@@ -91,47 +131,61 @@ class Console:
 #------------------------------------------------------------------------------
     def on_key_pressed(self, entry, event):
         # If return is pressed, run command
-        if event.keyval == 0xff0d :
+        if event.keyval == 0xff0d:
             cmd = entry.get_text()
             self.parse_command(cmd)
+            return True
+
+        if event.keyval == 0xff52:
+            cmd = self.history.previous()
+            self.print_to_entry(cmd)
+            return True
+
+        if event.keyval == 0xff54:
+            cmd = self.history.following()
+            self.print_to_entry(cmd)
+            return True
+
 
 #------------------------------------------------------------------------------
     def parse_command(self, cmd):
+        self.history.add(cmd)
+
         if cmd == CMD_CLEAR:
             self.m_buffer.set_text("")
             self.m_entry.set_text("")
 
         elif cmd == CMD_EXIT:
-            self.on_destroy()         
+            self.on_destroy()  
 
         elif cmd == CMD_HELP:
             self.print_help()
 
         elif cmd == CMD_VOID:
-            self.print_command('');
+            self.print_to_console("");
 
         else:    
             self.run_command(cmd)        
 
 #------------------------------------------------------------------------------
     def run_command(self, cmd):
-        self.m_lock.acquire()
-        # Send command to Crown
         self.m_sock.send(cmd.encode('utf-8'))
-        self.print_command(cmd)
-        print("locked :" + self.m_error_buffer)
-
-        self.m_lock.release()
+        self.print_to_console(cmd)
 
 #------------------------------------------------------------------------------
-    def print_command(self, cmd):
+    def print_to_console(self, text):
         # Print command to console
         end_iter = self.m_buffer.get_end_iter()
-        a_string = "> " + cmd + "\n"
+        a_string = "> " + text + "\n"
         # Append command to the end of buffer
         self.m_buffer.insert(end_iter, a_string, len(a_string))
         # Reset entry
-        self.m_entry.set_text("")
+        self.print_to_entry("")
+
+#------------------------------------------------------------------------------
+    def print_to_entry(self, text):
+        self.m_entry.set_text(text)
+
 
 #------------------------------------------------------------------------------
     # def popup_dialog(self, message, expl):