mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
jobs: default representation changed to dict (#5363)
* update * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: a <1@1.1> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
874ad9ffe6
commit
8ab1b9a0ee
2 changed files with 44 additions and 10 deletions
23
news/jobs_repr.rst
Normal file
23
news/jobs_repr.rst
Normal file
|
@ -0,0 +1,23 @@
|
|||
**Added:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Changed:**
|
||||
|
||||
* jobs: default representation changed to dict.
|
||||
|
||||
**Deprecated:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Removed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Fixed:**
|
||||
|
||||
* <news item>
|
||||
|
||||
**Security:**
|
||||
|
||||
* <news item>
|
|
@ -335,23 +335,33 @@ def _clear_dead_jobs():
|
|||
del get_jobs()[job]
|
||||
|
||||
|
||||
def format_job_string(num: int) -> str:
|
||||
def format_job_string(num: int, format="dict") -> str:
|
||||
try:
|
||||
job = get_jobs()[num]
|
||||
except KeyError:
|
||||
return ""
|
||||
tasks = get_tasks()
|
||||
pos = "+" if tasks[0] == num else "-" if tasks[1] == num else " "
|
||||
status = job["status"]
|
||||
cmd = " ".join([" ".join(i) if isinstance(i, list) else i for i in job["cmds"]])
|
||||
pid = f"({job['pids'][-1]})" if job["pids"] else ""
|
||||
bg = " &" if job["bg"] else ""
|
||||
return f"[{num}]{pos} {status}: {cmd}{bg} {pid}"
|
||||
r = {
|
||||
"num": num,
|
||||
"status": job["status"],
|
||||
"cmd": " ".join(
|
||||
[" ".join(i) if isinstance(i, list) else i for i in job["cmds"]]
|
||||
),
|
||||
"pid": int(job["pids"][-1]) if job["pids"] else None,
|
||||
}
|
||||
|
||||
if format == "posix":
|
||||
r["pos"] = "+" if tasks[0] == num else "-" if tasks[1] == num else " "
|
||||
r["bg"] = " &" if job["bg"] else ""
|
||||
r["pid"] = f"({r['pid']})" if r["pid"] else ""
|
||||
return "[{num}]{pos} {status}: {cmd}{bg} {pid}".format(**r)
|
||||
else:
|
||||
return repr(r)
|
||||
|
||||
|
||||
def print_one_job(num, outfile=sys.stdout):
|
||||
def print_one_job(num, outfile=sys.stdout, format="dict"):
|
||||
"""Print a line describing job number ``num``."""
|
||||
info = format_job_string(num)
|
||||
info = format_job_string(num, format)
|
||||
if info:
|
||||
print(info, file=outfile)
|
||||
|
||||
|
@ -445,8 +455,9 @@ def jobs(args, stdin=None, stdout=sys.stdout, stderr=None):
|
|||
Display a list of all current jobs.
|
||||
"""
|
||||
_clear_dead_jobs()
|
||||
format = "posix" if "--posix" in args else "dict"
|
||||
for j in get_tasks():
|
||||
print_one_job(j, outfile=stdout)
|
||||
print_one_job(j, outfile=stdout, format=format)
|
||||
return None, None
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue