fzf into download
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import db
|
||||
@@ -59,48 +61,54 @@ def cmd_add(args: argparse.Namespace) -> None:
|
||||
|
||||
|
||||
def cmd_disable(args: argparse.Namespace) -> None:
|
||||
with db.connect() as conn:
|
||||
ok = db.set_enabled(conn, args.user, args.url, enabled=False)
|
||||
if ok:
|
||||
conn.commit()
|
||||
print("ok" if ok else "not found")
|
||||
_apply_to_links(
|
||||
args,
|
||||
lambda conn, user, url: db.set_enabled(conn, user, url, enabled=False),
|
||||
selector_filter="disable",
|
||||
)
|
||||
|
||||
|
||||
def cmd_enable(args: argparse.Namespace) -> None:
|
||||
with db.connect() as conn:
|
||||
ok = db.set_enabled(conn, args.user, args.url, enabled=True)
|
||||
if ok:
|
||||
conn.commit()
|
||||
print("ok" if ok else "not found")
|
||||
_apply_to_links(
|
||||
args,
|
||||
lambda conn, user, url: db.set_enabled(conn, user, url, enabled=True),
|
||||
selector_filter="enable",
|
||||
)
|
||||
|
||||
|
||||
def cmd_ban(args: argparse.Namespace) -> None:
|
||||
with db.connect() as conn:
|
||||
ok = db.set_banned(conn, args.user, args.url, banned=True, reason=args.reason)
|
||||
if ok:
|
||||
conn.commit()
|
||||
print("ok" if ok else "not found")
|
||||
_apply_to_links(
|
||||
args,
|
||||
lambda conn, user, url: db.set_banned(
|
||||
conn, user, url, banned=True, reason=args.reason
|
||||
),
|
||||
selector_filter="ban",
|
||||
)
|
||||
|
||||
|
||||
def cmd_unban(args: argparse.Namespace) -> None:
|
||||
with db.connect() as conn:
|
||||
ok = db.set_banned(conn, args.user, args.url, banned=False)
|
||||
if ok:
|
||||
conn.commit()
|
||||
print("ok" if ok else "not found")
|
||||
_apply_to_links(
|
||||
args,
|
||||
lambda conn, user, url: db.set_banned(conn, user, url, banned=False),
|
||||
selector_filter="unban",
|
||||
)
|
||||
|
||||
|
||||
def cmd_remove(args: argparse.Namespace) -> None:
|
||||
with db.connect() as conn:
|
||||
ok = db.remove_link(conn, args.user, args.url)
|
||||
if ok:
|
||||
conn.commit()
|
||||
print("ok" if ok else "not found")
|
||||
_apply_to_links(args, lambda conn, user, url: db.remove_link(conn, user, url), "any")
|
||||
|
||||
|
||||
def cmd_rename(args: argparse.Namespace) -> None:
|
||||
old_url = args.old_url
|
||||
if not old_url:
|
||||
selection = _select_links(args.user, multi=False, selector_filter="any")
|
||||
if not selection:
|
||||
print("not found")
|
||||
return
|
||||
old_url = selection[0]
|
||||
new_url = args.new_url or input("New URL: ").strip()
|
||||
with db.connect() as conn:
|
||||
result = db.rename_link(conn, args.user, args.old_url, args.new_url)
|
||||
result = db.rename_link(conn, args.user, old_url, new_url)
|
||||
if result["status"] == "renamed":
|
||||
conn.commit()
|
||||
print(result["status"])
|
||||
@@ -191,3 +199,68 @@ def cmd_validate_import(_: argparse.Namespace) -> None:
|
||||
[missing_enabled, missing_disabled, extra_enabled, extra_disabled]
|
||||
):
|
||||
print(" OK")
|
||||
|
||||
|
||||
def _fzf_select(lines: list[str], multi: bool) -> list[str]:
|
||||
if not lines:
|
||||
return []
|
||||
if shutil.which("fzf") is None:
|
||||
print("fzf not found.")
|
||||
return []
|
||||
args = ["fzf"]
|
||||
if multi:
|
||||
args.append("--multi")
|
||||
proc = subprocess.run(
|
||||
args,
|
||||
input="\n".join(lines),
|
||||
text=True,
|
||||
capture_output=True,
|
||||
check=False,
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
return []
|
||||
return [ln for ln in proc.stdout.splitlines() if ln.strip()]
|
||||
|
||||
|
||||
def _select_links(user: str, multi: bool, selector_filter: str) -> list[str]:
|
||||
with db.connect() as conn:
|
||||
rows = db.get_links(conn, users=[user], include_disabled=True, include_banned=True)
|
||||
links = []
|
||||
for row in rows:
|
||||
enabled = bool(row["enabled"])
|
||||
banned = bool(row["banned_at"])
|
||||
if selector_filter == "enable" and enabled:
|
||||
continue
|
||||
if selector_filter == "disable" and not enabled:
|
||||
continue
|
||||
if selector_filter == "ban" and banned:
|
||||
continue
|
||||
if selector_filter == "unban" and not banned:
|
||||
continue
|
||||
links.append(row["url_original"])
|
||||
return _fzf_select(links, multi=multi)
|
||||
|
||||
|
||||
def _apply_to_links(args: argparse.Namespace, fn, selector_filter: str) -> None:
|
||||
if args.url:
|
||||
with db.connect() as conn:
|
||||
ok = fn(conn, args.user, args.url)
|
||||
if ok:
|
||||
conn.commit()
|
||||
print("ok" if ok else "not found")
|
||||
return
|
||||
|
||||
selections = _select_links(args.user, multi=True, selector_filter=selector_filter)
|
||||
if not selections:
|
||||
print("not found")
|
||||
return
|
||||
|
||||
with db.connect() as conn:
|
||||
changed = 0
|
||||
for url in selections:
|
||||
ok = fn(conn, args.user, url)
|
||||
if ok:
|
||||
changed += 1
|
||||
if changed:
|
||||
conn.commit()
|
||||
print(f"ok ({changed})")
|
||||
|
||||
Reference in New Issue
Block a user