Skip to content

Commit fbb6dba

Browse files
Added a new method to log21.Logger class: log21.Logger.clear_line.
1 parent 846022c commit fbb6dba

6 files changed

Lines changed: 41 additions & 5 deletions

File tree

CHANGES-LOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ Help this project by [Donation](DONATE.md)
66
Changes log
77
-----------
88

9+
### 2.3.4
10+
11+
Added a new method to `log21.Logger` class: `log21.Logger.clear_line`. This method clears the current line in the
12+
console and moves the cursor to the beginning of the line.
13+
914
### 2.3.3
1015

1116
Fixed a bug that would cause an error creating a progress bar with no value set for width in systems without support for

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ python setup.py install
5353
Changes
5454
-------
5555

56-
### 2.3.3
56+
### 2.3.4
5757

58-
Fixed a bug that would cause an error creating a progress bar with no value set for width in systems without support for
59-
os.get_terminal_size().
58+
Added a new method to `log21.Logger` class: `log21.Logger.clear_line`. This method clears the current line in the
59+
console and moves the cursor to the beginning of the line.
6060

6161
[Full Changes Log](https://github.com/MPCodeWriter21/log21/blob/master/CHANGES-LOG.md)
6262

log21/Logger.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,14 @@ def progress_bar(self):
191191
@progress_bar.setter
192192
def progress_bar(self, value: '_log21.ProgressBar'):
193193
self._progress_bar = value
194+
195+
def clear_line(self, length: int = None):
196+
"""
197+
Clear the current line.
198+
199+
:param length: The length of the line to clear.
200+
:return:
201+
"""
202+
for handler in self.handlers:
203+
if hasattr(handler, 'clear_line'):
204+
handler.clear_line(length)

log21/StreamHandler.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ def emit(self, record):
5858
self.check_nl(record)
5959
super().emit(record)
6060

61+
def clear_line(self, length: int = None):
62+
"""
63+
Clear the current line.
64+
65+
:param length: The length of the line to clear.
66+
:return:
67+
"""
68+
if IS_WINDOWS:
69+
file_descriptor = getattr(self.stream, 'fileno', None)
70+
if file_descriptor:
71+
file_descriptor = file_descriptor()
72+
if file_descriptor in (1, 2):
73+
if length is None:
74+
length = _os.get_terminal_size(file_descriptor)[0]
75+
self.stream.write('\r' + (' ' * (length - 1)) + '\r')
76+
else:
77+
if length is None:
78+
length = _os.get_terminal_size()[0]
79+
self.stream.write('\r' + (' ' * (length - 1)) + '\r')
80+
6181

6282
# A stream handler that supports colorizing.
6383
class ColorizingStreamHandler(StreamHandler):

log21/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from log21.Formatters import ColorizingFormatter, DecolorizingFormatter
2222
from log21.Colors import Colors, get_color, get_colors, ansi_escape, get_color_name, closest_color
2323

24-
__version__ = "2.3.3"
24+
__version__ = "2.3.4"
2525
__author__ = "CodeWriter21 (Mehrad Pooryoussof)"
2626
__github__ = "Https://GitHub.com/MPCodeWriter21/log21"
2727
__all__ = ['ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter', 'DecolorizingFormatter',

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
long_description = file.read()
88

99
DESCRIPTION = 'A simple logging package that helps you log colorized messages in Windows console.'
10-
VERSION = '2.3.3'
10+
VERSION = '2.3.4'
1111

1212
setup(
1313
name='log21',

0 commit comments

Comments
 (0)