mercurial/configuration/command.py
changeset 52419 04c3fb885fb6
parent 52418 e98cea8fc858
child 52425 3e79ca017157
equal deleted inserted replaced
52418:e98cea8fc858 52419:04c3fb885fb6
     2 
     2 
     3 from __future__ import annotations
     3 from __future__ import annotations
     4 
     4 
     5 import os
     5 import os
     6 
     6 
     7 from typing import Any, Dict, Optional
     7 from typing import Any, Collection, Dict, Optional
     8 
     8 
     9 from ..i18n import _
     9 from ..i18n import _
    10 
    10 
    11 from .. import (
    11 from .. import (
    12     cmdutil,
    12     cmdutil,
    13     error,
    13     error,
       
    14     formatter,
       
    15     pycompat,
    14     requirements,
    16     requirements,
    15     ui as uimod,
    17     ui as uimod,
    16     util,
    18     util,
    17     vfs as vfsmod,
    19     vfs as vfsmod,
    18 )
    20 )
   122         elif t == b'items':
   124         elif t == b'items':
   123             # Don't print anything for 'items'.
   125             # Don't print anything for 'items'.
   124             pass
   126             pass
   125         else:
   127         else:
   126             raise error.ProgrammingError(b'unknown rctype: %s' % t)
   128             raise error.ProgrammingError(b'unknown rctype: %s' % t)
       
   129 
       
   130 
       
   131 def show_config(
       
   132     ui: uimod.ui,
       
   133     repo,
       
   134     value_filters: Collection[bytes],
       
   135     formatter_options: dict,
       
   136     untrusted: bool = False,
       
   137     all_known: bool = False,
       
   138     show_source: bool = False,
       
   139 ) -> bool:
       
   140     """Display config value to the user
       
   141 
       
   142     The display is done using a dedicated `formatter` object.
       
   143 
       
   144 
       
   145     :value_filters:
       
   146         if non-empty filter the display value according to these filters. If
       
   147         the filter does not match any value, the function return False. True
       
   148         otherwise.
       
   149 
       
   150     :formatter_option:
       
   151         options passed to the formatter
       
   152 
       
   153     :untrusted:
       
   154         When set, use untrusted value instead of ignoring them
       
   155 
       
   156     :all_known:
       
   157         Display all known config item, not just the one with an explicit value.
       
   158 
       
   159     :show_source:
       
   160         Show where each value has been defined.
       
   161     """
       
   162     fm = ui.formatter(b'config', formatter_options)
       
   163     selsections = selentries = []
       
   164     filtered = False
       
   165     if value_filters:
       
   166         selsections = [v for v in value_filters if b'.' not in v]
       
   167         selentries = [v for v in value_filters if b'.' in v]
       
   168         filtered = True
       
   169     uniquesel = len(selentries) == 1 and not selsections
       
   170     selsections = set(selsections)
       
   171     selentries = set(selentries)
       
   172 
       
   173     matched = False
       
   174     entries = ui.walkconfig(untrusted=untrusted, all_known=all_known)
       
   175     for section, name, value in entries:
       
   176         source = ui.configsource(section, name, untrusted)
       
   177         value = pycompat.bytestr(value)
       
   178         defaultvalue = ui.configdefault(section, name)
       
   179         if fm.isplain():
       
   180             source = source or b'none'
       
   181             value = value.replace(b'\n', b'\\n')
       
   182         entryname = section + b'.' + name
       
   183         if filtered and not (section in selsections or entryname in selentries):
       
   184             continue
       
   185         fm.startitem()
       
   186         fm.condwrite(show_source, b'source', b'%s: ', source)
       
   187         if uniquesel:
       
   188             fm.data(name=entryname)
       
   189             fm.write(b'value', b'%s\n', value)
       
   190         else:
       
   191             fm.write(b'name value', b'%s=%s\n', entryname, value)
       
   192         if formatter.isprintable(defaultvalue):
       
   193             fm.data(defaultvalue=defaultvalue)
       
   194         elif isinstance(defaultvalue, list) and all(
       
   195             formatter.isprintable(e) for e in defaultvalue
       
   196         ):
       
   197             fm.data(defaultvalue=fm.formatlist(defaultvalue, name=b'value'))
       
   198         # TODO: no idea how to process unsupported defaultvalue types
       
   199         matched = True
       
   200     fm.end()
       
   201     return matched