cheatsheet-cli 0.1.0__py3-none-any.whl

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.
cheatsheet/__init__.py ADDED
@@ -0,0 +1,137 @@
1
+ """
2
+ cheatsheet — open, list, and search Will's cheatsheets from the terminal.
3
+
4
+ Usage:
5
+ cheatsheet <topic> Open topic in browser + print URL
6
+ cheatsheet <topic> --url Print URL only
7
+ cheatsheet <topic> --term Render markdown through bat
8
+ cheatsheet list List all cheatsheets
9
+ cheatsheet search <q> Fuzzy-search titles
10
+ """
11
+
12
+ import json
13
+ import shutil
14
+ import subprocess
15
+ import sys
16
+ import urllib.request
17
+ import webbrowser
18
+ from pathlib import Path
19
+
20
+ BASE_URL = "https://cheatsheets.khanpikehome.com"
21
+ SHEETS_URL = f"{BASE_URL}/api/sheets.json"
22
+ RAW_URL = f"{BASE_URL}/raw"
23
+
24
+
25
+ def fetch_json(url: str):
26
+ with urllib.request.urlopen(url) as resp:
27
+ return json.loads(resp.read().decode())
28
+
29
+
30
+ def fetch_text(url: str):
31
+ with urllib.request.urlopen(url) as resp:
32
+ return resp.read().decode()
33
+
34
+
35
+ def get_sheets():
36
+ data = fetch_json(SHEETS_URL)
37
+ return data.get("rows", [])
38
+
39
+
40
+ def find_sheet(slug: str):
41
+ """Find a sheet by slug (exact match)."""
42
+ sheets = get_sheets()
43
+ for s in sheets:
44
+ if s["slug"] == slug:
45
+ return s
46
+ return None
47
+
48
+
49
+ def open_topic(slug: str, print_url: bool = False):
50
+ url = f"{BASE_URL}/{slug}"
51
+ if print_url:
52
+ print(url)
53
+ else:
54
+ print(url)
55
+ webbrowser.open(url)
56
+
57
+
58
+ def render_term(slug: str):
59
+ raw = fetch_text(f"{RAW_URL}/{slug}.md")
60
+ # Strip YAML frontmatter
61
+ if raw.startswith("---"):
62
+ end = raw.find("---", 3)
63
+ if end != -1:
64
+ raw = raw[end + 3 :].lstrip()
65
+ # Pipe through bat if available
66
+ bat = shutil.which("bat")
67
+ if bat:
68
+ proc = subprocess.Popen([bat, "--language", "markdown", "--style", "plain"], stdin=subprocess.PIPE)
69
+ proc.communicate(raw.encode())
70
+ else:
71
+ print(raw)
72
+
73
+
74
+ def list_sheets():
75
+ sheets = get_sheets()
76
+ for s in sorted(sheets, key=lambda x: x["slug"]):
77
+ title = s.get("title", "") or ""
78
+ print(f"{s['slug']:30s} {title}")
79
+
80
+
81
+ def search_sheets(query: str):
82
+ sheets = get_sheets()
83
+ q = query.lower()
84
+ results = []
85
+ for s in sheets:
86
+ if q in s["slug"].lower() or q in (s.get("title", "") or "").lower():
87
+ results.append(s)
88
+ if not results:
89
+ print(f"No matches for '{query}'")
90
+ return
91
+ for s in results:
92
+ title = s.get("title", "") or ""
93
+ print(f"{BASE_URL}/{s['slug']} ({title})")
94
+
95
+
96
+ def main():
97
+ if len(sys.argv) < 2:
98
+ print(__doc__.strip())
99
+ sys.exit(1)
100
+
101
+ cmd = sys.argv[1]
102
+
103
+ if cmd in ("-h", "--help"):
104
+ print(__doc__.strip())
105
+ return
106
+
107
+ if cmd == "list":
108
+ list_sheets()
109
+ return
110
+
111
+ if cmd == "search":
112
+ if len(sys.argv) < 3:
113
+ print("Usage: cheatsheet search <query>")
114
+ sys.exit(1)
115
+ search_sheets(" ".join(sys.argv[2:]))
116
+ return
117
+
118
+ # Topic commands
119
+ slug = cmd
120
+ sheet = find_sheet(slug)
121
+ if not sheet:
122
+ print(f"Unknown cheatsheet: {slug}", file=sys.stderr)
123
+ print("Run 'cheatsheet list' to see available topics.", file=sys.stderr)
124
+ sys.exit(1)
125
+
126
+ flags = set(sys.argv[2:])
127
+
128
+ if "--term" in flags:
129
+ render_term(slug)
130
+ elif "--url" in flags:
131
+ open_topic(slug, print_url=True)
132
+ else:
133
+ open_topic(slug)
134
+
135
+
136
+ if __name__ == "__main__":
137
+ main()
@@ -0,0 +1,5 @@
1
+ Metadata-Version: 2.4
2
+ Name: cheatsheet-cli
3
+ Version: 0.1.0
4
+ Summary: Open, list, and search Will's cheatsheets from the terminal
5
+ Requires-Python: >=3.10
@@ -0,0 +1,5 @@
1
+ cheatsheet/__init__.py,sha256=gLm2O2dmZ7i7R6DdD8dZDnswzVufhUu_c55bueWn9I0,3311
2
+ cheatsheet_cli-0.1.0.dist-info/METADATA,sha256=_mFAHa37HMSsMoz4afem2JiwTF5-eUrT9LDols9Iw18,151
3
+ cheatsheet_cli-0.1.0.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
4
+ cheatsheet_cli-0.1.0.dist-info/entry_points.txt,sha256=m8e1HB8HXSVocdH-ivDTUAhYeNTp-n_X9N210e8-TXg,47
5
+ cheatsheet_cli-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.31.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ cheatsheet = cheatsheet:main