##// END OF EJS Templates
templates: adding a config() function for template customization...
rdamazio@google.com -
r41869:67046961 default
parent child Browse files
Show More
@@ -1081,6 +1081,10 b" coreconfigitem('templates', '.*',"
1081 default=None,
1081 default=None,
1082 generic=True,
1082 generic=True,
1083 )
1083 )
1084 coreconfigitem('templateconfig', '.*',
1085 default=dynamicdefault,
1086 generic=True,
1087 )
1084 coreconfigitem('trusted', 'groups',
1088 coreconfigitem('trusted', 'groups',
1085 default=list,
1089 default=list,
1086 )
1090 )
@@ -295,6 +295,39 b' def get(context, mapping, args):'
295 hint = _("get() expects a dict as first argument")
295 hint = _("get() expects a dict as first argument")
296 raise error.ParseError(bytes(err), hint=hint)
296 raise error.ParseError(bytes(err), hint=hint)
297
297
298 @templatefunc('config(section, name[, default])', requires={'ui'})
299 def config(context, mapping, args):
300 """Returns the requested hgrc config option as a string."""
301 fn = context.resource(mapping, 'ui').config
302 return _config(context, mapping, args, fn, evalstring)
303
304 @templatefunc('configbool(section, name[, default])', requires={'ui'})
305 def configbool(context, mapping, args):
306 """Returns the requested hgrc config option as a boolean."""
307 fn = context.resource(mapping, 'ui').configbool
308 return _config(context, mapping, args, fn, evalboolean)
309
310 @templatefunc('configint(section, name[, default])', requires={'ui'})
311 def configint(context, mapping, args):
312 """Returns the requested hgrc config option as an integer."""
313 fn = context.resource(mapping, 'ui').configint
314 return _config(context, mapping, args, fn, evalinteger)
315
316 def _config(context, mapping, args, configfn, defaultfn):
317 if not (2 <= len(args) <= 3):
318 raise error.ParseError(_("config expects two or three arguments"))
319
320 # The config option can come from any section, though we specifically
321 # reserve the [templateconfig] section for dynamically defining options
322 # for this function without also requiring an extension.
323 section = evalstringliteral(context, mapping, args[0])
324 name = evalstringliteral(context, mapping, args[1])
325 if len(args) == 3:
326 default = defaultfn(context, mapping, args[2])
327 return configfn(section, name, default)
328 else:
329 return configfn(section, name)
330
298 @templatefunc('if(expr, then[, else])')
331 @templatefunc('if(expr, then[, else])')
299 def if_(context, mapping, args):
332 def if_(context, mapping, args):
300 """Conditionally execute based on the result of
333 """Conditionally execute based on the result of
@@ -1549,4 +1549,31 b' pad width:'
1549 $ HGENCODING=utf-8 hg debugtemplate "{pad('`cat utf-8`', 2, '-')}\n"
1549 $ HGENCODING=utf-8 hg debugtemplate "{pad('`cat utf-8`', 2, '-')}\n"
1550 \xc3\xa9- (esc)
1550 \xc3\xa9- (esc)
1551
1551
1552 read config options:
1553
1554 $ hg log -T "{config('templateconfig', 'knob', 'foo')}\n"
1555 foo
1556 $ hg log -T "{config('templateconfig', 'knob', 'foo')}\n" \
1557 > --config templateconfig.knob=bar
1558 bar
1559 $ hg log -T "{configbool('templateconfig', 'knob', True)}\n"
1560 True
1561 $ hg log -T "{configbool('templateconfig', 'knob', True)}\n" \
1562 > --config templateconfig.knob=0
1563 False
1564 $ hg log -T "{configint('templateconfig', 'knob', 123)}\n"
1565 123
1566 $ hg log -T "{configint('templateconfig', 'knob', 123)}\n" \
1567 > --config templateconfig.knob=456
1568 456
1569 $ hg log -T "{config('templateconfig', 'knob')}\n"
1570 devel-warn: config item requires an explicit default value: 'templateconfig.knob' at: * (glob)
1571
1572 $ hg log -T "{configbool('ui', 'interactive')}\n"
1573 False
1574 $ hg log -T "{configbool('ui', 'interactive')}\n" --config ui.interactive=1
1575 True
1576 $ hg log -T "{config('templateconfig', 'knob', if(true, 'foo', 'bar'))}\n"
1577 foo
1578
1552 $ cd ..
1579 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now