author | mpm@selenic.com |
Wed, 29 Jun 2005 11:05:09 -0800 | |
changeset 519 | 50768efaf6f2 |
parent 515 | 03f27b1381f9 |
child 565 | 9a80418646dd |
permissions | -rw-r--r-- |
207 | 1 |
# ui.py - user interface bits for mercurial |
2 |
# |
|
3 |
# Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
4 |
# |
|
5 |
# This software may be used and distributed according to the terms |
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
|
7 |
||
508 | 8 |
import os, sys, re, ConfigParser, util |
207 | 9 |
|
10 |
class ui: |
|
11 |
def __init__(self, verbose=False, debug=False, quiet=False, |
|
12 |
interactive=True): |
|
285 | 13 |
self.cdata = ConfigParser.SafeConfigParser() |
14 |
self.cdata.read(os.path.expanduser("~/.hgrc")) |
|
15 |
||
16 |
self.quiet = self.configbool("ui", "quiet") |
|
17 |
self.verbose = self.configbool("ui", "verbose") |
|
18 |
self.debugflag = self.configbool("ui", "debug") |
|
19 |
self.interactive = self.configbool("ui", "interactive", True) |
|
20 |
||
21 |
self.quiet = (self.quiet or quiet) and not verbose and not debug |
|
22 |
self.verbose = (self.verbose or verbose) or debug |
|
23 |
self.debugflag = (self.debugflag or debug) |
|
24 |
self.interactive = (self.interactive and interactive) |
|
25 |
||
337 | 26 |
def readconfig(self, fp): |
27 |
self.cdata.readfp(fp) |
|
28 |
||
285 | 29 |
def config(self, section, val, default=None): |
30 |
if self.cdata.has_option(section, val): |
|
31 |
return self.cdata.get(section, val) |
|
32 |
return default |
|
33 |
||
34 |
def configbool(self, section, val, default=False): |
|
35 |
if self.cdata.has_option(section, val): |
|
36 |
return self.cdata.getboolean(section, val) |
|
37 |
return default |
|
38 |
||
39 |
def configitems(self, section): |
|
40 |
if self.cdata.has_section(section): |
|
41 |
return self.cdata.items(section) |
|
42 |
return [] |
|
43 |
||
506 | 44 |
def expandpath(self, loc): |
45 |
paths = {} |
|
46 |
for name, path in self.configitems("paths"): |
|
47 |
paths[name] = path |
|
48 |
||
49 |
return paths.get(loc, loc) |
|
50 |
||
207 | 51 |
def write(self, *args): |
52 |
for a in args: |
|
53 |
sys.stdout.write(str(a)) |
|
54 |
def readline(self): |
|
55 |
return sys.stdin.readline()[:-1] |
|
56 |
def prompt(self, msg, pat, default = "y"): |
|
57 |
if not self.interactive: return default |
|
58 |
while 1: |
|
59 |
self.write(msg, " ") |
|
60 |
r = self.readline() |
|
61 |
if re.match(pat, r): |
|
62 |
return r |
|
63 |
else: |
|
64 |
self.write("unrecognized response\n") |
|
65 |
def status(self, *msg): |
|
66 |
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
|
67 |
def warn(self, *msg): |
207 | 68 |
self.write(*msg) |
69 |
def note(self, *msg): |
|
70 |
if self.verbose: self.write(*msg) |
|
71 |
def debug(self, *msg): |
|
72 |
if self.debugflag: self.write(*msg) |
|
73 |
def edit(self, text): |
|
249 | 74 |
import tempfile |
207 | 75 |
(fd, name) = tempfile.mkstemp("hg") |
76 |
f = os.fdopen(fd, "w") |
|
77 |
f.write(text) |
|
78 |
f.close() |
|
79 |
||
80 |
editor = os.environ.get("HGEDITOR") or os.environ.get("EDITOR", "vi") |
|
508 | 81 |
util.system("%s %s" % (editor, name), errprefix = "edit failed") |
207 | 82 |
|
83 |
t = open(name).read() |
|
84 |
t = re.sub("(?m)^HG:.*\n", "", t) |
|
85 |
||
86 |
return t |