mirror of
https://github.com/verigak/progress.git
synced 2025-12-08 19:33:24 +00:00
Add flag to override tty check
This commit is contained in:
@@ -20,6 +20,7 @@ SHOW_CURSOR = '\x1b[?25h'
|
|||||||
|
|
||||||
|
|
||||||
class WriteMixin(object):
|
class WriteMixin(object):
|
||||||
|
check_tty = True
|
||||||
hide_cursor = False
|
hide_cursor = False
|
||||||
|
|
||||||
def __init__(self, message=None, **kwargs):
|
def __init__(self, message=None, **kwargs):
|
||||||
@@ -28,14 +29,14 @@ class WriteMixin(object):
|
|||||||
if message:
|
if message:
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
if self.file and self.file.isatty():
|
if self.file and self.is_tty():
|
||||||
if self.hide_cursor:
|
if self.hide_cursor:
|
||||||
print(HIDE_CURSOR, end='', file=self.file)
|
print(HIDE_CURSOR, end='', file=self.file)
|
||||||
print(self.message, end='', file=self.file)
|
print(self.message, end='', file=self.file)
|
||||||
self.file.flush()
|
self.file.flush()
|
||||||
|
|
||||||
def write(self, s):
|
def write(self, s):
|
||||||
if self.file and self.file.isatty():
|
if self.file and self.is_tty():
|
||||||
b = '\b' * self._width
|
b = '\b' * self._width
|
||||||
c = s.ljust(self._width)
|
c = s.ljust(self._width)
|
||||||
print(b + c, end='', file=self.file)
|
print(b + c, end='', file=self.file)
|
||||||
@@ -43,11 +44,15 @@ class WriteMixin(object):
|
|||||||
self.file.flush()
|
self.file.flush()
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
if self.file and self.file.isatty() and self.hide_cursor:
|
if self.file and self.is_tty() and self.hide_cursor:
|
||||||
print(SHOW_CURSOR, end='', file=self.file)
|
print(SHOW_CURSOR, end='', file=self.file)
|
||||||
|
|
||||||
|
def is_tty(self):
|
||||||
|
return self.file.isatty() if self.check_tty else True
|
||||||
|
|
||||||
|
|
||||||
class WritelnMixin(object):
|
class WritelnMixin(object):
|
||||||
|
check_tty = True
|
||||||
hide_cursor = False
|
hide_cursor = False
|
||||||
|
|
||||||
def __init__(self, message=None, **kwargs):
|
def __init__(self, message=None, **kwargs):
|
||||||
@@ -55,25 +60,28 @@ class WritelnMixin(object):
|
|||||||
if message:
|
if message:
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
if self.file and self.file.isatty() and self.hide_cursor:
|
if self.file and self.is_tty() and self.hide_cursor:
|
||||||
print(HIDE_CURSOR, end='', file=self.file)
|
print(HIDE_CURSOR, end='', file=self.file)
|
||||||
|
|
||||||
def clearln(self):
|
def clearln(self):
|
||||||
if self.file and self.file.isatty():
|
if self.file and self.is_tty():
|
||||||
print('\r\x1b[K', end='', file=self.file)
|
print('\r\x1b[K', end='', file=self.file)
|
||||||
|
|
||||||
def writeln(self, line):
|
def writeln(self, line):
|
||||||
if self.file and self.file.isatty():
|
if self.file and self.is_tty():
|
||||||
self.clearln()
|
self.clearln()
|
||||||
print(line, end='', file=self.file)
|
print(line, end='', file=self.file)
|
||||||
self.file.flush()
|
self.file.flush()
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
if self.file and self.file.isatty():
|
if self.file and self.is_tty():
|
||||||
print(file=self.file)
|
print(file=self.file)
|
||||||
if self.hide_cursor:
|
if self.hide_cursor:
|
||||||
print(SHOW_CURSOR, end='', file=self.file)
|
print(SHOW_CURSOR, end='', file=self.file)
|
||||||
|
|
||||||
|
def is_tty(self):
|
||||||
|
return self.file.isatty() if self.check_tty else True
|
||||||
|
|
||||||
|
|
||||||
from signal import signal, SIGINT
|
from signal import signal, SIGINT
|
||||||
from sys import exit
|
from sys import exit
|
||||||
|
|||||||
Reference in New Issue
Block a user