author | mpm@selenic.com |
Sat, 02 Jul 2005 19:57:47 -0800 | |
changeset 589 | 4be4d4580467 |
parent 565 | 9a80418646dd |
child 608 | d2994b5298fb |
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)) |
|
565 | 54 |
|
55 |
def write_err(self, *args): |
|
56 |
sys.stdout.flush() |
|
57 |
for a in args: |
|
58 |
sys.stderr.write(str(a)) |
|
59 |
||
207 | 60 |
def readline(self): |
61 |
return sys.stdin.readline()[:-1] |
|
62 |
def prompt(self, msg, pat, default = "y"): |
|
63 |
if not self.interactive: return default |
|
64 |
while 1: |
|
65 |
self.write(msg, " ") |
|
66 |
r = self.readline() |
|
67 |
if re.match(pat, r): |
|
68 |
return r |
|
69 |
else: |
|
70 |
self.write("unrecognized response\n") |
|
71 |
def status(self, *msg): |
|
72 |
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
|
73 |
def warn(self, *msg): |
565 | 74 |
self.write_err(*msg) |
207 | 75 |
def note(self, *msg): |
76 |
if self.verbose: self.write(*msg) |
|
77 |
def debug(self, *msg): |
|
78 |
if self.debugflag: self.write(*msg) |
|
79 |
def edit(self, text): |
|
249 | 80 |
import tempfile |
207 | 81 |
(fd, name) = tempfile.mkstemp("hg") |
82 |
f = os.fdopen(fd, "w") |
|
83 |
f.write(text) |
|
84 |
f.close() |
|
85 |
||
86 |
editor = os.environ.get("HGEDITOR") or os.environ.get("EDITOR", "vi") |
|
508 | 87 |
util.system("%s %s" % (editor, name), errprefix = "edit failed") |
207 | 88 |
|
89 |
t = open(name).read() |
|
90 |
t = re.sub("(?m)^HG:.*\n", "", t) |
|
91 |
||
92 |
return t |