mercurial/ui.py
author Thomas Arendsen Hein <thomas@intevation.de>
Thu, 02 Jun 2005 09:14:12 +0100
changeset 235 eb341488aa4c
parent 234 3427806d5ab9
child 237 4f802588cdfb
permissions -rw-r--r--
Obfuscate author in all HTML templates.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     1
# ui.py - user interface bits for mercurial
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     2
#
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     3
# Copyright 2005 Matt Mackall <mpm@selenic.com>
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     4
#
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     7
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     8
import os, tempfile, sys, re
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
     9
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    10
class ui:
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    11
    def __init__(self, verbose=False, debug=False, quiet=False,
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    12
                 interactive=True):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    13
        self.quiet = quiet and not verbose and not debug
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    14
        self.verbose = verbose or debug
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    15
        self.debugflag = debug
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    16
        self.interactive = interactive
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    17
    def write(self, *args):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    18
        for a in args:
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    19
            sys.stdout.write(str(a))
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    20
    def readline(self):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    21
        return sys.stdin.readline()[:-1]
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    22
    def prompt(self, msg, pat, default = "y"):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    23
        if not self.interactive: return default
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    24
        while 1:
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    25
            self.write(msg, " ")
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    26
            r = self.readline()
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    27
            if re.match(pat, r):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    28
                return r
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    29
            else:
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    30
                self.write("unrecognized response\n")
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    31
    def status(self, *msg):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    32
        if not self.quiet: self.write(*msg)
234
3427806d5ab9 ui.warn can use more than one argument like the other ui methods.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 207
diff changeset
    33
    def warn(self, *msg):
207
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    34
        self.write(*msg)
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    35
    def note(self, *msg):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    36
        if self.verbose: self.write(*msg)
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    37
    def debug(self, *msg):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    38
        if self.debugflag: self.write(*msg)
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    39
    def edit(self, text):
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    40
        (fd, name) = tempfile.mkstemp("hg")
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    41
        f = os.fdopen(fd, "w")
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    42
        f.write(text)
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    43
        f.close()
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    44
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    45
        editor = os.environ.get("HGEDITOR") or os.environ.get("EDITOR", "vi")
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    46
        r = os.system("%s %s" % (editor, name))
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    47
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    48
        if r:
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    49
            raise "Edit failed!"
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    50
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    51
        t = open(name).read()
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    52
        t = re.sub("(?m)^HG:.*\n", "", t)
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    53
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    54
        return t
ec327cf0d3a9 Move ui class to its own module
mpm@selenic.com
parents:
diff changeset
    55