py-wait-until 0.0.4__tar.gz → 0.0.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: py-wait-until
3
- Version: 0.0.4
3
+ Version: 0.0.5
4
4
  Summary: Print a loading spinner while another command is running.
5
5
  Author: billwallis
6
6
  License-File: LICENSE
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "py-wait-until"
7
- version = "0.0.4"
7
+ version = "0.0.5"
8
8
  description = "Print a loading spinner while another command is running."
9
9
  readme = "README.md"
10
10
  license-files = ["LICENSE"]
@@ -5,7 +5,7 @@ build-backend = "uv_build"
5
5
 
6
6
  [project]
7
7
  name = "py-wait-until"
8
- version = "0.0.4"
8
+ version = "0.0.5"
9
9
  description = "Print a loading spinner while another command is running."
10
10
  authors = [{name = "billwallis"}]
11
11
  readme = "README.md"
@@ -1,4 +1,5 @@
1
1
  import argparse
2
+ import contextlib
2
3
  import enum
3
4
  import random
4
5
  import subprocess
@@ -61,19 +62,23 @@ def _spinner(chars: Sequence[str]) -> Generator[str]:
61
62
  yield chars[index % len(chars)]
62
63
 
63
64
 
65
+ @contextlib.contextmanager
66
+ def hide_cursor(is_tty: bool) -> Generator[None]:
67
+ if is_tty:
68
+ _write_line(sys.stdout, ANSI_HIDE_CURSOR)
69
+
70
+ yield
71
+
72
+ if is_tty:
73
+ _write_line(sys.stdout, ANSI_SHOW_CURSOR)
74
+
75
+
64
76
  def _wait_for_process(
65
77
  cmd: list[str],
66
78
  message: str,
67
79
  spinner: Spinner,
80
+ is_tty: bool,
68
81
  ) -> int:
69
- """
70
- Execute a command and display a waiting message until it completes.
71
- """
72
-
73
- # Only show spinner if stdout is a TTY (interactive terminal)
74
- if is_tty := sys.stdout.isatty():
75
- _write_line(sys.stdout, ANSI_HIDE_CURSOR)
76
-
77
82
  process = subprocess.Popen( # noqa: S603
78
83
  cmd,
79
84
  stdout=subprocess.PIPE,
@@ -92,7 +97,6 @@ def _wait_for_process(
92
97
 
93
98
  if is_tty:
94
99
  _write_line(sys.stdout, f"\r{ANSI_CLEAR_LINE}")
95
- _write_line(sys.stdout, ANSI_SHOW_CURSOR)
96
100
 
97
101
  stdout, stderr = process.communicate()
98
102
  if stdout:
@@ -103,6 +107,32 @@ def _wait_for_process(
103
107
  return process.returncode
104
108
 
105
109
 
110
+ def _wait_until(
111
+ cmd: list[str],
112
+ message: str,
113
+ spinner: Spinner,
114
+ ) -> int:
115
+ """
116
+ Execute a command and display a waiting message until it completes.
117
+ """
118
+
119
+ # TODO: If _not_ a TTY, then just run the process; else run `_wait_for_process`?
120
+ # Only show spinner if stdout is a TTY (interactive terminal)
121
+ is_tty = sys.stdout.isatty()
122
+ with hide_cursor(is_tty=is_tty):
123
+ try:
124
+ return _wait_for_process(
125
+ cmd=cmd,
126
+ message=message,
127
+ spinner=spinner,
128
+ is_tty=is_tty,
129
+ )
130
+ # We purposefully want to catch _everything_, including e.g. `KeyboardInterrupt`
131
+ except BaseException as err:
132
+ _write_line(sys.stderr, str(err))
133
+ return 1
134
+
135
+
106
136
  def main(argv: Sequence[str] | None = None) -> int:
107
137
  """
108
138
  Print a loading spinner while another process is running.
@@ -138,7 +168,7 @@ def main(argv: Sequence[str] | None = None) -> int:
138
168
  parser.print_help()
139
169
  return 1
140
170
 
141
- return _wait_for_process(
171
+ return _wait_until(
142
172
  cmd=args.command,
143
173
  message=args.message,
144
174
  spinner=args.spinner,
File without changes
File without changes