Skip to content

Commit

Permalink
Add regex filter support
Browse files Browse the repository at this point in the history
  • Loading branch information
tedivm committed May 31, 2016
1 parent 0353c73 commit cd16902
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,20 @@ Scrolling can be done one line at a time using `alt up` and `alt down`. Using
through the console buffer a bit quicker.


## Filters

Console output can be filtered using regular expressions and the `filter`
command. Only commands that match at least one filter will be displayed.

`filter list` - this lists each current regex filter and it's index.
`filter add` - add a regular expression to the filter list.
`filter remove` - remove a regular expression using it's index.


## Colors and Severity

Console output can have colors, in both the website version and the shell. To get
the best of both worlds use font tags that also have a severity attribute.
Console output can have colors, in both the website version and the shell. To
get the best of both worlds use font tags that also have a severity attribute.

```
<font color="#999999" severity="2">Message goes here!</font>
Expand Down
39 changes: 37 additions & 2 deletions screeps_console/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ def __init__(self):
self.lastkeytime = 0
self.listbox = False
self.listwalker = False
self.consolemonitor = False
self.edit = False
self.getApiClient()
self.autocomplete = Autocomplete(self)

def setDisplayWidgets(self, loop, frame, listbox, listwalker, edit):
self.listbox = listbox
def setDisplayWidgets(self, loop, frame, listbox, listwalker, edit, consolemonitor):
self.listbox = listbox # console
self.listwalker = listwalker
self.edit = edit
self.loop = loop
self.consolemonitor = consolemonitor

def getApiClient(self):
if not self.apiclient:
Expand Down Expand Up @@ -159,6 +161,39 @@ def clear(self, comp):
def exit(self, comp):
raise urwid.ExitMainLoop()

def filter(self, comp):

user_text = comp.edit.get_edit_text()
user_command_split = user_text.split(' ')

if len(user_command_split) <= 1:
subcommand = 'list'
else:
subcommand = user_command_split[1]

if subcommand == 'list':
filters = comp.consolemonitor.filters[:]

if len(filters) <= 0:
comp.listwalker.append(urwid.Text(('logged_response', 'No filters')))
comp.listbox.autoscroll()
return
else:
for index, pattern in enumerate(filters):
comp.listwalker.append(urwid.Text(('logged_response', str(index) + '. ' + pattern)))
comp.listbox.autoscroll()
return

elif subcommand == 'add':
regex = user_command_split[2:]
comp.consolemonitor.filters.append(' '.join(regex))

elif subcommand == 'remove':
toRemove = user_command_split[2]
comp.consolemonitor.filters.pop(int(toRemove))
pass


def list(self, comp):
command_list = ''
aliases = list(comp.aliases)
Expand Down
35 changes: 32 additions & 3 deletions screeps_console/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import atexit
import command
import json
import logging
import os
from os.path import expanduser
import outputparser
import re
from settings import getSettings
import signal
import subprocess
Expand All @@ -19,14 +21,17 @@ class ScreepsInteractiveConsole:
consoleWidget = False
listWalker = False
userInput = False
consoleMonitor = False

def __init__(self):

frame = self.getFrame()
comp = self.getCommandProcessor()
self.loop = urwid.MainLoop(urwid.AttrMap(frame, 'bg'), unhandled_input=comp.onInput, palette=themes['dark'])
comp.setDisplayWidgets(self.loop, frame, self.getConsole(), self.getConsoleListWalker(), self.getEdit())
console_monitor = ScreepsConsoleMonitor(self.consoleWidget, self.listWalker, self.loop)
self.consoleMonitor = ScreepsConsoleMonitor(self.consoleWidget, self.listWalker, self.loop)

comp.setDisplayWidgets(self.loop, frame, self.getConsole(), self.getConsoleListWalker(), self.getEdit(), self.consoleMonitor)

self.loop.run()


Expand Down Expand Up @@ -214,6 +219,7 @@ def keypress(self, size, key):
class ScreepsConsoleMonitor:

proc = False
filters = []

def __init__(self, widget, walker, loop):
self.widget = widget
Expand Down Expand Up @@ -249,6 +255,10 @@ def onUpdate(self, data):
data_lines = data.rstrip().split('\n')
for line_json in data_lines:
try:

if len(line_json) <= 0:
continue

line = json.loads(line_json.strip())
log_type = outputparser.getType(line)

Expand All @@ -266,10 +276,29 @@ def onUpdate(self, data):

line = line.replace('&#09;', " ")
line = outputparser.clearTags(line)

if log_type == 'log' and len(self.filters) > 0:
has_match = False

for pattern in self.filters:
try:
match = re.search(pattern, line)
except:
e = sys.exc_info()[0]
logging.exception('dammit')

if match is not None:
has_match = True
break

if not has_match:
continue

self.walker.append(urwid.Text((formatting, line)))
self.widget.autoscroll()

except:
''
logging.exception('error processing data')

def __del__(self):
if self.proc:
Expand Down

0 comments on commit cd16902

Please sign in to comment.