##// END OF EJS Templates
py3: add b'' prefixes in tests/test-parseindex.t...
py3: add b'' prefixes in tests/test-parseindex.t # skip-blame because just b'' prefixes Differential Revision: https://phab.mercurial-scm.org/D3611

File last commit:

r38093:1978abdb default
r38098:307b19c7 default
Show More
crecord.py
1782 lines | 66.1 KiB | text/x-python | PythonLexer
Laurent Charignon
record: add crecord's ui logic to core...
r24310 # stuff related specifically to patch manipulation / parsing
#
# Copyright 2008 Mark Edgington <edgimar@gmail.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
#
# This code is based on the Mark Edgington's crecord extension.
# (Itself based on Bryan O'Sullivan's record extension.)
Gregory Szorc
crecord: use absolute_import
r25940 from __future__ import absolute_import
Laurent Charignon
record: add crecord's ui logic to core...
r24310
Gregory Szorc
crecord: use absolute_import
r25940 import locale
import os
import re
import signal
from .i18n import _
from . import (
encoding,
Pierre-Yves David
error: get Abort from 'error' instead of 'util'...
r26587 error,
Gregory Szorc
crecord: use absolute_import
r25940 patch as patchmod,
Yuya Nishihara
crecord: use scmutil.termsize()
r30315 scmutil,
Laurent Charignon
commit: add amend mode for commit -i...
r27156 util,
Gregory Szorc
crecord: use absolute_import
r25940 )
Yuya Nishihara
stringutil: bulk-replace call sites to point to new module...
r37102 from .utils import (
stringutil,
)
timeless
pycompat: switch to util.stringio for py3 compat
r28861 stringio = util.stringio
Matt Mackall
crecord: more import style
r24314
Laurent Charignon
record: add crecord's ui logic to core...
r24310 # This is required for ncurses to display non-ASCII characters in default user
# locale encoding correctly. --immerrr
Pulkit Goyal
py3: use unicode literals in crecord.py
r29697 locale.setlocale(locale.LC_ALL, u'')
Matt Mackall
crecord: fiddle with vertical whitespace
r24317
Jordi Gutiérrez Hermoso
crecord: break out the help message for editing hunks...
r28637 # patch comments based on the git one
diffhelptext = _("""# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed from the patch.
""")
hunkhelptext = _("""#
# If the patch applies cleanly, the edited hunk will immediately be
# added to the record list. If it does not apply cleanly, a rejects file
# will be generated. You can use that when you try again. If all lines
# of the hunk are removed, then the edit is aborted and the hunk is left
# unchanged.
""")
Jordi Gutiérrez Hermoso
crecord: re-enable reviewing a patch before comitting it...
r28638
patchhelptext = _("""#
# If the patch applies cleanly, the edited patch will immediately
# be finalised. If it does not apply cleanly, rejects files will be
# generated. You can use those when you try again.
""")
Sean Farley
crecord: use try/except for import of curses...
r27528 try:
Matt Harbison
crecord: fix mixed imports warning
r24909 import curses
Sean Farley
crecord: use try/except for import of curses...
r27528 curses.error
except ImportError:
Laurent Charignon
record: add crecord's ui logic to core...
r24310 # I have no idea if wcurses works with crecord...
Matt Harbison
crecord: conditionalize the imports that are not available on Windows...
r24423 try:
import wcurses as curses
Sean Farley
crecord: use try/except for import of curses...
r27528 curses.error
Matt Harbison
crecord: conditionalize the imports that are not available on Windows...
r24423 except ImportError:
Sean Farley
crecord: ensure that curses is False if not imported...
r27530 # wcurses is not shipped on Windows by default, or python is not
# compiled with curses
curses = False
Laurent Charignon
record: add crecord's ui logic to core...
r24310
Kyle Lippincott
crecord: fallback to text mode if diffs are too big for curses mode...
r38058 class fallbackerror(error.Abort):
"""Error that indicates the client should try to fallback to text mode."""
# Inherits from error.Abort so that existing behavior is preserved if the
# calling code does not know how to fallback.
Sean Farley
crecord: add helper function to determine if we should use curses...
r27529 def checkcurses(ui):
"""Return True if the user wants to use curses
This method returns True if curses is found (and that python is built with
it) and that the user has the correct flag for the ui.
"""
Simon Farnsworth
crecord: use ui.interface to choose curses interface...
r28543 return curses and ui.interface("chunkselector") == "curses"
Sean Farley
crecord: add helper function to determine if we should use curses...
r27529
Laurent Charignon
record: add crecord's ui logic to core...
r24310 class patchnode(object):
"""abstract class for patch graph nodes
(i.e. patchroot, header, hunk, hunkline)
"""
def firstchild(self):
raise NotImplementedError("method must be implemented by subclass")
def lastchild(self):
raise NotImplementedError("method must be implemented by subclass")
def allchildren(self):
"Return a list of all of the direct children of this node"
raise NotImplementedError("method must be implemented by subclass")
av6
crecord: add/remove blank lines (coding style)
r29076
Laurent Charignon
record: add crecord's ui logic to core...
r24310 def nextsibling(self):
"""
Return the closest next item of the same type where there are no items
of different types between the current item and this closest item.
If no such item exists, return None.
"""
raise NotImplementedError("method must be implemented by subclass")
def prevsibling(self):
"""
Return the closest previous item of the same type where there are no
items of different types between the current item and this closest item.
If no such item exists, return None.
"""
raise NotImplementedError("method must be implemented by subclass")
def parentitem(self):
raise NotImplementedError("method must be implemented by subclass")
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 def nextitem(self, skipfolded=True):
Laurent Charignon
record: add crecord's ui logic to core...
r24310 """
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 Try to return the next item closest to this item, regardless of item's
type (header, hunk, or hunkline).
Laurent Charignon
record: add crecord's ui logic to core...
r24310
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 If skipfolded == True, and the current item is folded, then the child
Laurent Charignon
record: add crecord's ui logic to core...
r24310 items that are hidden due to folding will be skipped when determining
the next item.
If it is not possible to get the next item, return None.
"""
try:
itemfolded = self.folded
except AttributeError:
itemfolded = False
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 if skipfolded and itemfolded:
Laurent Charignon
record: add crecord's ui logic to core...
r24310 nextitem = self.nextsibling()
if nextitem is None:
try:
nextitem = self.parentitem().nextsibling()
except AttributeError:
nextitem = None
return nextitem
else:
# try child
item = self.firstchild()
if item is not None:
return item
# else try next sibling
item = self.nextsibling()
if item is not None:
return item
try:
# else try parent's next sibling
item = self.parentitem().nextsibling()
if item is not None:
return item
# else return grandparent's next sibling (or None)
return self.parentitem().parentitem().nextsibling()
except AttributeError: # parent and/or grandparent was None
return None
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 def previtem(self):
Laurent Charignon
record: add crecord's ui logic to core...
r24310 """
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 Try to return the previous item closest to this item, regardless of
item's type (header, hunk, or hunkline).
Laurent Charignon
record: add crecord's ui logic to core...
r24310
If it is not possible to get the previous item, return None.
"""
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 # try previous sibling's last child's last child,
# else try previous sibling's last child, else try previous sibling
prevsibling = self.prevsibling()
if prevsibling is not None:
prevsiblinglastchild = prevsibling.lastchild()
if ((prevsiblinglastchild is not None) and
not prevsibling.folded):
prevsiblinglclc = prevsiblinglastchild.lastchild()
if ((prevsiblinglclc is not None) and
not prevsiblinglastchild.folded):
return prevsiblinglclc
Laurent Charignon
record: add crecord's ui logic to core...
r24310 else:
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 return prevsiblinglastchild
else:
return prevsibling
Laurent Charignon
record: add crecord's ui logic to core...
r24310
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 # try parent (or None)
return self.parentitem()
Laurent Charignon
record: add crecord's ui logic to core...
r24310
class patch(patchnode, list): # todo: rename patchroot
"""
list of header objects representing the patch.
"""
def __init__(self, headerlist):
self.extend(headerlist)
# add parent patch object reference to each header
for header in self:
header.patch = self
class uiheader(patchnode):
"""patch header
Mads Kiilerich
spelling: trivial spell checking
r26781 xxx shouldn't we move this to mercurial/patch.py ?
Laurent Charignon
record: add crecord's ui logic to core...
r24310 """
def __init__(self, header):
self.nonuiheader = header
# flag to indicate whether to apply this chunk
self.applied = True
# flag which only affects the status display indicating if a node's
# children are partially applied (i.e. some applied, some not).
self.partial = False
# flag to indicate whether to display as folded/unfolded to user
self.folded = True
# list of all headers in patch
self.patch = None
# flag is False if this header was ever unfolded from initial state
self.neverunfolded = True
self.hunks = [uihunk(h, self) for h in self.hunks]
def prettystr(self):
timeless
pycompat: switch to util.stringio for py3 compat
r28861 x = stringio()
Laurent Charignon
record: add crecord's ui logic to core...
r24310 self.pretty(x)
return x.getvalue()
def nextsibling(self):
numheadersinpatch = len(self.patch)
indexofthisheader = self.patch.index(self)
if indexofthisheader < numheadersinpatch - 1:
nextheader = self.patch[indexofthisheader + 1]
return nextheader
else:
return None
def prevsibling(self):
indexofthisheader = self.patch.index(self)
if indexofthisheader > 0:
previousheader = self.patch[indexofthisheader - 1]
return previousheader
else:
return None
def parentitem(self):
"""
there is no 'real' parent item of a header that can be selected,
so return None.
"""
return None
def firstchild(self):
"return the first child of this item, if one exists. otherwise None."
if len(self.hunks) > 0:
return self.hunks[0]
else:
return None
def lastchild(self):
"return the last child of this item, if one exists. otherwise None."
if len(self.hunks) > 0:
return self.hunks[-1]
else:
return None
def allchildren(self):
"return a list of all of the direct children of this node"
return self.hunks
def __getattr__(self, name):
return getattr(self.nonuiheader, name)
class uihunkline(patchnode):
"represents a changed line in a hunk"
def __init__(self, linetext, hunk):
self.linetext = linetext
self.applied = True
# the parent hunk to which this line belongs
self.hunk = hunk
# folding lines currently is not used/needed, but this flag is needed
# in the previtem method.
self.folded = False
def prettystr(self):
return self.linetext
def nextsibling(self):
numlinesinhunk = len(self.hunk.changedlines)
indexofthisline = self.hunk.changedlines.index(self)
if (indexofthisline < numlinesinhunk - 1):
nextline = self.hunk.changedlines[indexofthisline + 1]
return nextline
else:
return None
def prevsibling(self):
indexofthisline = self.hunk.changedlines.index(self)
if indexofthisline > 0:
previousline = self.hunk.changedlines[indexofthisline - 1]
return previousline
else:
return None
def parentitem(self):
"return the parent to the current item"
return self.hunk
def firstchild(self):
"return the first child of this item, if one exists. otherwise None."
# hunk-lines don't have children
return None
def lastchild(self):
"return the last child of this item, if one exists. otherwise None."
# hunk-lines don't have children
return None
class uihunk(patchnode):
"""ui patch hunk, wraps a hunk and keep track of ui behavior """
maxcontext = 3
def __init__(self, hunk, header):
self._hunk = hunk
self.changedlines = [uihunkline(line, self) for line in hunk.hunk]
self.header = header
# used at end for detecting how many removed lines were un-applied
self.originalremoved = self.removed
# flag to indicate whether to display as folded/unfolded to user
self.folded = True
# flag to indicate whether to apply this chunk
self.applied = True
# flag which only affects the status display indicating if a node's
# children are partially applied (i.e. some applied, some not).
self.partial = False
def nextsibling(self):
numhunksinheader = len(self.header.hunks)
indexofthishunk = self.header.hunks.index(self)
if (indexofthishunk < numhunksinheader - 1):
nexthunk = self.header.hunks[indexofthishunk + 1]
return nexthunk
else:
return None
def prevsibling(self):
indexofthishunk = self.header.hunks.index(self)
if indexofthishunk > 0:
previoushunk = self.header.hunks[indexofthishunk - 1]
return previoushunk
else:
return None
def parentitem(self):
"return the parent to the current item"
return self.header
def firstchild(self):
"return the first child of this item, if one exists. otherwise None."
if len(self.changedlines) > 0:
return self.changedlines[0]
else:
return None
def lastchild(self):
"return the last child of this item, if one exists. otherwise None."
if len(self.changedlines) > 0:
return self.changedlines[-1]
else:
return None
def allchildren(self):
"return a list of all of the direct children of this node"
return self.changedlines
av6
crecord: add/remove blank lines (coding style)
r29076
Laurent Charignon
record: add crecord's ui logic to core...
r24310 def countchanges(self):
"""changedlines -> (n+,n-)"""
add = len([l for l in self.changedlines if l.applied
and l.prettystr()[0] == '+'])
rem = len([l for l in self.changedlines if l.applied
and l.prettystr()[0] == '-'])
return add, rem
def getfromtoline(self):
# calculate the number of removed lines converted to context lines
removedconvertedtocontext = self.originalremoved - self.removed
contextlen = (len(self.before) + len(self.after) +
removedconvertedtocontext)
timeless
crecord: properly handle files with No newline at eof (issue5268)...
r29862 if self.after and self.after[-1] == '\\ No newline at end of file\n':
Laurent Charignon
record: add crecord's ui logic to core...
r24310 contextlen -= 1
fromlen = contextlen + self.removed
tolen = contextlen + self.added
# diffutils manual, section "2.2.2.2 detailed description of unified
# format": "an empty hunk is considered to end at the line that
# precedes the hunk."
#
# so, if either of hunks is empty, decrease its line start. --immerrr
# but only do this if fromline > 0, to avoid having, e.g fromline=-1.
fromline, toline = self.fromline, self.toline
if fromline != 0:
if fromlen == 0:
fromline -= 1
if tolen == 0:
toline -= 1
fromtoline = '@@ -%d,%d +%d,%d @@%s\n' % (
fromline, fromlen, toline, tolen,
self.proc and (' ' + self.proc))
return fromtoline
def write(self, fp):
# updated self.added/removed, which are used by getfromtoline()
self.added, self.removed = self.countchanges()
fp.write(self.getfromtoline())
hunklinelist = []
# add the following to the list: (1) all applied lines, and
# (2) all unapplied removal lines (convert these to context lines)
for changedline in self.changedlines:
changedlinestr = changedline.prettystr()
if changedline.applied:
hunklinelist.append(changedlinestr)
elif changedlinestr[0] == "-":
hunklinelist.append(" " + changedlinestr[1:])
fp.write(''.join(self.before + hunklinelist + self.after))
pretty = write
def prettystr(self):
timeless
pycompat: switch to util.stringio for py3 compat
r28861 x = stringio()
Laurent Charignon
record: add crecord's ui logic to core...
r24310 self.pretty(x)
return x.getvalue()
Jun Wu
patch: rewrite reversehunks (issue5337)...
r32979 def reversehunk(self):
"""return a recordhunk which is the reverse of the hunk
Assuming the displayed patch is diff(A, B) result. The returned hunk is
intended to be applied to B, instead of A.
For example, when A is "0\n1\n2\n6\n" and B is "0\n3\n4\n5\n6\n", and
the user made the following selection:
0
[x] -1 [x]: selected
[ ] -2 [ ]: not selected
[x] +3
[ ] +4
[x] +5
6
This function returns a hunk like:
0
-3
-4
-5
+1
+4
6
Note "4" was first deleted then added. That's because "4" exists in B
side and "-4" must exist between "-3" and "-5" to make the patch
applicable to B.
"""
dels = []
adds = []
for line in self.changedlines:
text = line.linetext
if line.applied:
if text[0] == '+':
dels.append(text[1:])
elif text[0] == '-':
adds.append(text[1:])
elif text[0] == '+':
dels.append(text[1:])
adds.append(text[1:])
hunk = ['-%s' % l for l in dels] + ['+%s' % l for l in adds]
h = self._hunk
return patchmod.recordhunk(h.header, h.toline, h.fromline, h.proc,
h.before, hunk, h.after)
Laurent Charignon
record: add crecord's ui logic to core...
r24310 def __getattr__(self, name):
return getattr(self._hunk, name)
av6
crecord: add/remove blank lines (coding style)
r29076
Laurent Charignon
record: add crecord's ui logic to core...
r24310 def __repr__(self):
return '<hunk %r@%d>' % (self.filename(), self.fromline)
Jun Wu
crecord: add an "operation" field...
r30533 def filterpatch(ui, chunks, chunkselector, operation=None):
Laurent Charignon
record: add crecord's ui logic to core...
r24310 """interactively filter patch chunks into applied-only chunks"""
chunks = list(chunks)
# convert chunks list into structure suitable for displaying/modifying
# with curses. create a list of headers only.
headers = [c for c in chunks if isinstance(c, patchmod.header)]
# if there are no changed files
if len(headers) == 0:
Laurent Charignon
crecord: add dictionary to default return value of filterpatch...
r27321 return [], {}
Laurent Charignon
record: add crecord's ui logic to core...
r24310 uiheaders = [uiheader(h) for h in headers]
# let user choose headers/hunks/lines, and mark their applied flags
# accordingly
Jun Wu
crecord: add an "operation" field...
r30533 ret = chunkselector(ui, uiheaders, operation=operation)
Laurent Charignon
record: add crecord's ui logic to core...
r24310 appliedhunklist = []
for hdr in uiheaders:
if (hdr.applied and
(hdr.special() or len([h for h in hdr.hunks if h.applied]) > 0)):
appliedhunklist.append(hdr)
fixoffset = 0
for hnk in hdr.hunks:
if hnk.applied:
appliedhunklist.append(hnk)
# adjust the 'to'-line offset of the hunk to be correct
# after de-activating some of the other hunks for this file
if fixoffset:
#hnk = copy.copy(hnk) # necessary??
hnk.toline += fixoffset
else:
fixoffset += hnk.removed - hnk.added
Laurent Charignon
commit: add a way to return more information from the chunkselector...
r27155 return (appliedhunklist, ret)
Laurent Charignon
record: add crecord's ui logic to core...
r24310
Jun Wu
crecord: add an "operation" field...
r30533 def chunkselector(ui, headerlist, operation=None):
Laurent Charignon
record: add crecord's ui logic to core...
r24310 """
curses interface to get selection of chunks, and mark the applied flags
of the chosen chunks.
"""
Laurent Charignon
record: add message when starting record's curses interface...
r24779 ui.write(_('starting interactive selection\n'))
Jun Wu
crecord: add an "operation" field...
r30533 chunkselector = curseschunkselector(headerlist, ui, operation)
Pierre-Yves David
crecord: avoid setting non-existing SIGTSTP signal on windows (issue5512)...
r31933 origsigtstp = sentinel = object()
if util.safehasattr(signal, 'SIGTSTP'):
origsigtstp = signal.getsignal(signal.SIGTSTP)
Pierre-Yves David
crecord: ensure we reinstall the SIGTSTP handler...
r31932 try:
curses.wrapper(chunkselector.main)
Kyle Lippincott
crecord: fallback to text mode if diffs are too big for curses mode...
r38058 if chunkselector.initexc is not None:
raise chunkselector.initexc
Pierre-Yves David
crecord: ensure we reinstall the SIGTSTP handler...
r31932 # ncurses does not restore signal handler for SIGTSTP
finally:
Pierre-Yves David
crecord: avoid setting non-existing SIGTSTP signal on windows (issue5512)...
r31933 if origsigtstp is not sentinel:
signal.signal(signal.SIGTSTP, origsigtstp)
Laurent Charignon
commit: add a way to return more information from the chunkselector...
r27155 return chunkselector.opts
Laurent Charignon
record: add crecord's ui logic to core...
r24310
def testdecorator(testfn, f):
def u(*args, **kwargs):
return f(testfn, *args, **kwargs)
return u
Jun Wu
crecord: add an "operation" field...
r30533 def testchunkselector(testfn, ui, headerlist, operation=None):
Laurent Charignon
record: add crecord's ui logic to core...
r24310 """
test interface to get selection of chunks, and mark the applied flags
of the chosen chunks.
"""
Jun Wu
crecord: add an "operation" field...
r30533 chunkselector = curseschunkselector(headerlist, ui, operation)
Laurent Charignon
record: add crecord's ui logic to core...
r24310 if testfn and os.path.exists(testfn):
Pulkit Goyal
py3: make sure we open files in bytes mode...
r38093 testf = open(testfn, 'rb')
Pulkit Goyal
py3: convert a map expression into list comprehension...
r36454 testcommands = [x.rstrip('\n') for x in testf.readlines()]
Laurent Charignon
record: add crecord's ui logic to core...
r24310 testf.close()
while True:
if chunkselector.handlekeypressed(testcommands.pop(0), test=True):
break
Laurent Charignon
commit: add a way to return more information from the chunkselector...
r27155 return chunkselector.opts
Laurent Charignon
record: add crecord's ui logic to core...
r24310
Jun Wu
crecord: change the verb according to the operation...
r30548 _headermessages = { # {operation: text}
Jun Wu
crecord: fix revert -ir '.^' crash caused by 3649c3f2cd...
r35067 'apply': _('Select hunks to apply'),
Jun Wu
crecord: change the verb according to the operation...
r30548 'discard': _('Select hunks to discard'),
None: _('Select hunks to record'),
}
Laurent Charignon
record: add crecord's ui logic to core...
r24310 class curseschunkselector(object):
Jun Wu
crecord: add an "operation" field...
r30533 def __init__(self, headerlist, ui, operation=None):
Laurent Charignon
record: add crecord's ui logic to core...
r24310 # put the headers into a patch object
self.headerlist = patch(headerlist)
self.ui = ui
Laurent Charignon
commit: add a way to return more information from the chunkselector...
r27155 self.opts = {}
Laurent Charignon
record: add crecord's ui logic to core...
r24310
Laurent Charignon
crecord: add mechanism for error reporting...
r25556 self.errorstr = None
Laurent Charignon
record: add crecord's ui logic to core...
r24310 # list of all chunks
self.chunklist = []
for h in headerlist:
self.chunklist.append(h)
self.chunklist.extend(h.hunks)
# dictionary mapping (fgcolor, bgcolor) pairs to the
# corresponding curses color-pair value.
self.colorpairs = {}
# maps custom nicknames of color-pairs to curses color-pair values
self.colorpairnames = {}
Elmar Bartel
crecord: honor "ui.color = no" config option...
r35545 # Honor color setting of ui section. Keep colored setup as
# long as not explicitly set to a falsy value - especially,
# when not set at all. This is to stay most compatible with
# previous (color only) behaviour.
Yuya Nishihara
stringutil: bulk-replace call sites to point to new module...
r37102 uicolor = stringutil.parsebool(self.ui.config('ui', 'color'))
Elmar Bartel
crecord: honor "ui.color = no" config option...
r35545 self.usecolor = uicolor is not False
Laurent Charignon
record: add crecord's ui logic to core...
r24310 # the currently selected header, hunk, or hunk-line
self.currentselecteditem = self.headerlist[0]
# updated when printing out patch-display -- the 'lines' here are the
# line positions *in the pad*, not on the screen.
self.selecteditemstartline = 0
self.selecteditemendline = None
# define indentation levels
self.headerindentnumchars = 0
self.hunkindentnumchars = 3
self.hunklineindentnumchars = 6
# the first line of the pad to print to the screen
self.firstlineofpadtoprint = 0
# keeps track of the number of lines in the pad
self.numpadlines = None
Jun Wu
crecord: make _getstatuslines update numstatuslines...
r30545 self.numstatuslines = 1
Laurent Charignon
record: add crecord's ui logic to core...
r24310
# keep a running count of the number of lines printed to the pad
# (used for determining when the selected item begins/ends)
self.linesprintedtopadsofar = 0
# the first line of the pad which is visible on the screen
self.firstlineofpadtoprint = 0
# stores optional text for a commit comment provided by the user
self.commenttext = ""
# if the last 'toggle all' command caused all changes to be applied
self.waslasttoggleallapplied = True
Jun Wu
crecord: add an "operation" field...
r30533 # affects some ui text
Jun Wu
crecord: change the verb according to the operation...
r30548 if operation not in _headermessages:
Jun Wu
crecord: use ProgrammingError
r31649 raise error.ProgrammingError('unexpected operation: %s' % operation)
Jun Wu
crecord: add an "operation" field...
r30533 self.operation = operation
Laurent Charignon
record: add crecord's ui logic to core...
r24310 def uparrowevent(self):
"""
try to select the previous item to the current item that has the
most-indented level. for example, if a hunk is selected, try to select
the last hunkline of the hunk prior to the selected hunk. or, if
the first hunkline of a hunk is currently selected, then select the
hunk itself.
"""
currentitem = self.currentselecteditem
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 nextitem = currentitem.previtem()
Laurent Charignon
record: add crecord's ui logic to core...
r24310
if nextitem is None:
# if no parent item (i.e. currentitem is the first header), then
# no change...
nextitem = currentitem
self.currentselecteditem = nextitem
def uparrowshiftevent(self):
"""
select (if possible) the previous item on the same level as the
currently selected item. otherwise, select (if possible) the
parent-item of the currently selected item.
"""
currentitem = self.currentselecteditem
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 nextitem = currentitem.prevsibling()
# if there's no previous sibling, try choosing the parent
Laurent Charignon
record: add crecord's ui logic to core...
r24310 if nextitem is None:
nextitem = currentitem.parentitem()
if nextitem is None:
# if no parent item (i.e. currentitem is the first header), then
# no change...
nextitem = currentitem
self.currentselecteditem = nextitem
def downarrowevent(self):
"""
try to select the next item to the current item that has the
most-indented level. for example, if a hunk is selected, select
the first hunkline of the selected hunk. or, if the last hunkline of
a hunk is currently selected, then select the next hunk, if one exists,
or if not, the next header if one exists.
"""
#self.startprintline += 1 #debug
currentitem = self.currentselecteditem
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 nextitem = currentitem.nextitem()
Laurent Charignon
record: add crecord's ui logic to core...
r24310 # if there's no next item, keep the selection as-is
if nextitem is None:
nextitem = currentitem
self.currentselecteditem = nextitem
def downarrowshiftevent(self):
"""
av6
crecord: update downarrowshiftevent() docstring, remove todo...
r29081 select (if possible) the next item on the same level as the currently
selected item. otherwise, select (if possible) the next item on the
same level as the parent item of the currently selected item.
Laurent Charignon
record: add crecord's ui logic to core...
r24310 """
currentitem = self.currentselecteditem
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 nextitem = currentitem.nextsibling()
# if there's no next sibling, try choosing the parent's nextsibling
Laurent Charignon
record: add crecord's ui logic to core...
r24310 if nextitem is None:
try:
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 nextitem = currentitem.parentitem().nextsibling()
Laurent Charignon
record: add crecord's ui logic to core...
r24310 except AttributeError:
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 # parentitem returned None, so nextsibling() can't be called
Laurent Charignon
record: add crecord's ui logic to core...
r24310 nextitem = None
if nextitem is None:
av6
crecord: call prevsibling() and nextsibling() directly...
r29130 # if parent has no next sibling, then no change...
Laurent Charignon
record: add crecord's ui logic to core...
r24310 nextitem = currentitem
self.currentselecteditem = nextitem
def rightarrowevent(self):
"""
select (if possible) the first of this item's child-items.
"""
currentitem = self.currentselecteditem
nextitem = currentitem.firstchild()
# turn off folding if we want to show a child-item
if currentitem.folded:
self.togglefolded(currentitem)
if nextitem is None:
# if no next item on parent-level, then no change...
nextitem = currentitem
self.currentselecteditem = nextitem
def leftarrowevent(self):
"""
if the current item can be folded (i.e. it is an unfolded header or
hunk), then fold it. otherwise try select (if possible) the parent
of this item.
"""
currentitem = self.currentselecteditem
# try to fold the item
if not isinstance(currentitem, uihunkline):
if not currentitem.folded:
self.togglefolded(item=currentitem)
return
# if it can't be folded, try to select the parent item
nextitem = currentitem.parentitem()
if nextitem is None:
# if no item on parent-level, then no change...
nextitem = currentitem
if not nextitem.folded:
self.togglefolded(item=nextitem)
self.currentselecteditem = nextitem
def leftarrowshiftevent(self):
"""
select the header of the current item (or fold current item if the
current item is already a header).
"""
currentitem = self.currentselecteditem
if isinstance(currentitem, uiheader):
if not currentitem.folded:
self.togglefolded(item=currentitem)
return
# select the parent item recursively until we're at a header
while True:
nextitem = currentitem.parentitem()
if nextitem is None:
break
else:
currentitem = nextitem
self.currentselecteditem = currentitem
def updatescroll(self):
"scroll the screen to fully show the currently-selected"
selstart = self.selecteditemstartline
selend = self.selecteditemendline
Nathan Goldbaum
crecord: delete commented line
r29942
Laurent Charignon
record: add crecord's ui logic to core...
r24310 padstart = self.firstlineofpadtoprint
padend = padstart + self.yscreensize - self.numstatuslines - 1
# 'buffered' pad start/end values which scroll with a certain
# top/bottom context margin
padstartbuffered = padstart + 3
padendbuffered = padend - 3
if selend > padendbuffered:
self.scrolllines(selend - padendbuffered)
elif selstart < padstartbuffered:
# negative values scroll in pgup direction
self.scrolllines(selstart - padstartbuffered)
def scrolllines(self, numlines):
"scroll the screen up (down) by numlines when numlines >0 (<0)."
self.firstlineofpadtoprint += numlines
if self.firstlineofpadtoprint < 0:
self.firstlineofpadtoprint = 0
if self.firstlineofpadtoprint > self.numpadlines - 1:
self.firstlineofpadtoprint = self.numpadlines - 1
def toggleapply(self, item=None):
"""
toggle the applied flag of the specified item. if no item is specified,
toggle the flag of the currently selected item.
"""
if item is None:
item = self.currentselecteditem
item.applied = not item.applied
if isinstance(item, uiheader):
item.partial = False
if item.applied:
Laurent Charignon
record_curses: fix ui bug for newly added file...
r24492 # apply all its hunks
for hnk in item.hunks:
hnk.applied = True
# apply all their hunklines
for hunkline in hnk.changedlines:
hunkline.applied = True
Laurent Charignon
record: add crecord's ui logic to core...
r24310 else:
# un-apply all its hunks
for hnk in item.hunks:
hnk.applied = False
hnk.partial = False
# un-apply all their hunklines
for hunkline in hnk.changedlines:
hunkline.applied = False
elif isinstance(item, uihunk):
item.partial = False
# apply all it's hunklines
for hunkline in item.changedlines:
hunkline.applied = item.applied
siblingappliedstatus = [hnk.applied for hnk in item.header.hunks]
allsiblingsapplied = not (False in siblingappliedstatus)
nosiblingsapplied = not (True in siblingappliedstatus)
siblingspartialstatus = [hnk.partial for hnk in item.header.hunks]
somesiblingspartial = (True in siblingspartialstatus)
#cases where applied or partial should be removed from header
# if no 'sibling' hunks are applied (including this hunk)
if nosiblingsapplied:
if not item.header.special():
item.header.applied = False
item.header.partial = False
else: # some/all parent siblings are applied
item.header.applied = True
item.header.partial = (somesiblingspartial or
not allsiblingsapplied)
elif isinstance(item, uihunkline):
siblingappliedstatus = [ln.applied for ln in item.hunk.changedlines]
allsiblingsapplied = not (False in siblingappliedstatus)
nosiblingsapplied = not (True in siblingappliedstatus)
# if no 'sibling' lines are applied
if nosiblingsapplied:
item.hunk.applied = False
item.hunk.partial = False
elif allsiblingsapplied:
item.hunk.applied = True
item.hunk.partial = False
else: # some siblings applied
item.hunk.applied = True
item.hunk.partial = True
parentsiblingsapplied = [hnk.applied for hnk
in item.hunk.header.hunks]
noparentsiblingsapplied = not (True in parentsiblingsapplied)
allparentsiblingsapplied = not (False in parentsiblingsapplied)
parentsiblingspartial = [hnk.partial for hnk
in item.hunk.header.hunks]
someparentsiblingspartial = (True in parentsiblingspartial)
# if all parent hunks are not applied, un-apply header
if noparentsiblingsapplied:
if not item.hunk.header.special():
item.hunk.header.applied = False
item.hunk.header.partial = False
# set the applied and partial status of the header if needed
else: # some/all parent siblings are applied
item.hunk.header.applied = True
item.hunk.header.partial = (someparentsiblingspartial or
not allparentsiblingsapplied)
def toggleall(self):
"toggle the applied flag of all items."
if self.waslasttoggleallapplied: # then unapply them this time
for item in self.headerlist:
if item.applied:
self.toggleapply(item)
else:
for item in self.headerlist:
if not item.applied:
self.toggleapply(item)
self.waslasttoggleallapplied = not self.waslasttoggleallapplied
def togglefolded(self, item=None, foldparent=False):
"toggle folded flag of specified item (defaults to currently selected)"
if item is None:
item = self.currentselecteditem
if foldparent or (isinstance(item, uiheader) and item.neverunfolded):
if not isinstance(item, uiheader):
# we need to select the parent item in this case
self.currentselecteditem = item = item.parentitem()
elif item.neverunfolded:
item.neverunfolded = False
# also fold any foldable children of the parent/current item
if isinstance(item, uiheader): # the original or 'new' item
for child in item.allchildren():
child.folded = not item.folded
if isinstance(item, (uiheader, uihunk)):
item.folded = not item.folded
def alignstring(self, instr, window):
"""
add whitespace to the end of a string in order to make it fill
the screen in the x direction. the current cursor position is
taken into account when making this calculation. the string can span
multiple lines.
"""
y, xstart = window.getyx()
width = self.xscreensize
# turn tabs into spaces
instr = instr.expandtabs(4)
Matt Mackall
crecord: use colwidth instead of ucolwidth
r24351 strwidth = encoding.colwidth(instr)
Matt Mackall
crecord: use encoding.ucolwidth
r24315 numspaces = (width - ((strwidth + xstart) % width) - 1)
Laurent Charignon
record: add crecord's ui logic to core...
r24310 return instr + " " * numspaces + "\n"
def printstring(self, window, text, fgcolor=None, bgcolor=None, pair=None,
pairname=None, attrlist=None, towin=True, align=True, showwhtspc=False):
"""
print the string, text, with the specified colors and attributes, to
the specified curses window object.
the foreground and background colors are of the form
curses.color_xxxx, where xxxx is one of: [black, blue, cyan, green,
magenta, red, white, yellow]. if pairname is provided, a color
pair will be looked up in the self.colorpairnames dictionary.
attrlist is a list containing text attributes in the form of
curses.a_xxxx, where xxxx can be: [bold, dim, normal, standout,
underline].
if align == True, whitespace is added to the printed string such that
the string stretches to the right border of the window.
if showwhtspc == True, trailing whitespace of a string is highlighted.
"""
# preprocess the text, converting tabs to spaces
text = text.expandtabs(4)
# strip \n, and convert control characters to ^[char] representation
Pulkit Goyal
py3: make sure regex is bytes in crecord.py...
r36323 text = re.sub(br'[\x00-\x08\x0a-\x1f]',
Laurent Charignon
record: add crecord's ui logic to core...
r24310 lambda m:'^' + chr(ord(m.group()) + 64), text.strip('\n'))
if pair is not None:
colorpair = pair
elif pairname is not None:
colorpair = self.colorpairnames[pairname]
else:
if fgcolor is None:
fgcolor = -1
if bgcolor is None:
bgcolor = -1
if (fgcolor, bgcolor) in self.colorpairs:
colorpair = self.colorpairs[(fgcolor, bgcolor)]
else:
colorpair = self.getcolorpair(fgcolor, bgcolor)
# add attributes if possible
if attrlist is None:
attrlist = []
if colorpair < 256:
# then it is safe to apply all attributes
for textattr in attrlist:
colorpair |= textattr
else:
# just apply a select few (safe?) attributes
for textattr in (curses.A_UNDERLINE, curses.A_BOLD):
if textattr in attrlist:
colorpair |= textattr
y, xstart = self.chunkpad.getyx()
t = "" # variable for counting lines printed
# if requested, show trailing whitespace
if showwhtspc:
origlen = len(text)
text = text.rstrip(' \n') # tabs have already been expanded
strippedlen = len(text)
numtrailingspaces = origlen - strippedlen
if towin:
window.addstr(text, colorpair)
t += text
if showwhtspc:
wscolorpair = colorpair | curses.A_REVERSE
if towin:
for i in range(numtrailingspaces):
window.addch(curses.ACS_CKBOARD, wscolorpair)
t += " " * numtrailingspaces
if align:
if towin:
extrawhitespace = self.alignstring("", window)
window.addstr(extrawhitespace, colorpair)
else:
# need to use t, since the x position hasn't incremented
extrawhitespace = self.alignstring(t, window)
t += extrawhitespace
# is reset to 0 at the beginning of printitem()
linesprinted = (xstart + len(t)) / self.xscreensize
self.linesprintedtopadsofar += linesprinted
return t
Jun Wu
crecord: rewrite status line text (BC)...
r30546 def _getstatuslinesegments(self):
"""-> [str]. return segments"""
Jun Wu
crecord: change help text for the space key dynamically...
r30547 selected = self.currentselecteditem.applied
Filip Filmar
crecord: fixes the formatting of the select status in the status line...
r33812 spaceselect = _('space: select')
spacedeselect = _('space: deselect')
# Format the selected label into a place as long as the longer of the
# two possible labels. This may vary by language.
spacelen = max(len(spaceselect), len(spacedeselect))
selectedlabel = '%-*s' % (spacelen,
spacedeselect if selected else spaceselect)
Jun Wu
crecord: rewrite status line text (BC)...
r30546 segments = [
Jun Wu
crecord: change the verb according to the operation...
r30548 _headermessages[self.operation],
Jun Wu
crecord: rewrite status line text (BC)...
r30546 '-',
_('[x]=selected **=collapsed'),
_('c: confirm'),
_('q: abort'),
_('arrow keys: move/expand/collapse'),
Filip Filmar
crecord: fixes the formatting of the select status in the status line...
r33812 selectedlabel,
Jun Wu
crecord: rewrite status line text (BC)...
r30546 _('?: help'),
]
return segments
Jun Wu
crecord: move status window text calculation to a separate method...
r30544 def _getstatuslines(self):
"""() -> [str]. return short help used in the top status window"""
if self.errorstr is not None:
lines = [self.errorstr, _('Press any key to continue')]
else:
Jun Wu
crecord: rewrite status line text (BC)...
r30546 # wrap segments to lines
segments = self._getstatuslinesegments()
width = self.xscreensize
lines = []
lastwidth = width
for s in segments:
w = encoding.colwidth(s)
sep = ' ' * (1 + (s and s[0] not in '-['))
if lastwidth + w + len(sep) >= width:
lines.append(s)
lastwidth = w
else:
lines[-1] += sep + s
lastwidth += w + len(sep)
Jun Wu
crecord: make _getstatuslines update numstatuslines...
r30545 if len(lines) != self.numstatuslines:
self.numstatuslines = len(lines)
self.statuswin.resize(self.numstatuslines, self.xscreensize)
Yuya Nishihara
stringutil: bulk-replace call sites to point to new module...
r37102 return [stringutil.ellipsis(l, self.xscreensize - 1) for l in lines]
Jun Wu
crecord: move status window text calculation to a separate method...
r30544
Laurent Charignon
record: add crecord's ui logic to core...
r24310 def updatescreen(self):
self.statuswin.erase()
self.chunkpad.erase()
printstring = self.printstring
# print out the status lines at the top
try:
Jun Wu
crecord: move status window text calculation to a separate method...
r30544 for line in self._getstatuslines():
printstring(self.statuswin, line, pairname="legend")
self.statuswin.refresh()
Laurent Charignon
record: add crecord's ui logic to core...
r24310 except curses.error:
pass
Jun Wu
crecord: move status window text calculation to a separate method...
r30544 if self.errorstr is not None:
return
Laurent Charignon
record: add crecord's ui logic to core...
r24310
# print out the patch in the remaining part of the window
try:
self.printitem()
self.updatescroll()
self.chunkpad.refresh(self.firstlineofpadtoprint, 0,
self.numstatuslines, 0,
Jun Wu
crecord: make _getstatuslines update numstatuslines...
r30545 self.yscreensize - self.numstatuslines,
Laurent Charignon
record: add crecord's ui logic to core...
r24310 self.xscreensize)
except curses.error:
pass
def getstatusprefixstring(self, item):
"""
create a string to prefix a line with which indicates whether 'item'
is applied and/or folded.
Ryan McElroy
crecord: clean up empty lines at ends of docblocks
r28579 """
Laurent Charignon
record: add crecord's ui logic to core...
r24310
# create checkbox string
if item.applied:
if not isinstance(item, uihunkline) and item.partial:
checkbox = "[~]"
else:
checkbox = "[x]"
else:
checkbox = "[ ]"
try:
if item.folded:
checkbox += "**"
if isinstance(item, uiheader):
# one of "m", "a", or "d" (modified, added, deleted)
filestatus = item.changetype
checkbox += filestatus + " "
else:
checkbox += " "
if isinstance(item, uiheader):
# add two more spaces for headers
checkbox += " "
except AttributeError: # not foldable
checkbox += " "
return checkbox
def printheader(self, header, selected=False, towin=True,
ignorefolding=False):
"""
print the header to the pad. if countlines is True, don't print
anything, but just count the number of lines which would be printed.
Ryan McElroy
crecord: clean up empty lines at ends of docblocks
r28579 """
Laurent Charignon
record: add crecord's ui logic to core...
r24310
outstr = ""
text = header.prettystr()
chunkindex = self.chunklist.index(header)
if chunkindex != 0 and not header.folded:
# add separating line before headers
outstr += self.printstring(self.chunkpad, '_' * self.xscreensize,
towin=towin, align=False)
# select color-pair based on if the header is selected
colorpair = self.getcolorpair(name=selected and "selected" or "normal",
attrlist=[curses.A_BOLD])
# print out each line of the chunk, expanding it to screen width
# number of characters to indent lines on this level by
indentnumchars = 0
checkbox = self.getstatusprefixstring(header)
if not header.folded or ignorefolding:
textlist = text.split("\n")
linestr = checkbox + textlist[0]
else:
linestr = checkbox + header.filename()
outstr += self.printstring(self.chunkpad, linestr, pair=colorpair,
towin=towin)
if not header.folded or ignorefolding:
if len(textlist) > 1:
for line in textlist[1:]:
linestr = " "*(indentnumchars + len(checkbox)) + line
outstr += self.printstring(self.chunkpad, linestr,
pair=colorpair, towin=towin)
return outstr
def printhunklinesbefore(self, hunk, selected=False, towin=True,
ignorefolding=False):
"includes start/end line indicator"
outstr = ""
# where hunk is in list of siblings
hunkindex = hunk.header.hunks.index(hunk)
if hunkindex != 0:
# add separating line before headers
outstr += self.printstring(self.chunkpad, ' '*self.xscreensize,
towin=towin, align=False)
colorpair = self.getcolorpair(name=selected and "selected" or "normal",
attrlist=[curses.A_BOLD])
# print out from-to line with checkbox
checkbox = self.getstatusprefixstring(hunk)
lineprefix = " "*self.hunkindentnumchars + checkbox
frtoline = " " + hunk.getfromtoline().strip("\n")
outstr += self.printstring(self.chunkpad, lineprefix, towin=towin,
align=False) # add uncolored checkbox/indent
outstr += self.printstring(self.chunkpad, frtoline, pair=colorpair,
towin=towin)
if hunk.folded and not ignorefolding:
# skip remainder of output
return outstr
# print out lines of the chunk preceeding changed-lines
for line in hunk.before:
linestr = " "*(self.hunklineindentnumchars + len(checkbox)) + line
outstr += self.printstring(self.chunkpad, linestr, towin=towin)
return outstr
def printhunklinesafter(self, hunk, towin=True, ignorefolding=False):
outstr = ""
if hunk.folded and not ignorefolding:
return outstr
# a bit superfluous, but to avoid hard-coding indent amount
checkbox = self.getstatusprefixstring(hunk)
for line in hunk.after:
linestr = " "*(self.hunklineindentnumchars + len(checkbox)) + line
outstr += self.printstring(self.chunkpad, linestr, towin=towin)
return outstr
def printhunkchangedline(self, hunkline, selected=False, towin=True):
outstr = ""
checkbox = self.getstatusprefixstring(hunkline)
linestr = hunkline.prettystr().strip("\n")
# select color-pair based on whether line is an addition/removal
if selected:
colorpair = self.getcolorpair(name="selected")
elif linestr.startswith("+"):
colorpair = self.getcolorpair(name="addition")
elif linestr.startswith("-"):
colorpair = self.getcolorpair(name="deletion")
elif linestr.startswith("\\"):
colorpair = self.getcolorpair(name="normal")
lineprefix = " "*self.hunklineindentnumchars + checkbox
outstr += self.printstring(self.chunkpad, lineprefix, towin=towin,
align=False) # add uncolored checkbox/indent
outstr += self.printstring(self.chunkpad, linestr, pair=colorpair,
towin=towin, showwhtspc=True)
return outstr
def printitem(self, item=None, ignorefolding=False, recursechildren=True,
towin=True):
"""
use __printitem() to print the the specified item.applied.
if item is not specified, then print the entire patch.
(hiding folded elements, etc. -- see __printitem() docstring)
"""
Ryan McElroy
crecord: clean up empty lines at ends of docblocks
r28579
Laurent Charignon
record: add crecord's ui logic to core...
r24310 if item is None:
item = self.headerlist
if recursechildren:
self.linesprintedtopadsofar = 0
outstr = []
self.__printitem(item, ignorefolding, recursechildren, outstr,
towin=towin)
return ''.join(outstr)
def outofdisplayedarea(self):
y, _ = self.chunkpad.getyx() # cursor location
# * 2 here works but an optimization would be the max number of
# consecutive non selectable lines
# i.e the max number of context line for any hunk in the patch
miny = min(0, self.firstlineofpadtoprint - self.yscreensize)
maxy = self.firstlineofpadtoprint + self.yscreensize * 2
return y < miny or y > maxy
def handleselection(self, item, recursechildren):
selected = (item is self.currentselecteditem)
if selected and recursechildren:
# assumes line numbering starting from line 0
self.selecteditemstartline = self.linesprintedtopadsofar
selecteditemlines = self.getnumlinesdisplayed(item,
recursechildren=False)
self.selecteditemendline = (self.selecteditemstartline +
selecteditemlines - 1)
return selected
def __printitem(self, item, ignorefolding, recursechildren, outstr,
towin=True):
"""
recursive method for printing out patch/header/hunk/hunk-line data to
screen. also returns a string with all of the content of the displayed
patch (not including coloring, etc.).
if ignorefolding is True, then folded items are printed out.
if recursechildren is False, then only print the item without its
child items.
Ryan McElroy
crecord: clean up empty lines at ends of docblocks
r28579 """
Laurent Charignon
record: add crecord's ui logic to core...
r24310
if towin and self.outofdisplayedarea():
return
selected = self.handleselection(item, recursechildren)
# patch object is a list of headers
if isinstance(item, patch):
if recursechildren:
for hdr in item:
self.__printitem(hdr, ignorefolding,
recursechildren, outstr, towin)
# todo: eliminate all isinstance() calls
if isinstance(item, uiheader):
outstr.append(self.printheader(item, selected, towin=towin,
ignorefolding=ignorefolding))
if recursechildren:
for hnk in item.hunks:
self.__printitem(hnk, ignorefolding,
recursechildren, outstr, towin)
elif (isinstance(item, uihunk) and
((not item.header.folded) or ignorefolding)):
# print the hunk data which comes before the changed-lines
outstr.append(self.printhunklinesbefore(item, selected, towin=towin,
ignorefolding=ignorefolding))
if recursechildren:
for l in item.changedlines:
self.__printitem(l, ignorefolding,
recursechildren, outstr, towin)
outstr.append(self.printhunklinesafter(item, towin=towin,
ignorefolding=ignorefolding))
elif (isinstance(item, uihunkline) and
((not item.hunk.folded) or ignorefolding)):
outstr.append(self.printhunkchangedline(item, selected,
towin=towin))
return outstr
def getnumlinesdisplayed(self, item=None, ignorefolding=False,
recursechildren=True):
"""
return the number of lines which would be displayed if the item were
to be printed to the display. the item will not be printed to the
display (pad).
if no item is given, assume the entire patch.
if ignorefolding is True, folded items will be unfolded when counting
the number of lines.
Ryan McElroy
crecord: clean up empty lines at ends of docblocks
r28579 """
Laurent Charignon
record: add crecord's ui logic to core...
r24310
# temporarily disable printing to windows by printstring
patchdisplaystring = self.printitem(item, ignorefolding,
recursechildren, towin=False)
Pulkit Goyal
py3: make sure we are doing integer division by using '//'...
r36324 numlines = len(patchdisplaystring) // self.xscreensize
Laurent Charignon
record: add crecord's ui logic to core...
r24310 return numlines
def sigwinchhandler(self, n, frame):
"handle window resizing"
try:
curses.endwin()
Yuya Nishihara
crecord: use scmutil.termsize()
r30315 self.xscreensize, self.yscreensize = scmutil.termsize(self.ui)
Laurent Charignon
record: add crecord's ui logic to core...
r24310 self.statuswin.resize(self.numstatuslines, self.xscreensize)
self.numpadlines = self.getnumlinesdisplayed(ignorefolding=True) + 1
self.chunkpad = curses.newpad(self.numpadlines, self.xscreensize)
except curses.error:
pass
def getcolorpair(self, fgcolor=None, bgcolor=None, name=None,
attrlist=None):
"""
get a curses color pair, adding it to self.colorpairs if it is not
already defined. an optional string, name, can be passed as a shortcut
for referring to the color-pair. by default, if no arguments are
specified, the white foreground / black background color-pair is
returned.
it is expected that this function will be used exclusively for
initializing color pairs, and not curses.init_pair().
attrlist is used to 'flavor' the returned color-pair. this information
is not stored in self.colorpairs. it contains attribute values like
curses.A_BOLD.
Ryan McElroy
crecord: clean up empty lines at ends of docblocks
r28579 """
Laurent Charignon
record: add crecord's ui logic to core...
r24310
if (name is not None) and name in self.colorpairnames:
# then get the associated color pair and return it
colorpair = self.colorpairnames[name]
else:
if fgcolor is None:
fgcolor = -1
if bgcolor is None:
bgcolor = -1
if (fgcolor, bgcolor) in self.colorpairs:
colorpair = self.colorpairs[(fgcolor, bgcolor)]
else:
pairindex = len(self.colorpairs) + 1
Elmar Bartel
crecord: honor "ui.color = no" config option...
r35545 if self.usecolor:
curses.init_pair(pairindex, fgcolor, bgcolor)
colorpair = self.colorpairs[(fgcolor, bgcolor)] = (
curses.color_pair(pairindex))
if name is not None:
self.colorpairnames[name] = curses.color_pair(pairindex)
else:
cval = 0
if name is not None:
if name == 'selected':
cval = curses.A_REVERSE
self.colorpairnames[name] = cval
colorpair = self.colorpairs[(fgcolor, bgcolor)] = cval
Laurent Charignon
record: add crecord's ui logic to core...
r24310
# add attributes if possible
if attrlist is None:
attrlist = []
if colorpair < 256:
# then it is safe to apply all attributes
for textattr in attrlist:
colorpair |= textattr
else:
# just apply a select few (safe?) attributes
for textattrib in (curses.A_UNDERLINE, curses.A_BOLD):
if textattrib in attrlist:
colorpair |= textattrib
return colorpair
def initcolorpair(self, *args, **kwargs):
"same as getcolorpair."
self.getcolorpair(*args, **kwargs)
def helpwindow(self):
"print a help window to the screen. exit after any keypress."
Jun Wu
crecord: filter text via i18n...
r30535 helptext = _(
""" [press any key to return to the patch-display]
Laurent Charignon
record: add crecord's ui logic to core...
r24310
crecord allows you to interactively choose among the changes you have made,
Laurent Charignon
record: change wording for record curses interface...
r24840 and confirm only those changes you select for further processing by the command
you are running (commit/shelve/revert), after confirming the selected
Laurent Charignon
record: add crecord's ui logic to core...
r24310 changes, the unselected changes are still present in your working copy, so you
can use crecord multiple times to split large changes into smaller changesets.
the following are valid keystrokes:
[space] : (un-)select item ([~]/[x] = partly/fully applied)
Laurent Charignon
crecord: fix typo in the help text...
r27936 A : (un-)select all items
Laurent Charignon
record: add crecord's ui logic to core...
r24310 up/down-arrow [k/j] : go to previous/next unfolded item
Laurent Charignon
crecord: fix three typos introduced while moving crecord into core...
r25460 pgup/pgdn [K/J] : go to previous/next item of same type
Laurent Charignon
record: add crecord's ui logic to core...
r24310 right/left-arrow [l/h] : go to child item / parent item
Laurent Charignon
crecord: fix three typos introduced while moving crecord into core...
r25460 shift-left-arrow [H] : go to parent header / fold selected header
Laurent Charignon
record: add crecord's ui logic to core...
r24310 f : fold / unfold item, hiding/revealing its children
Laurent Charignon
crecord: fix a typo introduced when moving crecord to core...
r25447 F : fold / unfold parent item and all of its ancestors
Nathan Goldbaum
crecord: add an event that scrolls the selected line to the top of the screen...
r29957 ctrl-l : scroll the selected line to the top of the screen
Laurent Charignon
record: add crecord's ui logic to core...
r24310 m : edit / resume editing the commit message
e : edit the currently selected hunk
Pierre-Yves David
crecord: drop the version condition for amend...
r29077 a : toggle amend mode, only with commit -i
Laurent Charignon
record: change wording for record curses interface...
r24840 c : confirm selected changes
r : review/edit and confirm selected changes
q : quit without confirming (no changes will be made)
Jun Wu
crecord: filter text via i18n...
r30535 ? : help (what you're currently reading)""")
Laurent Charignon
record: add crecord's ui logic to core...
r24310
helpwin = curses.newwin(self.yscreensize, 0, 0, 0)
helplines = helptext.split("\n")
helplines = helplines + [" "]*(
self.yscreensize - self.numstatuslines - len(helplines) - 1)
try:
for line in helplines:
self.printstring(helpwin, line, pairname="legend")
except curses.error:
pass
helpwin.refresh()
try:
Simon Farnsworth
crecord: log blocked time waiting for curses input...
r30981 with self.ui.timeblockedsection('crecord'):
helpwin.getkey()
Laurent Charignon
record: add crecord's ui logic to core...
r24310 except curses.error:
pass
Peter Vitt
record: make the m key open an editor for the commit message (issue5667)...
r33975 def commitMessageWindow(self):
"Create a temporary commit message editing window on the screen."
curses.raw()
curses.def_prog_mode()
curses.endwin()
self.commenttext = self.ui.edit(self.commenttext, self.ui.username())
curses.cbreak()
self.stdscr.refresh()
self.stdscr.keypad(1) # allow arrow-keys to continue to function
Laurent Charignon
record: add crecord's ui logic to core...
r24310 def confirmationwindow(self, windowtext):
"display an informational window, then wait for and return a keypress."
confirmwin = curses.newwin(self.yscreensize, 0, 0, 0)
try:
lines = windowtext.split("\n")
for line in lines:
self.printstring(confirmwin, line, pairname="selected")
except curses.error:
pass
self.stdscr.refresh()
confirmwin.refresh()
try:
Simon Farnsworth
crecord: log blocked time waiting for curses input...
r30981 with self.ui.timeblockedsection('crecord'):
response = chr(self.stdscr.getch())
Laurent Charignon
record: add crecord's ui logic to core...
r24310 except ValueError:
response = None
return response
Pierre-Yves David
crecord: cleanup the remains of commit confirmation...
r28926 def reviewcommit(self):
Laurent Charignon
record: change wording for record curses interface...
r24840 """ask for 'y' to be pressed to confirm selected. return True if
confirmed."""
Jun Wu
crecord: filter text via i18n...
r30535 confirmtext = _(
Laurent Charignon
record: add crecord's ui logic to core...
r24310 """if you answer yes to the following, the your currently chosen patch chunks
will be loaded into an editor. you may modify the patch from the editor, and
save the changes if you wish to change the patch. otherwise, you can just
close the editor without saving to accept the current patch as-is.
note: don't add/remove lines unless you also modify the range information.
failing to follow this rule will result in the commit aborting.
Laurent Charignon
record: change wording for record curses interface...
r24840 are you sure you want to review/edit and confirm the selected changes [yn]?
""")
Simon Farnsworth
crecord: log blocked time waiting for curses input...
r30981 with self.ui.timeblockedsection('crecord'):
response = self.confirmationwindow(confirmtext)
Laurent Charignon
record: add crecord's ui logic to core...
r24310 if response is None:
response = "n"
if response.lower().startswith("y"):
return True
else:
return False
Laurent Charignon
commit: add amend mode for commit -i...
r27156 def toggleamend(self, opts, test):
"""Toggle the amend flag.
When the amend flag is set, a commit will modify the most recently
committed changeset, instead of creating a new changeset. Otherwise, a
new changeset will be created (the normal commit behavior).
Ryan McElroy
crecord: clean up empty lines at ends of docblocks
r28579 """
Laurent Charignon
commit: add amend mode for commit -i...
r27156
try:
ver = float(util.version()[:3])
except ValueError:
ver = 1
if ver < 2.19:
Jun Wu
crecord: filter text via i18n...
r30535 msg = _("The amend option is unavailable with hg versions < 2.2\n\n"
"Press any key to continue.")
Laurent Charignon
commit: add amend mode for commit -i...
r27156 elif opts.get('amend') is None:
opts['amend'] = True
Jun Wu
crecord: filter text via i18n...
r30535 msg = _("Amend option is turned on -- committing the currently "
"selected changes will not create a new changeset, but "
"instead update the most recently committed changeset.\n\n"
"Press any key to continue.")
Laurent Charignon
commit: add amend mode for commit -i...
r27156 elif opts.get('amend') is True:
opts['amend'] = None
Jun Wu
crecord: filter text via i18n...
r30535 msg = _("Amend option is turned off -- committing the currently "
"selected changes will create a new changeset.\n\n"
"Press any key to continue.")
Laurent Charignon
commit: add amend mode for commit -i...
r27156 if not test:
self.confirmationwindow(msg)
Laurent Charignon
record: add crecord's ui logic to core...
r24310 def recenterdisplayedarea(self):
"""
once we scrolled with pg up pg down we can be pointing outside of the
display zone. we print the patch with towin=False to compute the
Mads Kiilerich
spelling: trivial spell checking
r26781 location of the selected item even though it is outside of the displayed
Laurent Charignon
record: add crecord's ui logic to core...
r24310 zone and then update the scroll.
"""
self.printitem(towin=False)
self.updatescroll()
def toggleedit(self, item=None, test=False):
"""
Ryan McElroy
crecord: fix docblock indentation
r28580 edit the currently selected chunk
Laurent Charignon
record: add crecord's ui logic to core...
r24310 """
Laurent Charignon
crecord: extract ui refresh code after editing of hunk in a function...
r25555 def updateui(self):
self.numpadlines = self.getnumlinesdisplayed(ignorefolding=True) + 1
self.chunkpad = curses.newpad(self.numpadlines, self.xscreensize)
self.updatescroll()
self.stdscr.refresh()
self.statuswin.refresh()
self.stdscr.keypad(1)
Laurent Charignon
record: add crecord's ui logic to core...
r24310
def editpatchwitheditor(self, chunk):
if chunk is None:
self.ui.write(_('cannot edit patch for whole file'))
self.ui.write("\n")
return None
if chunk.header.binary():
self.ui.write(_('cannot edit patch for binary file'))
self.ui.write("\n")
return None
Jordi Gutiérrez Hermoso
crecord: refactor hunk edit action to use ui.edit...
r28636 # write the initial patch
timeless
pycompat: switch to util.stringio for py3 compat
r28861 patch = stringio()
Jordi Gutiérrez Hermoso
crecord: break out the help message for editing hunks...
r28637 patch.write(diffhelptext + hunkhelptext)
Jordi Gutiérrez Hermoso
crecord: refactor hunk edit action to use ui.edit...
r28636 chunk.header.write(patch)
chunk.write(patch)
# start the editor and wait for it to complete
Laurent Charignon
record: add crecord's ui logic to core...
r24310 try:
Michael Bolin
editor: use an unambiguous path suffix for editor files...
r34030 patch = self.ui.edit(patch.getvalue(), "", action="diff")
Jordi Gutiérrez Hermoso
crecord: refactor hunk edit action to use ui.edit...
r28636 except error.Abort as exc:
self.errorstr = str(exc)
return None
# remove comment lines
patch = [line + '\n' for line in patch.splitlines()
if not line.startswith('#')]
return patchmod.parsepatch(patch)
Laurent Charignon
record: add crecord's ui logic to core...
r24310 if item is None:
item = self.currentselecteditem
if isinstance(item, uiheader):
return
if isinstance(item, uihunkline):
item = item.parentitem()
if not isinstance(item, uihunk):
return
Laurent Charignon
crecord: edit during hg crecord should preserve cursor position (issue5041)...
r27914 # To go back to that hunk or its replacement at the end of the edit
itemindex = item.parentitem().hunks.index(item)
Laurent Charignon
record: add crecord's ui logic to core...
r24310 beforeadded, beforeremoved = item.added, item.removed
newpatches = editpatchwitheditor(self, item)
Laurent Charignon
crecord: exit edition of hunk with non-zero status does not interrupt session...
r25557 if newpatches is None:
if not test:
updateui(self)
return
Laurent Charignon
record: add crecord's ui logic to core...
r24310 header = item.header
editedhunkindex = header.hunks.index(item)
hunksbefore = header.hunks[:editedhunkindex]
hunksafter = header.hunks[editedhunkindex + 1:]
newpatchheader = newpatches[0]
newhunks = [uihunk(h, header) for h in newpatchheader.hunks]
newadded = sum([h.added for h in newhunks])
newremoved = sum([h.removed for h in newhunks])
offset = (newadded - beforeadded) - (newremoved - beforeremoved)
for h in hunksafter:
h.toline += offset
for h in newhunks:
h.folded = False
header.hunks = hunksbefore + newhunks + hunksafter
if self.emptypatch():
header.hunks = hunksbefore + [item] + hunksafter
self.currentselecteditem = header
Laurent Charignon
crecord: edit during hg crecord should preserve cursor position (issue5041)...
r27914 if len(header.hunks) > itemindex:
self.currentselecteditem = header.hunks[itemindex]
Laurent Charignon
record: add crecord's ui logic to core...
r24310
if not test:
Laurent Charignon
crecord: extract ui refresh code after editing of hunk in a function...
r25555 updateui(self)
Laurent Charignon
record: add crecord's ui logic to core...
r24310
def emptypatch(self):
item = self.headerlist
if not item:
return True
for header in item:
if header.hunks:
return False
return True
def handlekeypressed(self, keypressed, test=False):
Ryan McElroy
crecord: add docblock to handlekeypressed...
r28581 """
Perform actions based on pressed keys.
Return true to exit the main loop.
"""
Laurent Charignon
record: add crecord's ui logic to core...
r24310 if keypressed in ["k", "KEY_UP"]:
self.uparrowevent()
Laurent Charignon
crecord: fix three typos introduced while moving crecord into core...
r25460 if keypressed in ["K", "KEY_PPAGE"]:
Laurent Charignon
record: add crecord's ui logic to core...
r24310 self.uparrowshiftevent()
elif keypressed in ["j", "KEY_DOWN"]:
self.downarrowevent()
Laurent Charignon
crecord: fix three typos introduced while moving crecord into core...
r25460 elif keypressed in ["J", "KEY_NPAGE"]:
Laurent Charignon
record: add crecord's ui logic to core...
r24310 self.downarrowshiftevent()
elif keypressed in ["l", "KEY_RIGHT"]:
self.rightarrowevent()
elif keypressed in ["h", "KEY_LEFT"]:
self.leftarrowevent()
Laurent Charignon
crecord: fix three typos introduced while moving crecord into core...
r25460 elif keypressed in ["H", "KEY_SLEFT"]:
Laurent Charignon
record: add crecord's ui logic to core...
r24310 self.leftarrowshiftevent()
elif keypressed in ["q"]:
Pierre-Yves David
error: get Abort from 'error' instead of 'util'...
r26587 raise error.Abort(_('user quit'))
Laurent Charignon
commit: add amend mode for commit -i...
r27156 elif keypressed in ['a']:
self.toggleamend(self.opts, test)
Laurent Charignon
record: add crecord's ui logic to core...
r24310 elif keypressed in ["c"]:
Pierre-Yves David
crecord: drop the extra confirmation screen...
r28925 return True
Jordi Gutiérrez Hermoso
crecord: re-enable reviewing a patch before comitting it...
r28638 elif test and keypressed in ['X']:
return True
Laurent Charignon
record: add crecord's ui logic to core...
r24310 elif keypressed in ["r"]:
Pierre-Yves David
crecord: cleanup the remains of commit confirmation...
r28926 if self.reviewcommit():
Jordi Gutiérrez Hermoso
crecord: re-enable reviewing a patch before comitting it...
r28638 self.opts['review'] = True
Laurent Charignon
record: add crecord's ui logic to core...
r24310 return True
Jordi Gutiérrez Hermoso
crecord: re-enable reviewing a patch before comitting it...
r28638 elif test and keypressed in ['R']:
self.opts['review'] = True
Laurent Charignon
record: add crecord's ui logic to core...
r24310 return True
elif keypressed in [' '] or (test and keypressed in ["TOGGLE"]):
self.toggleapply()
av6
crecord: add an experimental option for space key to move cursor down...
r30797 if self.ui.configbool('experimental', 'spacemovesdown'):
self.downarrowevent()
Laurent Charignon
record: add crecord's ui logic to core...
r24310 elif keypressed in ['A']:
self.toggleall()
elif keypressed in ['e']:
self.toggleedit(test=test)
elif keypressed in ["f"]:
self.togglefolded()
Laurent Charignon
crecord: fix a typo introduced when moving crecord to core...
r25447 elif keypressed in ["F"]:
Laurent Charignon
record: add crecord's ui logic to core...
r24310 self.togglefolded(foldparent=True)
Peter Vitt
record: make the m key open an editor for the commit message (issue5667)...
r33975 elif keypressed in ["m"]:
self.commitMessageWindow()
Laurent Charignon
record: add crecord's ui logic to core...
r24310 elif keypressed in ["?"]:
self.helpwindow()
Laurent Charignon
crecord: fix blue artifact bug coming back from help screen...
r25419 self.stdscr.clear()
self.stdscr.refresh()
Nathan Goldbaum
crecord: add an event that scrolls the selected line to the top of the screen...
r29957 elif curses.unctrl(keypressed) in ["^L"]:
# scroll the current line to the top of the screen
self.scrolllines(self.selecteditemstartline)
Laurent Charignon
record: add crecord's ui logic to core...
r24310
def main(self, stdscr):
"""
method to be wrapped by curses.wrapper() for selecting chunks.
Ryan McElroy
crecord: clean up empty lines at ends of docblocks
r28579 """
Laurent Charignon
record: add crecord's ui logic to core...
r24310
Pierre-Yves David
crecord: avoid setting non-existing signal SIGWINCH on windows...
r31931 origsigwinch = sentinel = object()
if util.safehasattr(signal, 'SIGWINCH'):
origsigwinch = signal.signal(signal.SIGWINCH,
self.sigwinchhandler)
Pierre-Yves David
crecord: ensure we reinstall the SIGWINCH handler...
r31930 try:
return self._main(stdscr)
finally:
Pierre-Yves David
crecord: avoid setting non-existing signal SIGWINCH on windows...
r31931 if origsigwinch is not sentinel:
signal.signal(signal.SIGWINCH, origsigwinch)
Pierre-Yves David
crecord: extract most of 'main' into a sub function...
r31929
def _main(self, stdscr):
Laurent Charignon
record: add crecord's ui logic to core...
r24310 self.stdscr = stdscr
Laurent Charignon
crecord: add error reporting for failure in curses interface initialization...
r25820 # error during initialization, cannot be printed in the curses
# interface, it should be printed by the calling code
Kyle Lippincott
crecord: fallback to text mode if diffs are too big for curses mode...
r38058 self.initexc = None
Laurent Charignon
record: add crecord's ui logic to core...
r24310 self.yscreensize, self.xscreensize = self.stdscr.getmaxyx()
curses.start_color()
Elmar Bartel
crecord: fallback to color = no when curses.use_default_colors() fails...
r35546 try:
curses.use_default_colors()
except curses.error:
self.usecolor = False
Laurent Charignon
record: add crecord's ui logic to core...
r24310
# available colors: black, blue, cyan, green, magenta, white, yellow
# init_pair(color_id, foreground_color, background_color)
self.initcolorpair(None, None, name="normal")
self.initcolorpair(curses.COLOR_WHITE, curses.COLOR_MAGENTA,
name="selected")
self.initcolorpair(curses.COLOR_RED, None, name="deletion")
self.initcolorpair(curses.COLOR_GREEN, None, name="addition")
self.initcolorpair(curses.COLOR_WHITE, curses.COLOR_BLUE, name="legend")
# newwin([height, width,] begin_y, begin_x)
self.statuswin = curses.newwin(self.numstatuslines, 0, 0, 0)
self.statuswin.keypad(1) # interpret arrow-key, etc. esc sequences
# figure out how much space to allocate for the chunk-pad which is
# used for displaying the patch
# stupid hack to prevent getnumlinesdisplayed from failing
self.chunkpad = curses.newpad(1, self.xscreensize)
# add 1 so to account for last line text reaching end of line
self.numpadlines = self.getnumlinesdisplayed(ignorefolding=True) + 1
Laurent Charignon
crecord: throws error instead of crashing for large diffs...
r25821 try:
self.chunkpad = curses.newpad(self.numpadlines, self.xscreensize)
except curses.error:
Kyle Lippincott
crecord: fallback to text mode if diffs are too big for curses mode...
r38058 self.initexc = fallbackerror(
_('this diff is too large to be displayed'))
Laurent Charignon
crecord: throws error instead of crashing for large diffs...
r25821 return
Mads Kiilerich
spelling: fixes of non-dictionary words
r30332 # initialize selecteditemendline (initial start-line is 0)
Laurent Charignon
record: add crecord's ui logic to core...
r24310 self.selecteditemendline = self.getnumlinesdisplayed(
self.currentselecteditem, recursechildren=False)
while True:
self.updatescreen()
try:
Simon Farnsworth
crecord: log blocked time waiting for curses input...
r30981 with self.ui.timeblockedsection('crecord'):
keypressed = self.statuswin.getkey()
Laurent Charignon
crecord: add mechanism for error reporting...
r25556 if self.errorstr is not None:
self.errorstr = None
continue
Laurent Charignon
record: add crecord's ui logic to core...
r24310 except curses.error:
keypressed = "foobar"
if self.handlekeypressed(keypressed):
break
Peter Vitt
record: make the m key open an editor for the commit message (issue5667)...
r33975
if self.commenttext != "":
whitespaceremoved = re.sub("(?m)^\s.*(\n|$)", "", self.commenttext)
if whitespaceremoved != "":
self.opts['message'] = self.commenttext