Skip to content

Commit

Permalink
Add shard support and controls to the console
Browse files Browse the repository at this point in the history
  • Loading branch information
tedivm committed Aug 2, 2017
1 parent d2297fb commit 2d1a1ac
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 20 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ commands however.
* `list` - lists these and other built in commands and aliases.
* `pause` - disables autoscrolling (hit enter on an empty terminal to reenable).
* `reconnect` - reconnects the console to the Screeps server.
* `shard` - controls the active shards.
* `themes` - lists available themes when called without an argument, or switches.
Otherwise switches themes when called with the theme name as the first
argument.
Expand All @@ -110,18 +111,30 @@ Scrolling can be done one line at a time using `alt up` and `alt down`. Using
`PageUp` and `PageDown` (FN+up and FN+down on Apple) can be used to scroll
through the console buffer a bit quicker.

## Shards

By default all output from all shards is displayed and console commands go to
shard0. This can be changed with the `shard` commands.

* `shard` - by itself it outputs the shard that console input will go to.
* `shard SHARDNAME` - changes the shard that console input goes to.
* `shard focus SHARDNAME` - changes the console output and only displays
messages from this shard.
* `shard clear` - display all output from all shards, but leave the console
input pointed at the same shard.


## 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 its index.
`filter add REGEX` - add a regular expression to the filter list.
`filter clear` - remove all filters.
`filter contains STRING` - add a filter that looks for log lines that contain the
supplied string.
`filter remove INDEX` - remove a regular expression using it's index.
* `filter list` - this lists each current regex filter and its index.
* `filter add REGEX` - add a regular expression to the filter list.
* `filter clear` - remove all filters.
* `filter contains STRING` - add a filter that looks for log lines that contain
the supplied string.
* `filter remove INDEX` - remove a regular expression using it's index.


## Colors and Severity
Expand Down
39 changes: 35 additions & 4 deletions screeps_console/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


class Processor(object):
shard = 'shard0'
lastkey = False
apiclient = False
aliases = {
Expand Down Expand Up @@ -118,7 +119,7 @@ def onEnter(self, key):
self.listbox.scrollBottom()
userInput.set_edit_text('')
apiclient = self.getApiClient()
apiclient.console(user_text)
apiclient.console(user_text, self.shard)

def onTab(self, key):
self.autocomplete.complete()
Expand Down Expand Up @@ -266,6 +267,36 @@ def reconnect(self, comp):
comp.listwalker.append(urwid.Text(('logged_response', 'reconnecting')))
comp.listbox.autoscroll()

def shard(self, comp):
userInput = comp.edit
user_text = userInput.get_edit_text()
user_command_split = user_text.split(' ')

if len(user_command_split) < 2 or user_command_split[1] == 'current':
comp.listwalker.appendText(comp.shard)
return

if user_command_split[1] == 'clear':
comp.consolemonitor.focus = False
comp.listwalker.appendText('Clearing shard settings')
return

if user_command_split[1] == 'focus':
if len(user_command_split) < 3:
comp.consolemonitor.focus = False
comp.listwalker.appendText('Disabled shard focus')
else:
shard = user_command_split[2]
comp.consolemonitor.focus = shard
comp.shard = shard
message = 'Enabled shard focus on %s' % (shard)
comp.listwalker.appendText(message)
return

comp.shard = user_command_split[1]
response = 'Setting active shard to %s' % (comp.shard,)
comp.listwalker.appendText(response)

def themes(self, comp):
userInput = comp.edit
user_text = userInput.get_edit_text()
Expand All @@ -277,7 +308,7 @@ def themes(self, comp):
for theme in theme_names:
theme_list = theme + ' ' + theme_list

comp.listwalker.append(urwid.Text(('logged_response', theme_list)))
comp.listwalker.appendText(theme_list)
return

if user_command_split[1] == 'test':
Expand Down Expand Up @@ -323,8 +354,8 @@ def turtle(self, comp):
(_(__/ ./ / \_\ \.
(_(___/ \_____)_)
'''
comp.listwalker.append(urwid.Text(('logged_response', turtle)))
comp.listwalker.append(urwid.Text(('logged_response', '')))
comp.listwalker.appendText(turtle)
comp.listwalker.appendText('')

def whoami(self, comp):
me = comp.getApiClient().me()['username']
Expand Down
19 changes: 12 additions & 7 deletions screeps_console/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def on_message(self, ws, message):

data = json.loads(message)

if 'shard' in data[1]:
shard = data[1]['shard']
else:
shard = 'shard0'


if 'messages' in data[1]:
stream = []

Expand All @@ -57,7 +63,6 @@ def on_message(self, ws, message):
results = map(lambda x:'<type="result">'+x+'</type>',results)
stream = stream + results


message_count = len(stream)

if message_count > 0:
Expand All @@ -71,11 +76,11 @@ def on_message(self, ws, message):

for line in stream:
if self.format == 'color':
line_parsed = parseLine(line)
line_parsed = '%s: %s' % (shard, parseLine(line))
elif self.format == 'json':
line_parsed = json.dumps(line)
line_parsed = json.dumps({'line':line,'shard':shard})
else:
line_parsed = tagLine(line)
line_parsed = '%s: %s' % (shard, tagLine(line))

print line_parsed
sys.stdout.flush()
Expand All @@ -86,11 +91,11 @@ def on_message(self, ws, message):
#line = '<'+data[1]['error']
line = "<severity=\"5\" type=\"error\">" + data[1]['error'] + "</font>"
if self.format == 'color':
line_parsed = parseLine(line)
line_parsed = '%s: %s' % (shard, parseLine(line))
elif self.format == 'json':
line_parsed = json.dumps(line)
line_parsed = json.dumps({'line':line,'shard':shard})
else:
line_parsed = tagLine(line)
line_parsed = '%s: %s' % (shard, tagLine(line))

print line_parsed
return
Expand Down
18 changes: 15 additions & 3 deletions screeps_console/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ def scrollDown(self, quantity):


class consoleWalker(urwid.SimpleListWalker):

def appendText(self, message, format='logged_response'):
self.append(urwid.Text((format, message)))

def append(self, value):
if(len(self) >= self.max_buffer):
self.pop(0)
Expand Down Expand Up @@ -234,6 +238,7 @@ class ScreepsConsoleMonitor:

proc = False
quiet = False
focus = False
filters = []

def __init__(self, widget, walker, loop):
Expand Down Expand Up @@ -293,9 +298,16 @@ def onUpdate(self, data):
if len(line_json) <= 0:
continue
try:
line = json.loads(line_json.strip())
line_data = json.loads(line_json.strip())
line = line_data['line']
shard = line_data['shard']

if self.focus and self.focus != line_data['shard']:
continue


except:
print line_json
print line_json
logging.exception('error processing data: ' + line_json)
continue

Expand Down Expand Up @@ -339,7 +351,7 @@ def onUpdate(self, data):
if not has_match:
continue

self.walker.append(urwid.Text((formatting, line)))
self.walker.append(urwid.Text((formatting, '%s: %s' % (shard, line))))
self.widget.autoscroll()

except:
Expand Down

0 comments on commit 2d1a1ac

Please sign in to comment.