[PATCH] Log, annotate and diff use an environment pager if available
Adrian Buehlmann
adrian at cadifra.com
Thu Mar 6 06:19:41 CST 2008
On 06.03.2008 12:01, David Soria Parra wrote:
> Here is a changeset that uses popen instead of subprocess and therefore
> should be compatible with 2.3
>
> best whishes
> --------
>
> # HG changeset patch
> # User David Soria Parra <dsp at php.net>
> # Date 1204801144 -3600
> # Node ID 673b797c079a2b596b3dfc31b4258c6720a8a98e
> # Parent 0068809347d72e552f980487ee669ace9a82d214
> Log, annotate and diff use an environment pager if available
>
> Unix systems usually have a PAGER environment variable set.
> If it is set, mercurial will use this to display annotate, logs
> or diffs instead of just writing them out
>
[snip]
> +class pager(object):
> + """
> + Use the environment pager that is usually set on
> + UNIX like systems to display the contents of e.g. the reflog
reflog -> revlog
http://www.selenic.com/mercurial/wiki/index.cgi/Revlog
> +
> + the pager is not used if the output is not bigger than min_lines
> + but instead just print to stdout.
> + if min_lines is 0, the pager is always used
> + """
> + def __init__(self, ui, min_lines=25):
> + self.buffer = []
> + self.ui = ui
> + self.min_lines = min_lines
> +
> + def __del__(self):
> + if len(self.buffer) < self.min_lines:
> + self.ui.write(os.linesep.join(self.buffer) + os.linesep)
> + elif self.proc:
> + try:
> + self.proc.close()
> + except IOError:
> + # we might get into an broken pipe if the users quit
> + # the pager before we finished io
> + pass
> +
> + def __getattr__(self, key):
> + try:
> + return getattr(self.ui, key)
> + except AttributeError:
> + pass
> +
> + def write(self, w):
> + self.buffer.extend(w.splitlines())
> + if len(self.buffer) >= self.min_lines and not self.buffer_written:
> + # we reached the limit and not yet written the buffer
> + self.buffer_written = True
> + if self.getpager() and not self.proc:
> + # we now that we need a process only if we already
> reach the lines_to_page limit
* Error in comment
* Possible email linewrapping problem here
> + self.proc = os.popen(self.getpager(), "w")
> + self.proc.write(os.linesep.join(self.buffer))
> + else:
> + self.ui.write(os.linesep.join(self.buffer))
> + elif self.buffer_written:
> + if self.proc:
> + self.proc.write(w)
> + else:
> + self.ui.write(w)
> +
> + def getpager(self):
> + '''return a pager'''
> + return (os.environ.get("PAGER") or
> + self.config("ui", "pager"))
More information about the Mercurial-devel
mailing list