comparison mercurial/ui.py @ 49794:25fe689a4a64

typing: add type hints related to progress bars in mercurial/ui.py Pretty low hanging fruit while trying to deal with other more complicated parts of this module.
author Matt Harbison <matt_harbison@yahoo.com>
date Sat, 10 Dec 2022 00:22:13 -0500
parents 8147abc05794
children f1e820cda2f5
comparison
equal deleted inserted replaced
49793:8147abc05794 49794:25fe689a4a64
16 import signal 16 import signal
17 import socket 17 import socket
18 import subprocess 18 import subprocess
19 import sys 19 import sys
20 import traceback 20 import traceback
21
22 from typing import (
23 Optional,
24 )
21 25
22 from .i18n import _ 26 from .i18n import _
23 from .node import hex 27 from .node import hex
24 from .pycompat import ( 28 from .pycompat import (
25 getattr, 29 getattr,
2055 return encoding.environ.get(b"HGEDITOR") or self.config( 2059 return encoding.environ.get(b"HGEDITOR") or self.config(
2056 b"ui", b"editor", editor 2060 b"ui", b"editor", editor
2057 ) 2061 )
2058 2062
2059 @util.propertycache 2063 @util.propertycache
2060 def _progbar(self): 2064 def _progbar(self) -> Optional[progress.progbar]:
2061 """setup the progbar singleton to the ui object""" 2065 """setup the progbar singleton to the ui object"""
2062 if ( 2066 if (
2063 self.quiet 2067 self.quiet
2064 or self.debugflag 2068 or self.debugflag
2065 or self.configbool(b'progress', b'disable') 2069 or self.configbool(b'progress', b'disable')
2066 or not progress.shouldprint(self) 2070 or not progress.shouldprint(self)
2067 ): 2071 ):
2068 return None 2072 return None
2069 return getprogbar(self) 2073 return getprogbar(self)
2070 2074
2071 def _progclear(self): 2075 def _progclear(self) -> None:
2072 """clear progress bar output if any. use it before any output""" 2076 """clear progress bar output if any. use it before any output"""
2073 if not haveprogbar(): # nothing loaded yet 2077 if not haveprogbar(): # nothing loaded yet
2074 return 2078 return
2075 if self._progbar is not None and self._progbar.printed: 2079 if self._progbar is not None and self._progbar.printed:
2076 self._progbar.clear() 2080 self._progbar.clear()
2077 2081
2078 def makeprogress(self, topic, unit=b"", total=None): 2082 def makeprogress(
2083 self, topic: bytes, unit: bytes = b"", total: Optional[int] = None
2084 ) -> scmutil.progress:
2079 """Create a progress helper for the specified topic""" 2085 """Create a progress helper for the specified topic"""
2080 if getattr(self._fmsgerr, 'structured', False): 2086 if getattr(self._fmsgerr, 'structured', False):
2081 # channel for machine-readable output with metadata, just send 2087 # channel for machine-readable output with metadata, just send
2082 # raw information 2088 # raw information
2083 # TODO: consider porting some useful information (e.g. estimated 2089 # TODO: consider porting some useful information (e.g. estimated
2247 return util._estimatememory() 2253 return util._estimatememory()
2248 2254
2249 2255
2250 # we instantiate one globally shared progress bar to avoid 2256 # we instantiate one globally shared progress bar to avoid
2251 # competing progress bars when multiple UI objects get created 2257 # competing progress bars when multiple UI objects get created
2252 _progresssingleton = None 2258 _progresssingleton: Optional[progress.progbar] = None
2253 2259
2254 2260
2255 def getprogbar(ui): 2261 def getprogbar(ui: ui) -> progress.progbar:
2256 global _progresssingleton 2262 global _progresssingleton
2257 if _progresssingleton is None: 2263 if _progresssingleton is None:
2258 # passing 'ui' object to the singleton is fishy, 2264 # passing 'ui' object to the singleton is fishy,
2259 # this is how the extension used to work but feel free to rework it. 2265 # this is how the extension used to work but feel free to rework it.
2260 _progresssingleton = progress.progbar(ui) 2266 _progresssingleton = progress.progbar(ui)
2261 return _progresssingleton 2267 return _progresssingleton
2262 2268
2263 2269
2264 def haveprogbar(): 2270 def haveprogbar() -> bool:
2265 return _progresssingleton is not None 2271 return _progresssingleton is not None
2266 2272
2267 2273
2268 def _selectmsgdests(ui): 2274 def _selectmsgdests(ui):
2269 name = ui.config(b'ui', b'message-output') 2275 name = ui.config(b'ui', b'message-output')