Show More
@@ -1,56 +1,80 b'' | |||||
1 | # ui.py - user interface bits for mercurial |
|
1 | # ui.py - user interface bits for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms |
|
5 | # This software may be used and distributed according to the terms | |
6 | # of the GNU General Public License, incorporated herein by reference. |
|
6 | # of the GNU General Public License, incorporated herein by reference. | |
7 |
|
7 | |||
8 | import os, sys, re |
|
8 | import os, sys, re, ConfigParser | |
9 |
|
9 | |||
10 | class ui: |
|
10 | class ui: | |
11 | def __init__(self, verbose=False, debug=False, quiet=False, |
|
11 | def __init__(self, verbose=False, debug=False, quiet=False, | |
12 | interactive=True): |
|
12 | interactive=True): | |
13 | self.quiet = quiet and not verbose and not debug |
|
13 | self.cdata = ConfigParser.SafeConfigParser() | |
14 | self.verbose = verbose or debug |
|
14 | self.cdata.read(os.path.expanduser("~/.hgrc")) | |
15 | self.debugflag = debug |
|
15 | ||
16 | self.interactive = interactive |
|
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 | ||||
|
26 | def config(self, section, val, default=None): | |||
|
27 | if self.cdata.has_option(section, val): | |||
|
28 | return self.cdata.get(section, val) | |||
|
29 | return default | |||
|
30 | ||||
|
31 | def configbool(self, section, val, default=False): | |||
|
32 | if self.cdata.has_option(section, val): | |||
|
33 | return self.cdata.getboolean(section, val) | |||
|
34 | return default | |||
|
35 | ||||
|
36 | def configitems(self, section): | |||
|
37 | if self.cdata.has_section(section): | |||
|
38 | return self.cdata.items(section) | |||
|
39 | return [] | |||
|
40 | ||||
17 | def write(self, *args): |
|
41 | def write(self, *args): | |
18 | for a in args: |
|
42 | for a in args: | |
19 | sys.stdout.write(str(a)) |
|
43 | sys.stdout.write(str(a)) | |
20 | def readline(self): |
|
44 | def readline(self): | |
21 | return sys.stdin.readline()[:-1] |
|
45 | return sys.stdin.readline()[:-1] | |
22 | def prompt(self, msg, pat, default = "y"): |
|
46 | def prompt(self, msg, pat, default = "y"): | |
23 | if not self.interactive: return default |
|
47 | if not self.interactive: return default | |
24 | while 1: |
|
48 | while 1: | |
25 | self.write(msg, " ") |
|
49 | self.write(msg, " ") | |
26 | r = self.readline() |
|
50 | r = self.readline() | |
27 | if re.match(pat, r): |
|
51 | if re.match(pat, r): | |
28 | return r |
|
52 | return r | |
29 | else: |
|
53 | else: | |
30 | self.write("unrecognized response\n") |
|
54 | self.write("unrecognized response\n") | |
31 | def status(self, *msg): |
|
55 | def status(self, *msg): | |
32 | if not self.quiet: self.write(*msg) |
|
56 | if not self.quiet: self.write(*msg) | |
33 | def warn(self, *msg): |
|
57 | def warn(self, *msg): | |
34 | self.write(*msg) |
|
58 | self.write(*msg) | |
35 | def note(self, *msg): |
|
59 | def note(self, *msg): | |
36 | if self.verbose: self.write(*msg) |
|
60 | if self.verbose: self.write(*msg) | |
37 | def debug(self, *msg): |
|
61 | def debug(self, *msg): | |
38 | if self.debugflag: self.write(*msg) |
|
62 | if self.debugflag: self.write(*msg) | |
39 | def edit(self, text): |
|
63 | def edit(self, text): | |
40 | import tempfile |
|
64 | import tempfile | |
41 | (fd, name) = tempfile.mkstemp("hg") |
|
65 | (fd, name) = tempfile.mkstemp("hg") | |
42 | f = os.fdopen(fd, "w") |
|
66 | f = os.fdopen(fd, "w") | |
43 | f.write(text) |
|
67 | f.write(text) | |
44 | f.close() |
|
68 | f.close() | |
45 |
|
69 | |||
46 | editor = os.environ.get("HGEDITOR") or os.environ.get("EDITOR", "vi") |
|
70 | editor = os.environ.get("HGEDITOR") or os.environ.get("EDITOR", "vi") | |
47 | r = os.system("%s %s" % (editor, name)) |
|
71 | r = os.system("%s %s" % (editor, name)) | |
48 |
|
72 | |||
49 | if r: |
|
73 | if r: | |
50 | raise "Edit failed!" |
|
74 | raise "Edit failed!" | |
51 |
|
75 | |||
52 | t = open(name).read() |
|
76 | t = open(name).read() | |
53 | t = re.sub("(?m)^HG:.*\n", "", t) |
|
77 | t = re.sub("(?m)^HG:.*\n", "", t) | |
54 |
|
78 | |||
55 | return t |
|
79 | return t | |
56 |
|
80 |
General Comments 0
You need to be logged in to leave comments.
Login now