Mercurial > hg
comparison mercurial/ui.py @ 31954:e518192d6bac
pager: set some environment variables if they're not set
Git did this already [1] [2]. We want this behavior too [3].
This provides a better default user experience (like, supporting colors) if
users have things like "PAGER=less" set, which is not uncommon.
The environment variables are provided by a method so extensions can
override them on demand.
[1]: https://github.com/git/git/blob/6a5ff7acb5965718cc7016c0ab6c601454fd7cde/pager.c#L87
[2]: https://github.com/git/git/blob/6a5ff7acb5965718cc7016c0ab6c601454fd7cde/Makefile#L1545
[3]: https://www.mercurial-scm.org/pipermail/mercurial-devel/2017-March/094780.html
author | Jun Wu <quark@fb.com> |
---|---|
date | Thu, 13 Apr 2017 08:27:19 -0700 |
parents | ac69675fff1c |
children | c13ff31818b0 |
comparison
equal
deleted
inserted
replaced
31953:cc2382b60007 | 31954:e518192d6bac |
---|---|
852 fallbackpager = 'more' | 852 fallbackpager = 'more' |
853 pagercmd = self.config('pager', 'pager', fallbackpager) | 853 pagercmd = self.config('pager', 'pager', fallbackpager) |
854 if not pagercmd: | 854 if not pagercmd: |
855 return | 855 return |
856 | 856 |
857 pagerenv = {} | |
858 for name, value in rcutil.defaultpagerenv().items(): | |
859 if name not in encoding.environ: | |
860 pagerenv[name] = value | |
861 | |
857 self.debug('starting pager for command %r\n' % command) | 862 self.debug('starting pager for command %r\n' % command) |
858 self.flush() | 863 self.flush() |
859 | 864 |
860 wasformatted = self.formatted() | 865 wasformatted = self.formatted() |
861 if util.safehasattr(signal, "SIGPIPE"): | 866 if util.safehasattr(signal, "SIGPIPE"): |
862 signal.signal(signal.SIGPIPE, _catchterm) | 867 signal.signal(signal.SIGPIPE, _catchterm) |
863 if self._runpager(pagercmd): | 868 if self._runpager(pagercmd, pagerenv): |
864 self.pageractive = True | 869 self.pageractive = True |
865 # Preserve the formatted-ness of the UI. This is important | 870 # Preserve the formatted-ness of the UI. This is important |
866 # because we mess with stdout, which might confuse | 871 # because we mess with stdout, which might confuse |
867 # auto-detection of things being formatted. | 872 # auto-detection of things being formatted. |
868 self.setconfig('ui', 'formatted', wasformatted, 'pager') | 873 self.setconfig('ui', 'formatted', wasformatted, 'pager') |
877 # If the pager can't be spawned in dispatch when --pager=on is | 882 # If the pager can't be spawned in dispatch when --pager=on is |
878 # given, don't try again when the command runs, to avoid a duplicate | 883 # given, don't try again when the command runs, to avoid a duplicate |
879 # warning about a missing pager command. | 884 # warning about a missing pager command. |
880 self.disablepager() | 885 self.disablepager() |
881 | 886 |
882 def _runpager(self, command): | 887 def _runpager(self, command, env=None): |
883 """Actually start the pager and set up file descriptors. | 888 """Actually start the pager and set up file descriptors. |
884 | 889 |
885 This is separate in part so that extensions (like chg) can | 890 This is separate in part so that extensions (like chg) can |
886 override how a pager is invoked. | 891 override how a pager is invoked. |
887 """ | 892 """ |
910 | 915 |
911 try: | 916 try: |
912 pager = subprocess.Popen( | 917 pager = subprocess.Popen( |
913 command, shell=shell, bufsize=-1, | 918 command, shell=shell, bufsize=-1, |
914 close_fds=util.closefds, stdin=subprocess.PIPE, | 919 close_fds=util.closefds, stdin=subprocess.PIPE, |
915 stdout=util.stdout, stderr=util.stderr) | 920 stdout=util.stdout, stderr=util.stderr, |
921 env=util.shellenviron(env)) | |
916 except OSError as e: | 922 except OSError as e: |
917 if e.errno == errno.ENOENT and not shell: | 923 if e.errno == errno.ENOENT and not shell: |
918 self.warn(_("missing pager command '%s', skipping pager\n") | 924 self.warn(_("missing pager command '%s', skipping pager\n") |
919 % command) | 925 % command) |
920 return False | 926 return False |