mirror of
https://github.com/daylinmorgan/viv.git
synced 2024-12-22 18:50:43 -06:00
fix: use stderr where appropriate to allow piping
This commit is contained in:
parent
cb27f9a8d4
commit
d367571957
2 changed files with 16 additions and 16 deletions
|
@ -67,22 +67,22 @@ class Spinner:
|
||||||
def write_next(self):
|
def write_next(self):
|
||||||
with self._screen_lock:
|
with self._screen_lock:
|
||||||
if not self.spinner_visible:
|
if not self.spinner_visible:
|
||||||
sys.stdout.write(next(self.spinner))
|
sys.stderr.write(next(self.spinner))
|
||||||
self.spinner_visible = True
|
self.spinner_visible = True
|
||||||
sys.stdout.flush()
|
sys.stderr.flush()
|
||||||
|
|
||||||
def remove_spinner(self, cleanup=False):
|
def remove_spinner(self, cleanup=False):
|
||||||
with self._screen_lock:
|
with self._screen_lock:
|
||||||
if self.spinner_visible:
|
if self.spinner_visible:
|
||||||
sys.stdout.write("\b\b\b")
|
sys.stderr.write("\b\b\b")
|
||||||
# sys.stdout.write("\b")
|
# sys.stdout.write("\b")
|
||||||
self.spinner_visible = False
|
self.spinner_visible = False
|
||||||
if cleanup:
|
if cleanup:
|
||||||
sys.stdout.write(" ") # overwrite spinner with blank
|
sys.stderr.write(" ") # overwrite spinner with blank
|
||||||
# sys.stdout.write("\r") # move to next line
|
# sys.stdout.write("\r") # move to next line
|
||||||
# move back then delete the line
|
# move back then delete the line
|
||||||
sys.stdout.write("\r\033[K")
|
sys.stderr.write("\r\033[K")
|
||||||
sys.stdout.flush()
|
sys.stderr.flush()
|
||||||
|
|
||||||
def spinner_task(self):
|
def spinner_task(self):
|
||||||
while self.busy:
|
while self.busy:
|
||||||
|
@ -91,18 +91,18 @@ class Spinner:
|
||||||
self.remove_spinner()
|
self.remove_spinner()
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
if sys.stdout.isatty():
|
if sys.stderr.isatty():
|
||||||
self._screen_lock = threading.Lock()
|
self._screen_lock = threading.Lock()
|
||||||
self.busy = True
|
self.busy = True
|
||||||
self.thread = threading.Thread(target=self.spinner_task)
|
self.thread = threading.Thread(target=self.spinner_task)
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_traceback): # noqa
|
def __exit__(self, exc_type, exc_val, exc_traceback): # noqa
|
||||||
if sys.stdout.isatty():
|
if sys.stderr.isatty():
|
||||||
self.busy = False
|
self.busy = False
|
||||||
self.remove_spinner(cleanup=True)
|
self.remove_spinner(cleanup=True)
|
||||||
else:
|
else:
|
||||||
sys.stdout.write("\r")
|
sys.stderr.write("\r")
|
||||||
|
|
||||||
|
|
||||||
BOX: Dict[str, str] = {
|
BOX: Dict[str, str] = {
|
||||||
|
@ -136,7 +136,7 @@ class Ansi:
|
||||||
metavar: str = "\033[33m" # normal yellow
|
metavar: str = "\033[33m" # normal yellow
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
if os.getenv("NO_COLOR") or not sys.stdout.isatty():
|
if os.getenv("NO_COLOR") or not sys.stderr.isatty():
|
||||||
for attr in self.__dict__:
|
for attr in self.__dict__:
|
||||||
setattr(self, attr, "")
|
setattr(self, attr, "")
|
||||||
|
|
||||||
|
@ -248,9 +248,9 @@ class Ansi:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
sys.stdout.write(f" {BOX['tl']}{BOX['h']*(sum(sizes)+5)}{BOX['tr']}\n")
|
sys.stderr.write(f" {BOX['tl']}{BOX['h']*(sum(sizes)+5)}{BOX['tr']}\n")
|
||||||
sys.stdout.write("\n".join(table_rows) + "\n")
|
sys.stderr.write("\n".join(table_rows) + "\n")
|
||||||
sys.stdout.write(f" {BOX['bl']}{BOX['h']*(sum(sizes)+5)}{BOX['br']}\n")
|
sys.stderr.write(f" {BOX['bl']}{BOX['h']*(sum(sizes)+5)}{BOX['br']}\n")
|
||||||
|
|
||||||
|
|
||||||
a = Ansi()
|
a = Ansi()
|
||||||
|
@ -268,12 +268,12 @@ def warn(msg):
|
||||||
echo(f"{a.yellow}warn:{a.end} {msg}", style="yellow")
|
echo(f"{a.yellow}warn:{a.end} {msg}", style="yellow")
|
||||||
|
|
||||||
|
|
||||||
def echo(msg: str, style="magenta", newline=True) -> None:
|
def echo(msg: str, style="magenta", newline=True, fd=sys.stderr) -> None:
|
||||||
"""output general message to stdout"""
|
"""output general message to stdout"""
|
||||||
output = f"{a.cyan}Viv{a.end}{a.__dict__[style]}::{a.end} {msg}"
|
output = f"{a.cyan}Viv{a.end}{a.__dict__[style]}::{a.end} {msg}"
|
||||||
if newline:
|
if newline:
|
||||||
output += "\n"
|
output += "\n"
|
||||||
sys.stdout.write(output)
|
fd.write(output)
|
||||||
|
|
||||||
|
|
||||||
def run(
|
def run(
|
||||||
|
|
2
todo.md
2
todo.md
|
@ -1,7 +1,7 @@
|
||||||
# VIV Todo's
|
# VIV Todo's
|
||||||
|
|
||||||
- [x] swap flake8 for ruff
|
- [x] swap flake8 for ruff
|
||||||
- [ ] use stdout and stderr more effectively (or switch to logging?)
|
- [x] use stdout and stderr more effectively (or switch to logging?)
|
||||||
- [ ] use config file (probably ini or toml for python>=3.11)
|
- [ ] use config file (probably ini or toml for python>=3.11)
|
||||||
- [ ] enable a garbage collection based on time or file existence (configurable)
|
- [ ] enable a garbage collection based on time or file existence (configurable)
|
||||||
- [ ] unit tests (v important)
|
- [ ] unit tests (v important)
|
||||||
|
|
Loading…
Reference in a new issue