##// END OF EJS Templates
cmdutil: replace showlist() closure with a function
Patrick Mezard -
r10053:5c5c6295 default
parent child Browse files
Show More
@@ -0,0 +1,69 b''
1 # templatekw.py - common changeset template keywords
2 #
3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com>
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2, incorporated herein by reference.
7
8
9 def showlist(templ, name, values, plural=None, **args):
10 '''expand set of values.
11 name is name of key in template map.
12 values is list of strings or dicts.
13 plural is plural of name, if not simply name + 's'.
14
15 expansion works like this, given name 'foo'.
16
17 if values is empty, expand 'no_foos'.
18
19 if 'foo' not in template map, return values as a string,
20 joined by space.
21
22 expand 'start_foos'.
23
24 for each value, expand 'foo'. if 'last_foo' in template
25 map, expand it instead of 'foo' for last key.
26
27 expand 'end_foos'.
28 '''
29 if plural: names = plural
30 else: names = name + 's'
31 if not values:
32 noname = 'no_' + names
33 if noname in templ:
34 yield templ(noname, **args)
35 return
36 if name not in templ:
37 if isinstance(values[0], str):
38 yield ' '.join(values)
39 else:
40 for v in values:
41 yield dict(v, **args)
42 return
43 startname = 'start_' + names
44 if startname in templ:
45 yield templ(startname, **args)
46 vargs = args.copy()
47 def one(v, tag=name):
48 try:
49 vargs.update(v)
50 except (AttributeError, ValueError):
51 try:
52 for a, b in v:
53 vargs[a] = b
54 except ValueError:
55 vargs[name] = v
56 return templ(tag, **vargs)
57 lastname = 'last_' + name
58 if lastname in templ:
59 last = values.pop()
60 else:
61 last = None
62 for v in values:
63 yield one(v)
64 if last is not None:
65 yield one(last, tag=lastname)
66 endname = 'end_' + names
67 if endname in templ:
68 yield templ(endname, **args)
69
@@ -8,7 +8,7 b''
8 8 from node import hex, nullid, nullrev, short
9 9 from i18n import _
10 10 import os, sys, errno, re, glob
11 import mdiff, bdiff, util, templater, patch, error, encoding
11 import mdiff, bdiff, util, templater, patch, error, encoding, templatekw
12 12 import match as _match
13 13
14 14 revrangesep = ':'
@@ -810,90 +810,32 b' class changeset_templater(changeset_prin'
810 810 def _show(self, ctx, copies, props):
811 811 '''show a single changeset or file revision'''
812 812
813 def showlist(name, values, plural=None, **args):
814 '''expand set of values.
815 name is name of key in template map.
816 values is list of strings or dicts.
817 plural is plural of name, if not simply name + 's'.
818
819 expansion works like this, given name 'foo'.
820
821 if values is empty, expand 'no_foos'.
822
823 if 'foo' not in template map, return values as a string,
824 joined by space.
825
826 expand 'start_foos'.
827
828 for each value, expand 'foo'. if 'last_foo' in template
829 map, expand it instead of 'foo' for last key.
813 showlist = templatekw.showlist
830 814
831 expand 'end_foos'.
832 '''
833 if plural: names = plural
834 else: names = name + 's'
835 if not values:
836 noname = 'no_' + names
837 if noname in self.t:
838 yield self.t(noname, **args)
839 return
840 if name not in self.t:
841 if isinstance(values[0], str):
842 yield ' '.join(values)
843 else:
844 for v in values:
845 yield dict(v, **args)
846 return
847 startname = 'start_' + names
848 if startname in self.t:
849 yield self.t(startname, **args)
850 vargs = args.copy()
851 def one(v, tag=name):
852 try:
853 vargs.update(v)
854 except (AttributeError, ValueError):
855 try:
856 for a, b in v:
857 vargs[a] = b
858 except ValueError:
859 vargs[name] = v
860 return self.t(tag, **vargs)
861 lastname = 'last_' + name
862 if lastname in self.t:
863 last = values.pop()
864 else:
865 last = None
866 for v in values:
867 yield one(v)
868 if last is not None:
869 yield one(last, tag=lastname)
870 endname = 'end_' + names
871 if endname in self.t:
872 yield self.t(endname, **args)
873
874 def showbranches(**args):
815 def showbranches(templ, **args):
875 816 branch = ctx.branch()
876 817 if branch != 'default':
877 818 branch = encoding.tolocal(branch)
878 return showlist('branch', [branch], plural='branches', **args)
819 return showlist(templ, 'branch', [branch], plural='branches',
820 **args)
879 821
880 def showparents(**args):
822 def showparents(templ, **args):
881 823 parents = [[('rev', p.rev()), ('node', p.hex())]
882 824 for p in self._meaningful_parentrevs(ctx)]
883 return showlist('parent', parents, **args)
825 return showlist(templ, 'parent', parents, **args)
884 826
885 def showtags(**args):
886 return showlist('tag', ctx.tags(), **args)
827 def showtags(templ, **args):
828 return showlist(templ, 'tag', ctx.tags(), **args)
887 829
888 def showextras(**args):
830 def showextras(templ, **args):
889 831 for key, value in sorted(ctx.extra().items()):
890 832 args = args.copy()
891 833 args.update(dict(key=key, value=value))
892 yield self.t('extra', **args)
834 yield templ('extra', **args)
893 835
894 def showcopies(**args):
836 def showcopies(templ, **args):
895 837 c = [{'name': x[0], 'source': x[1]} for x in copies]
896 return showlist('file_copy', c, plural='file_copies', **args)
838 return showlist(templ, 'file_copy', c, plural='file_copies', **args)
897 839
898 840 files = []
899 841 def getfiles():
@@ -901,21 +843,21 b' class changeset_templater(changeset_prin'
901 843 files[:] = self.repo.status(ctx.parents()[0].node(),
902 844 ctx.node())[:3]
903 845 return files
904 def showfiles(**args):
905 return showlist('file', ctx.files(), **args)
906 def showmods(**args):
907 return showlist('file_mod', getfiles()[0], **args)
908 def showadds(**args):
909 return showlist('file_add', getfiles()[1], **args)
910 def showdels(**args):
911 return showlist('file_del', getfiles()[2], **args)
912 def showmanifest(**args):
846 def showfiles(templ, **args):
847 return showlist(templ, 'file', ctx.files(), **args)
848 def showmods(templ, **args):
849 return showlist(templ, 'file_mod', getfiles()[0], **args)
850 def showadds(templ, **args):
851 return showlist(templ, 'file_add', getfiles()[1], **args)
852 def showdels(templ, **args):
853 return showlist(templ, 'file_del', getfiles()[2], **args)
854 def showmanifest(templ, **args):
913 855 args = args.copy()
914 856 args.update(dict(rev=self.repo.manifest.rev(ctx.changeset()[0]),
915 857 node=hex(ctx.changeset()[0])))
916 return self.t('manifest', **args)
858 return templ('manifest', **args)
917 859
918 def showdiffstat(**args):
860 def showdiffstat(templ, **args):
919 861 diff = patch.diff(self.repo, ctx.parents()[0].node(), ctx.node())
920 862 files, adds, removes = 0, 0, 0
921 863 for i in patch.diffstatdata(util.iterlines(diff)):
@@ -924,9 +866,9 b' class changeset_templater(changeset_prin'
924 866 removes += i[2]
925 867 return '%s: +%s/-%s' % (files, adds, removes)
926 868
927 def showlatesttag(**args):
869 def showlatesttag(templ, **args):
928 870 return self._latesttaginfo(ctx.rev())[2]
929 def showlatesttagdistance(**args):
871 def showlatesttagdistance(templ, **args):
930 872 return self._latesttaginfo(ctx.rev())[1]
931 873
932 874 defprops = {
@@ -951,6 +893,7 b' class changeset_templater(changeset_prin'
951 893 }
952 894 props = props.copy()
953 895 props.update(defprops)
896 props['templ'] = self.t
954 897
955 898 # find correct templates for current mode
956 899
General Comments 0
You need to be logged in to leave comments. Login now