Console interface to Gerrit Code Review
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.4 KiB

# Copyright 2014 OpenStack Foundation
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import urwid
from gertty import mywid
from gertty.view import change as view_change
class ChangeRow(urwid.Button):
change_focus_map = {None: 'reversed',
'unreviewed-change': 'reversed-unreviewed-change',
'reviewed-change': 'reversed-reviewed-change',
def selectable(self):
return True
def __init__(self, change, callback=None):
super(ChangeRow, self).__init__('', on_press=callback, user_data=change.key)
self.change_key = change.key
self.subject = urwid.Text(u'', wrap='clip')
self.number = urwid.Text(u'')
cols = [(8, self.number), self.subject]
self.columns = urwid.Columns(cols)
self.row_style = urwid.AttrMap(self.columns, '')
self._w = urwid.AttrMap(self.row_style, None, focus_map=self.change_focus_map)
def update(self, change):
if change.reviewed:
style = 'reviewed-change'
style = 'unreviewed-change'
self.row_style.set_attr_map({None: style})
del self.columns.contents[2:]
for category in change.getCategories():
v = change.getMaxForCategory(category)
if v == 0:
v = ''
v = '%2i' % v
self.columns.contents.append((urwid.Text(v), self.columns.options('given', 3)))
class ChangeListHeader(urwid.WidgetWrap):
def __init__(self):
cols = [(8, urwid.Text(u'Number')), urwid.Text(u'Subject')]
super(ChangeListHeader, self).__init__(urwid.Columns(cols))
def update(self, change):
del self._w.contents[2:]
for category in change.getCategories():
self._w.contents.append((urwid.Text(' %s' % category[0]), self._w.options('given', 3)))
class ChangeListView(urwid.WidgetWrap):
help = mywid.GLOBAL_HELP + """
This Screen
<l> Toggle whether only unreviewed or all changes are displayed.
<r> Toggle the reviewed flag for the currently selected change.
def __init__(self, app, project_key):
super(ChangeListView, self).__init__(urwid.Pile([])) = app
self.project_key = project_key
self.unreviewed = True
self.change_rows = {}
self.listbox = urwid.ListBox(urwid.SimpleFocusListWalker([]))
self.header = ChangeListHeader()
self._w.contents.append((app.header, ('pack', 1)))
self._w.contents.append((urwid.Divider(), ('pack', 1)))
self._w.contents.append((urwid.AttrWrap(self.header, 'table-header'), ('pack', 1)))
self._w.contents.append((self.listbox, ('weight', 1)))
def refresh(self):
unseen_keys = set(self.change_rows.keys())
with as session:
project = session.getProject(self.project_key)
self.project_name =
if self.unreviewed:
self.title = u'Unreviewed changes in %s' %
lst = project.unreviewed_changes
self.title = u'Open changes in %s' %
lst = project.changes
i = 0
for change in lst:
row = self.change_rows.get(change.key)
if not row:
row = ChangeRow(change, self.onSelect)
self.listbox.body.insert(i, row)
self.change_rows[change.key] = row
i += 1
if project.changes:
for key in unseen_keys:
row = self.change_rows[key]
del self.change_rows[key]
def toggleReviewed(self, change_key):
with as session:
change = session.getChange(change_key)
change.reviewed = not change.reviewed
ret = change.reviewed
return ret
def keypress(self, size, key):
if key=='l':
self.unreviewed = not self.unreviewed
return None
if key=='r':
if not len(self.listbox.body):
return None
pos = self.listbox.focus_position
reviewed = self.toggleReviewed(self.listbox.body[pos].change_key)
return None
return super(ChangeListView, self).keypress(size, key)
def onSelect(self, button, change_key):, change_key))