Right align line numbers

And change the column width to 5.

With left-aligned numbers and a column width of 4, the only time
numbers would abut the content was with >= 4 digit numbers.  However,
with right alignment we need to format 1 space less than we want to
leave room for in the general case.  If we format for 3 digit numbers
in a 4 column space, then we end up with a line number misalignment
between 999 and 1000.  That's reasonably common in our files.

Instead, format for 4 digit numbers in a 5 column space, which means
that numbers will be correctly aligned through 9999, and only on line
10000 will the column shift and the right side of the number abut the
content (note, the content itself will still not shift).  That seems
a reasonable compromise since no one will actually be able to load
a file that large using the web interface anyway.

Change-Id: If2b81a215dc6537ded5098ddb96f86d54be12633
This commit is contained in:
James E. Blair 2015-03-12 14:15:22 -07:00 committed by James Polley
parent abfe8013bc
commit edf0322043
2 changed files with 16 additions and 12 deletions

View File

@ -22,6 +22,8 @@ from gertty import mywid
from gertty import gitrepo from gertty import gitrepo
from gertty.view.diff import * from gertty.view.diff import *
LN_COL_WIDTH = 5
class SideDiffCommentEdit(BaseDiffCommentEdit): class SideDiffCommentEdit(BaseDiffCommentEdit):
def __init__(self, app, context, old_key=None, new_key=None, old=u'', new=u''): def __init__(self, app, context, old_key=None, new_key=None, old=u'', new=u''):
super(SideDiffCommentEdit, self).__init__([]) super(SideDiffCommentEdit, self).__init__([])
@ -32,9 +34,9 @@ class SideDiffCommentEdit(BaseDiffCommentEdit):
self.new_key = new_key self.new_key = new_key
self.old = urwid.Edit(edit_text=old, multiline=True) self.old = urwid.Edit(edit_text=old, multiline=True)
self.new = urwid.Edit(edit_text=new, multiline=True) self.new = urwid.Edit(edit_text=new, multiline=True)
self.contents.append((urwid.Text(u''), ('given', 4, False))) self.contents.append((urwid.Text(u''), ('given', LN_COL_WIDTH, False)))
self.contents.append((urwid.AttrMap(self.old, 'draft-comment'), ('weight', 1, False))) self.contents.append((urwid.AttrMap(self.old, 'draft-comment'), ('weight', 1, False)))
self.contents.append((urwid.Text(u''), ('given', 4, False))) self.contents.append((urwid.Text(u''), ('given', LN_COL_WIDTH, False)))
self.contents.append((urwid.AttrMap(self.new, 'draft-comment'), ('weight', 1, False))) self.contents.append((urwid.AttrMap(self.new, 'draft-comment'), ('weight', 1, False)))
self.focus_position = 3 self.focus_position = 3
@ -60,9 +62,9 @@ class SideDiffComment(BaseDiffComment):
oldt = urwid.AttrMap(oldt, 'comment') oldt = urwid.AttrMap(oldt, 'comment')
if new: if new:
newt = urwid.AttrMap(newt, 'comment') newt = urwid.AttrMap(newt, 'comment')
self.contents.append((urwid.Text(u''), ('given', 4, False))) self.contents.append((urwid.Text(u''), ('given', LN_COL_WIDTH, False)))
self.contents.append((oldt, ('weight', 1, False))) self.contents.append((oldt, ('weight', 1, False)))
self.contents.append((urwid.Text(u''), ('given', 4, False))) self.contents.append((urwid.Text(u''), ('given', LN_COL_WIDTH, False)))
self.contents.append((newt, ('weight', 1, False))) self.contents.append((newt, ('weight', 1, False)))
class SideDiffLine(BaseDiffLine): class SideDiffLine(BaseDiffLine):
@ -74,13 +76,13 @@ class SideDiffLine(BaseDiffLine):
if ln is None: if ln is None:
ln = '' ln = ''
else: else:
ln = str(ln) ln = '%*i' % (LN_COL_WIDTH-1, ln)
ln_col = urwid.Text(('line-number', ln)) ln_col = urwid.Text(('line-number', ln))
ln_col.set_wrap_mode('clip') ln_col.set_wrap_mode('clip')
line_col = urwid.Text(line) line_col = urwid.Text(line)
if action == '': if action == '':
line_col = urwid.AttrMap(line_col, 'nonexistent') line_col = urwid.AttrMap(line_col, 'nonexistent')
columns += [(4, ln_col), line_col] columns += [(LN_COL_WIDTH, ln_col), line_col]
col = urwid.Columns(columns) col = urwid.Columns(columns)
map = {None: 'focused', map = {None: 'focused',
'added-line': 'focused-added-line', 'added-line': 'focused-added-line',

View File

@ -22,6 +22,8 @@ from gertty import mywid
from gertty import gitrepo from gertty import gitrepo
from gertty.view.diff import * from gertty.view.diff import *
LN_COL_WIDTH = 5
class UnifiedDiffCommentEdit(BaseDiffCommentEdit): class UnifiedDiffCommentEdit(BaseDiffCommentEdit):
def __init__(self, context, oldnew, key=None, comment=u''): def __init__(self, context, oldnew, key=None, comment=u''):
super(UnifiedDiffCommentEdit, self).__init__([]) super(UnifiedDiffCommentEdit, self).__init__([])
@ -53,11 +55,11 @@ class UnifiedDiffLine(BaseDiffLine):
if old_ln is None: if old_ln is None:
old_ln = '' old_ln = ''
else: else:
old_ln = str(old_ln) old_ln = '%*i' % (LN_COL_WIDTH-1, old_ln)
if new_ln is None: if new_ln is None:
new_ln = '' new_ln = ''
else: else:
new_ln = str(new_ln) new_ln = '%*i' % (LN_COL_WIDTH-1, new_ln)
old_ln_col = urwid.Text(('line-number', old_ln)) old_ln_col = urwid.Text(('line-number', old_ln))
old_ln_col.set_wrap_mode('clip') old_ln_col.set_wrap_mode('clip')
new_ln_col = urwid.Text(('line-number', new_ln)) new_ln_col = urwid.Text(('line-number', new_ln))
@ -65,13 +67,13 @@ class UnifiedDiffLine(BaseDiffLine):
if oldnew == gitrepo.OLD: if oldnew == gitrepo.OLD:
action = old_action action = old_action
line = old_line line = old_line
columns = [(4, old_ln_col), (4, urwid.Text(u''))] columns = [(LN_COL_WIDTH, old_ln_col), (LN_COL_WIDTH, urwid.Text(u''))]
elif oldnew == gitrepo.NEW: elif oldnew == gitrepo.NEW:
action = new_action action = new_action
line = new_line line = new_line
columns = [(4, urwid.Text(u'')), (4, new_ln_col)] columns = [(LN_COL_WIDTH, urwid.Text(u'')), (LN_COL_WIDTH, new_ln_col)]
if new_action == ' ': if new_action == ' ':
columns = [(4, old_ln_col), (4, new_ln_col)] columns = [(LN_COL_WIDTH, old_ln_col), (LN_COL_WIDTH, new_ln_col)]
line_col = urwid.Text(line) line_col = urwid.Text(line)
if action == '': if action == '':
line_col = urwid.AttrMap(line_col, 'nonexistent') line_col = urwid.AttrMap(line_col, 'nonexistent')
@ -97,7 +99,7 @@ class UnifiedFileHeader(BaseFileHeader):
urwid.Text(('filename', old))]) urwid.Text(('filename', old))])
elif oldnew == gitrepo.NEW: elif oldnew == gitrepo.NEW:
col = urwid.Columns([ col = urwid.Columns([
(4, urwid.Text(u'')), (LN_COL_WIDTH, urwid.Text(u'')),
urwid.Text(('filename', new))]) urwid.Text(('filename', new))])
map = {None: 'focused-filename', map = {None: 'focused-filename',
'filename': 'focused-filename'} 'filename': 'focused-filename'}