1
1
"""Module to contain logic to generate the banner for ACA-py."""
2
2
3
- import sys
3
+ import logging
4
4
import textwrap
5
5
from contextlib import contextmanager
6
6
from enum import Enum , auto
7
7
from typing import Optional , TextIO
8
8
9
+ logger = logging .getLogger (__name__ )
10
+
9
11
10
12
@contextmanager
11
13
def Banner (border : str , length : int , file : Optional [TextIO ] = None ):
12
14
"""Context manager to generate a banner for ACA-py."""
13
15
banner = _Banner (border , length , file )
14
- banner .print_border ()
16
+ banner .add_border ()
15
17
yield banner
16
- banner .print_border ()
18
+ banner .add_border ()
19
+
20
+ # Join all lines with newlines and log them
21
+ banner_text = "\n " .join (banner .lines )
22
+ banner_text = f"\n { banner_text .strip ()} \n " # Start/end with a newline
23
+ if file :
24
+ print (banner_text , file = file )
25
+ else :
26
+ logger .info (banner_text )
17
27
18
28
19
29
class _Banner :
@@ -34,11 +44,12 @@ def __init__(self, border: str, length: int, file: Optional[TextIO] = None):
34
44
"""
35
45
self .border = border
36
46
self .length = length
37
- self .file = file or sys .stdout
47
+ self .file = file
48
+ self .lines = []
38
49
39
- def _print (self , text : str ):
40
- """Print value ."""
41
- print ( text , file = self .file )
50
+ def _add_line (self , text : str ):
51
+ """Add a line to the banner ."""
52
+ self .lines . append ( text )
42
53
43
54
def _lr_pad (self , content : str ):
44
55
"""Pad string content with defined border character.
@@ -48,80 +59,87 @@ def _lr_pad(self, content: str):
48
59
"""
49
60
return f"{ self .border } { self .border } { content } { self .border } { self .border } "
50
61
51
- def _print_line (self , text : str , alignment : align = align .LEFT ):
52
- """Print line."""
62
+ def _format_line (self , text : str , alignment : align = align .LEFT ):
63
+ """Format a line with the specified alignment ."""
53
64
lines = textwrap .wrap (text , width = self .length )
65
+ formatted_lines = []
66
+
54
67
for line in lines :
55
68
if len (line ) < self .length :
56
69
if alignment == self .align .LEFT :
57
- left = ""
58
- right = " " * ( self .length - len ( line ))
70
+ # Left alignment
71
+ formatted_line = f" { line :<{ self .length } } "
59
72
elif alignment == self .align .CENTER :
60
- left = " " * ((self .length - len (line )) // 2 )
61
- right = " " * ((self .length - len (line )) // 2 )
62
- if len (line ) % 2 != 0 :
63
- right += " "
73
+ # Center alignment
74
+ total_padding = self .length - len (line )
75
+ left_padding = total_padding // 2
76
+ right_padding = total_padding - left_padding
77
+ formatted_line = f"{ ' ' * left_padding } { line } { ' ' * right_padding } "
64
78
elif alignment == self .align .RIGHT :
65
- left = " " * ( self . length - len ( line ))
66
- right = " "
79
+ # Right alignment
80
+ formatted_line = f" { line :>{ self . length } } "
67
81
else :
68
82
raise ValueError (f"Invalid alignment: { alignment } " )
69
- line = f"{ left } { line } { right } "
70
- self ._print (self ._lr_pad (line ))
83
+ else :
84
+ formatted_line = line
85
+
86
+ formatted_lines .append (self ._lr_pad (formatted_line ))
87
+
88
+ return formatted_lines
71
89
72
- def print_border (self ):
73
- """Print a full line using the border character."""
74
- self ._print (self .border * (self .length + 6 ))
90
+ def add_border (self ):
91
+ """Add a full line using the border character."""
92
+ self ._add_line (self .border * (self .length + 6 ))
75
93
76
94
def title (self , title , spacing_after : int = 2 ):
77
- """Print the main title element."""
78
- self ._print_line ( title , self .align .CENTER )
95
+ """Add the main title element."""
96
+ self .lines . extend ( self . _format_line ( title , self .align .CENTER ) )
79
97
for _ in range (spacing_after ):
80
98
self .spacer ()
81
99
82
100
def spacer (self ):
83
- """Print an empty line with the border character only."""
84
- self ._print (self ._lr_pad (" " * self .length ))
101
+ """Add an empty line with the border character only."""
102
+ self ._add_line (self ._lr_pad (" " * self .length ))
85
103
86
104
def hr (self , char : str = "-" ):
87
- """Print a line with a horizontal rule."""
88
- self ._print (self ._lr_pad (char * self .length ))
105
+ """Add a line with a horizontal rule."""
106
+ self ._add_line (self ._lr_pad (char * self .length ))
89
107
90
108
def subtitle (self , title : str , spacing_after : int = 1 ):
91
- """Print a subtitle for a section."""
109
+ """Add a subtitle for a section."""
92
110
title += ":"
93
- self ._print_line ( title , self .align .LEFT )
111
+ self .lines . extend ( self . _format_line ( title , self .align .LEFT ) )
94
112
for _ in range (spacing_after ):
95
113
self .spacer ()
96
114
97
115
def list (self , items , spacing_after : int = 1 ):
98
- """Print a list of items, prepending a dash to each item."""
116
+ """Add a list of items, prepending a dash to each item."""
99
117
for item in items :
100
- self ._print_line ( f" - { item } " , self .align .LEFT )
118
+ self .lines . extend ( self . _format_line ( f" - { item } " , self .align .LEFT ) )
101
119
102
120
for _ in range (spacing_after ):
103
121
self .spacer ()
104
122
105
123
def version (self , version ):
106
- """Print the current ``version``."""
124
+ """Add the current ``version``."""
107
125
version = f"ver: { version } "
108
- self ._print_line ( version , self .align .RIGHT )
126
+ self .lines . extend ( self . _format_line ( version , self .align .RIGHT ) )
109
127
110
128
def print (self , text : str ):
111
- """Print a line of text."""
112
- self ._print_line ( text , self .align .LEFT )
129
+ """Add a line of text."""
130
+ self .lines . extend ( self . _format_line ( text , self .align .LEFT ) )
113
131
114
132
def left (self , text : str ):
115
- """Print a line of text left aligned.
133
+ """Add a line of text left aligned.
116
134
117
135
Same as `print` method.
118
136
"""
119
- self ._print_line ( text , self .align .LEFT )
137
+ self .lines . extend ( self . _format_line ( text , self .align .LEFT ) )
120
138
121
139
def centered (self , text : str ):
122
- """Print a line of text centered."""
123
- self ._print_line ( text , self .align .CENTER )
140
+ """Add a line of text centered."""
141
+ self .lines . extend ( self . _format_line ( text , self .align .CENTER ) )
124
142
125
143
def right (self , text : str ):
126
- """Print a line of text right aligned."""
127
- self ._print_line ( text , self .align .RIGHT )
144
+ """Add a line of text right aligned."""
145
+ self .lines . extend ( self . _format_line ( text , self .align .RIGHT ) )
0 commit comments