Przeglądaj źródła

improve console behavior

mikymod 12 lat temu
rodzic
commit
bea26d8677
2 zmienionych plików z 57 dodań i 32 usunięć
  1. 41 24
      tools/gui/console/console.py
  2. 16 8
      tools/gui/console/ui/console.glade

+ 41 - 24
tools/gui/console/console.py

@@ -32,33 +32,47 @@ import threading
 from gi.repository import Gtk
 
 # Client Console commands
-CMD_CLEAR   = "clear"   # Clear console output
-CMD_EXIT    = "exit"    # Close console
-CMD_HELP    = "help"    # Console help
-CMD_VOID    = ""        
+CMD_CLEAR   = "clear"           # Clear console output
+CMD_EXIT_1  = "exit"            # Close console
+CMD_EXIT_2  = "Device.stop()"   # Close console
+CMD_HELP    = "help"            # Console help
+CMD_HISTORY = "history"         # History
+CMD_VOID    = ""
 
 
-# class ReaderThread(threading.Thread):
-# #------------------------------------------------------------------------------
-#     def __init__(self, socket, error_buffer):
-#         threading.Thread.__init__(self)
+class ReaderThread(threading.Thread):
+#------------------------------------------------------------------------------
+    def __init__(self, console):
+        threading.Thread.__init__(self)
+        self.t_console = console
+        self.t_stop = threading.Event()
+        self.t_is_running = True
+
+#------------------------------------------------------------------------------
+    def stop(self):
+        self.t_stop.set()
+        self.t_is_running = False
+
+
+#------------------------------------------------------------------------------
+    def stopped(self):
+        return self.t_stop.isSet()
 
-#         self.t_socket = socket
-#         self.t_error = error_buffer
-#         self.t_tmp = bytearray()
+#------------------------------------------------------------------------------
+    def run(self):
+        while self.t_is_running:
+            self.t_tmp = self.t_console.m_sock.recv(1024)
+
+            if self.t_tmp != "":
+                self.t_tmp = self.t_tmp.decode("utf-8")
+                self.t_error = self.t_tmp.split('\x00', 1)[0]
+                self.t_console.print_to_console(self.t_error)
 
-# #------------------------------------------------------------------------------
-#     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):
         self.m_list = list()
-
         self.m_count = 0
         self.m_index = 0
 
@@ -116,16 +130,15 @@ class Console:
 
         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.start()
+        self.m_thread = ReaderThread(self)
+        self.m_thread.start()
 
         Gtk.main()
 
 #------------------------------------------------------------------------------
     def on_destroy(self, *args):
         self.m_sock.close()
+        self.m_thread.stop()
         Gtk.main_quit(*args)
 
 #------------------------------------------------------------------------------
@@ -155,9 +168,13 @@ class Console:
             self.m_buffer.set_text("")
             self.m_entry.set_text("")
 
-        elif cmd == CMD_EXIT:
+        elif cmd == CMD_EXIT_1:
             self.on_destroy()  
 
+        elif cmd == CMD_EXIT_2:
+            self.run_command(cmd)                  
+            self.on_destroy() 
+
         elif cmd == CMD_HELP:
             self.print_help()
 
@@ -179,6 +196,7 @@ class Console:
         a_string = "> " + text + "\n"
         # Append command to the end of buffer
         self.m_buffer.insert(end_iter, a_string, len(a_string))
+        self.m_view.scroll_mark_onscreen(self.m_buffer.get_insert())
         # Reset entry
         self.print_to_entry("")
 
@@ -186,7 +204,6 @@ class Console:
     def print_to_entry(self, text):
         self.m_entry.set_text(text)
 
-
 #------------------------------------------------------------------------------
     # def popup_dialog(self, message, expl):
     #     dialog = Gtk.MessageDialog(self.m_window, 0, Gtk.MessageType.INFO, Gtk.ButtonsType.OK, message)

+ 16 - 8
tools/gui/console/ui/console.glade

@@ -29,19 +29,27 @@
           </packing>
         </child>
         <child>
-          <object class="GtkTextView" id="textview1">
-            <property name="height_request">339</property>
+          <object class="GtkScrolledWindow" id="scrolledwindow1">
             <property name="visible">True</property>
             <property name="can_focus">True</property>
-            <property name="editable">False</property>
-            <property name="cursor_visible">False</property>
-            <property name="buffer">textbuffer1</property>
-            <property name="accepts_tab">False</property>
+            <property name="hscrollbar_policy">never</property>
+            <property name="vscrollbar_policy">always</property>
+            <property name="shadow_type">in</property>
+            <child>
+              <object class="GtkTextView" id="textview1">
+                <property name="height_request">339</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="editable">False</property>
+                <property name="cursor_visible">False</property>
+                <property name="buffer">textbuffer1</property>
+                <property name="accepts_tab">False</property>
+              </object>
+            </child>
           </object>
           <packing>
-            <property name="expand">False</property>
+            <property name="expand">True</property>
             <property name="fill">True</property>
-            <property name="pack_type">end</property>
             <property name="position">1</property>
           </packing>
         </child>