Browse files

Initial commit

  • Loading branch information...
0 parents commit 077c5acbee40a212944fa31b211cd35244baf1ac @quarnster committed Jan 29, 2012
Showing with 188 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +7 −0 Default.sublime-keymap
  3. +33 −0 adb.tmLanguage
  4. +144 −0 adbview.py
4 .gitignore
@@ -0,0 +1,4 @@
+*.cache
+*.pyc
+*.sublime-project
+*.sublime-workspace
7 Default.sublime-keymap
@@ -0,0 +1,7 @@
+[
+
+ {
+ "command": "adb_launch",
+ "keys": ["ctrl+d"]
+ }
+]
33 adb.tmLanguage
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>name</key>
+ <string>Android Debug Bridge</string>
+ <key>patterns</key>
+ <array>
+ <dict>
+ <key>match</key>
+ <string>^E.*</string>
+ <key>name</key>
+ <string>invalid.adb</string>
+ </dict>
+ <dict>
+ <key>match</key>
+ <string>^W.*</string>
+ <key>name</key>
+ <string>keyword</string>
+ </dict>
+ <dict>
+ <key>match</key>
+ <string>^D.*</string>
+ <key>name</key>
+ <string>comment</string>
+ </dict>
+ </array>
+ <key>scopeName</key>
+ <string>source.adb</string>
+ <key>uuid</key>
+ <string>95F39BAA-EBD3-468A-9965-2C47C3B30C11</string>
+</dict>
+</plist>
144 adbview.py
@@ -0,0 +1,144 @@
+import sublime, sublime_plugin
+import subprocess
+import Queue
+import threading
+import traceback
+
+
+class ADBView(object):
+ LINE = 0
+ FOLD_ALL = 1
+ CLEAR = 2
+ SCROLL = 3
+ VIEWPORT_POSITION = 4
+
+ def __init__(self, s=True, settingsprefix=None):
+ self.queue = Queue.Queue()
+ self.name = "ADB"
+ self.closed = True
+ self.doScroll = s
+ self.view = None
+ self.settingsprefix = settingsprefix
+
+ def is_open(self):
+ return not self.closed
+
+ def open(self):
+ if self.view == None or self.view.window() == None:
+ self.create_view()
+
+ def add_line(self, line):
+ if self.is_open():
+ self.queue.put((ADBView.LINE, line))
+ sublime.set_timeout(self.update, 0)
+
+ def scroll(self, line):
+ if self.is_open():
+ self.queue.put((ADBView.SCROLL, line))
+ sublime.set_timeout(self.update, 0)
+
+ def set_viewport_position(self, pos):
+ if self.is_open():
+ self.queue.put((ADBView.VIEWPORT_POSITION, pos))
+ sublime.set_timeout(self.update, 0)
+
+ def clear(self):
+ if self.is_open():
+ self.queue.put((ADBView.CLEAR, None))
+ sublime.set_timeout(self.update, 0)
+
+ def create_view(self):
+ self.view = sublime.active_window().new_file()
+ self.view.set_name(self.name)
+ self.view.set_scratch(True)
+ self.view.set_read_only(True)
+ self.view.set_syntax_file("Packages/ADBView/adb.tmLanguage")
+ self.closed = False
+
+ def is_closed(self):
+ return self.closed
+
+ def was_closed(self):
+ self.closed = True
+
+ def fold_all(self):
+ if self.is_open():
+ self.queue.put((ADBView.FOLD_ALL, None))
+
+ def get_view(self):
+ return self.view
+
+ def update(self):
+ if not self.is_open():
+ return
+ insert = ""
+ try:
+ while True:
+ cmd, data = self.queue.get_nowait()
+ if cmd == ADBView.LINE:
+ insert += data
+ elif cmd == ADBView.FOLD_ALL:
+ self.view.run_command("fold_all")
+ elif cmd == ADBView.CLEAR:
+ insert = ""
+ self.view.set_read_only(False)
+ e = self.view.begin_edit()
+ self.view.erase(e, sublime.Region(0, self.view.size()))
+ self.view.end_edit(e)
+ self.view.set_read_only(True)
+ elif cmd == ADBView.SCROLL:
+ self.view.run_command("goto_line", {"line": data + 1})
+ elif cmd == ADBView.VIEWPORT_POSITION:
+ self.view.set_viewport_position(data, True)
+ self.queue.task_done()
+ except Queue.Empty:
+ # get_nowait throws an exception when there's nothing..
+ pass
+ except:
+ traceback.print_exc()
+ finally:
+ if len(insert) > 0:
+ self.view.set_read_only(False)
+ e = self.view.begin_edit()
+ self.view.insert(e, self.view.size(), insert)
+ self.view.end_edit(e)
+ self.view.set_read_only(True)
+ if self.doScroll:
+ self.view.show(self.view.size())
+
+adb_view = ADBView()
+adb_process = None
+
+def output(pipe):
+ while True:
+ try:
+ if adb_process.poll() != None:
+ break
+ line = pipe.readline().strip()
+
+ if len(line) > 0:
+ adb_view.add_line("%s\n" % line)
+ except:
+ traceback.print_exc()
+
+
+class AdbLaunch(sublime_plugin.WindowCommand):
+ def run(self):
+ print "launch"
+ global adb_process
+ if adb_process == None or adb_process.poll() != None:
+ adb_process = subprocess.Popen(["adb", "logcat"], shell=False, stdout=subprocess.PIPE)
+ adb_view.open()
+ t = threading.Thread(target=output, args=(adb_process.stdout,))
+ t.start()
+
+ def is_enabled(self):
+ return True
+
+
+class AdbEventListener(sublime_plugin.EventListener):
+ def on_close(self, view):
+ if adb_view.is_open() and view.id() == adb_view.get_view().id():
+ adb_view.was_closed()
+ adb_process.kill()
+

0 comments on commit 077c5ac

Please sign in to comment.