Coverage for sources/mimeogram/display.py: 34%

32 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-16 01:42 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2# -*- coding: utf-8 -*- 

3 

4#============================================================================# 

5# # 

6# Licensed under the Apache License, Version 2.0 (the "License"); # 

7# you may not use this file except in compliance with the License. # 

8# You may obtain a copy of the License at # 

9# # 

10# http://www.apache.org/licenses/LICENSE-2.0 # 

11# # 

12# Unless required by applicable law or agreed to in writing, software # 

13# distributed under the License is distributed on an "AS IS" BASIS, # 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # 

15# See the License for the specific language governing permissions and # 

16# limitations under the License. # 

17# # 

18#============================================================================# 

19 

20 

21''' System pager interaction. ''' 

22 

23 

24from __future__ import annotations 

25 

26from . import __ 

27 

28 

29_scribe = __.produce_scribe( __name__ ) 

30 

31 

32def discover_pager( ) -> __.cabc.Callable[ [ str ], None ]: 

33 ''' Discovers pager and returns executor function. ''' 

34 from shutil import which 

35 from subprocess import run # nosec B404 

36 # TODO: Better default for Windows. 

37 pager = __.os.environ.get( 'PAGER', 'less' ) 

38 # TODO: Platform-specific list. 

39 for pager_ in ( pager, 'less', 'more' ): 

40 if which( pager_ ): 

41 pager = pager_ 

42 break 

43 else: pager = '' 

44 

45 if pager: 

46 

47 # TODO? async 

48 def pager_executor( filename: str ) -> None: 

49 ''' Executes pager with file. ''' 

50 run( ( pager, filename ), check = True ) # nosec B603 

51 

52 return pager_executor 

53 

54 # TODO? async 

55 def console_display( filename: str ) -> None: 

56 ''' Prints file to stdout and waits for ENTER key. ''' 

57 with open( filename, 'r', encoding = 'utf-8' ) as stream: 

58 content = stream.read( ) 

59 print( f"\n\n{content}\n\n" ) 

60 if __.sys.stdin.isatty( ): input( "Press Enter to continue..." ) 

61 

62 _scribe.warning( "Could not find pager program for display." ) 

63 return console_display 

64 

65 

66def display_content( 

67 content: str, *, 

68 suffix: str = '.txt', 

69 pager_discoverer: __.cabc.Callable[ 

70 [ ], __.cabc.Callable[ [ str ], None ] ] = discover_pager, 

71) -> None: 

72 ''' Displays content via discovered pager. ''' 

73 from .exceptions import PagerFailure 

74 pager = pager_discoverer( ) 

75 import tempfile 

76 with tempfile.NamedTemporaryFile( mode = 'w', suffix = suffix ) as tmp: 

77 tmp.write( content ) 

78 tmp.flush( ) 

79 try: pager( tmp.name ) 

80 except Exception as exc: raise PagerFailure( cause = exc ) from exc