codestrain 0.1.3__tar.gz → 0.1.4__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: codestrain
3
- Version: 0.1.3
3
+ Version: 0.1.4
4
4
  Summary: Your AI coding recovery score, from the terminal.
5
5
  Project-URL: Homepage, https://codestrain.dev
6
6
  Project-URL: Repository, https://github.com/codestrain/codestrain-cli
@@ -707,6 +707,49 @@ def print_project_breakdown(project_stats, anonymize=False):
707
707
  print()
708
708
 
709
709
 
710
+ # ── Share encoding ───────────────────────────────────────────────────────────
711
+ #
712
+ # `codestrain --share` produces a single self-contained URL that anyone can
713
+ # open to see the same anonymized report. No server, no upload, no cookies —
714
+ # the whole report is gzip+base64-encoded into the URL query string. Decoded
715
+ # client-side by codestrain.dev/s/index.html.
716
+
717
+ SHARE_BASE_URL = "https://codestrain.dev/s/"
718
+
719
+
720
+ def build_share_url(text: str, base: str = SHARE_BASE_URL) -> str:
721
+ """Encode `text` into a self-contained share URL.
722
+
723
+ Pipeline: utf-8 → gzip (level 9, deterministic) → base64-urlsafe (strip `=`
724
+ padding). The frontend reverses this with DecompressionStream('gzip').
725
+ Average codestrain --all output (~1.5 KB raw) compresses to ~600 B → ~800 B
726
+ base64 → final URL ~ 850 B. Well within all known browser URL limits
727
+ (Chrome / Safari / Firefox / curl all accept up to ~32 KB).
728
+ """
729
+ import base64
730
+ import gzip
731
+ payload = gzip.compress(text.encode("utf-8"), compresslevel=9, mtime=0)
732
+ encoded = base64.urlsafe_b64encode(payload).decode("ascii").rstrip("=")
733
+ return f"{base.rstrip('/')}/?d={encoded}"
734
+
735
+
736
+ def _copy_to_clipboard(text: str) -> bool:
737
+ """Best-effort clipboard copy. Returns True on success, False if no
738
+ suitable system command is available. Never raises.
739
+ """
740
+ import shutil
741
+ import subprocess
742
+ for cmd in (["pbcopy"], ["xclip", "-selection", "clipboard"], ["wl-copy"]):
743
+ if shutil.which(cmd[0]):
744
+ try:
745
+ p = subprocess.run(cmd, input=text.encode("utf-8"), timeout=2)
746
+ if p.returncode == 0:
747
+ return True
748
+ except (OSError, subprocess.TimeoutExpired):
749
+ continue
750
+ return False
751
+
752
+
710
753
  # ── Main ─────────────────────────────────────────────────────────────────────
711
754
 
712
755
  def main():
@@ -766,9 +809,21 @@ examples:
766
809
  help="Logo variant: auto (default, adapts to terminal width), "
767
810
  "big (5-line ASCII), small (one-line), or none (skip logo)",
768
811
  )
812
+ parser.add_argument(
813
+ "--share",
814
+ action="store_true",
815
+ help="Generate a shareable codestrain.dev URL with anonymized stats "
816
+ "(implies --anonymize --no-color; nothing is uploaded — all data "
817
+ "is encoded in the URL itself)",
818
+ )
769
819
 
770
820
  args = parser.parse_args()
771
821
 
822
+ # --share is a shortcut that anonymizes, strips color, then prints a URL.
823
+ if args.share:
824
+ args.anonymize = True
825
+ args.no_color = True
826
+
772
827
  if args.no_color:
773
828
  global _colors_on
774
829
  _colors_on = False
@@ -852,23 +907,43 @@ examples:
852
907
  project_stats[project_name] = []
853
908
  project_stats[project_name].append(stats)
854
909
 
855
- # Display results
856
- print_header_adaptive(args.logo)
857
-
858
- time_label = "Today" if not args.all else "All Time"
859
- if args.project and not args.anonymize:
860
- time_label += f" (project: {args.project})"
861
- elif args.project and args.anonymize:
862
- time_label += " (filtered)"
863
-
864
- print_divider(time_label)
865
- print()
866
- print_session_summary(all_stats)
867
-
868
- if len(project_stats) > 1 and not args.no_breakdown:
869
- print_project_breakdown(project_stats, anonymize=args.anonymize)
910
+ # Display results — capture stdout into a buffer if --share, so we can
911
+ # encode the rendered report into a URL after the run.
912
+ import contextlib
913
+ import io as _io
914
+
915
+ def _render_to(stream):
916
+ with contextlib.redirect_stdout(stream):
917
+ print_header_adaptive(args.logo)
918
+ time_label = "Today" if not args.all else "All Time"
919
+ if args.project and not args.anonymize:
920
+ time_label += f" (project: {args.project})"
921
+ elif args.project and args.anonymize:
922
+ time_label += " (filtered)"
923
+ print_divider(time_label)
924
+ print()
925
+ print_session_summary(all_stats)
926
+ if len(project_stats) > 1 and not args.no_breakdown:
927
+ print_project_breakdown(project_stats, anonymize=args.anonymize)
928
+ print()
870
929
 
871
- print()
930
+ if args.share:
931
+ buf = _io.StringIO()
932
+ _render_to(buf)
933
+ report_text = buf.getvalue()
934
+ # Print the report to stdout so the user sees what they're sharing
935
+ sys.stdout.write(report_text)
936
+ share_url = build_share_url(report_text)
937
+ print()
938
+ print(f" Shareable URL ({len(share_url)} chars):")
939
+ print(f" {share_url}")
940
+ print()
941
+ # Best-effort clipboard copy on macOS / Linux
942
+ if _copy_to_clipboard(share_url):
943
+ print(" (copied to clipboard)")
944
+ print()
945
+ else:
946
+ _render_to(sys.stdout)
872
947
 
873
948
 
874
949
  if __name__ == "__main__":
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "codestrain"
3
- version = "0.1.3"
3
+ version = "0.1.4"
4
4
  description = "Your AI coding recovery score, from the terminal."
5
5
  requires-python = ">=3.9"
6
6
  readme = "README.md"
@@ -107,6 +107,13 @@ out=$("$PY" "$CLI" --path "$FIXTURES" --all --project projectA --no-color 2>&1)
107
107
  # format_tokens compacts to "3.8K"-style — assert the K
108
108
  contains "$out" "3.8K" "expected ~3800 input tokens for projectA (compact = 3.8K)"
109
109
 
110
+ # 9. --share emits a codestrain.dev/s/ URL and implies --anonymize.
111
+ out=$("$PY" "$CLI" --path "$FIXTURES" --all --share 2>&1)
112
+ contains "$out" "https://codestrain.dev/s/?d=" "--share prints shareable URL"
113
+ contains "$out" "project-1" "--share implies --anonymize (project-1)"
114
+ not_contains "$out" "projectA" "--share scrubs real project names"
115
+ not_contains "$out" "projectB" "--share scrubs real project names"
116
+
110
117
  # ----------------------------------------------------------------------------
111
118
  # summary
112
119
  # ----------------------------------------------------------------------------
@@ -145,3 +145,54 @@ def test_format_cost_dollars(cli, cost, expected_prefix):
145
145
  def test_format_tokens_compact(cli, n, expected_substr):
146
146
  out = cli.format_tokens(n)
147
147
  assert expected_substr in out
148
+
149
+
150
+ # ─── Share encoding ─────────────────────────────────────────────────────────
151
+
152
+ def test_share_url_round_trip(cli):
153
+ """gzip + base64-urlsafe encoder must round-trip a real report string."""
154
+ import base64
155
+ import gzip
156
+ sample = (
157
+ "--- All Time ----\n"
158
+ " Sessions: 1454\n"
159
+ " Duration: 137h 21m\n"
160
+ " Cost: $21948.61\n"
161
+ )
162
+ url = cli.build_share_url(sample)
163
+ assert url.startswith("https://codestrain.dev/s/?d=")
164
+ encoded = url.split("?d=", 1)[1]
165
+ # Restore stripped padding + base64-urlsafe → bytes → gunzip → text.
166
+ pad = "=" * (-len(encoded) % 4)
167
+ raw = base64.urlsafe_b64decode(encoded + pad)
168
+ decoded = gzip.decompress(raw).decode("utf-8")
169
+ assert decoded == sample
170
+
171
+
172
+ def test_share_url_unicode_safe(cli):
173
+ """Non-ASCII content (Cyrillic, emoji, currency) must survive the trip."""
174
+ import base64
175
+ import gzip
176
+ sample = "Сессий: 1454 · стоимость €18 950 / $21 948 · 🟢 recovery 82%"
177
+ url = cli.build_share_url(sample)
178
+ encoded = url.split("?d=", 1)[1]
179
+ pad = "=" * (-len(encoded) % 4)
180
+ raw = base64.urlsafe_b64decode(encoded + pad)
181
+ assert gzip.decompress(raw).decode("utf-8") == sample
182
+
183
+
184
+ def test_share_url_compact_for_typical_report(cli):
185
+ """A realistic 1.5 KB report should encode to under ~1 KB URL."""
186
+ # 30 lines × 50 chars = 1500 bytes (close to real --all output).
187
+ sample = "\n".join(f" line {i:02d}: project-{i} 12h 34m $123.45" for i in range(30))
188
+ url = cli.build_share_url(sample)
189
+ # Comfortably under 32 KB (the conservative browser URL limit).
190
+ assert len(url) < 2_000
191
+ # Should compress reasonably — the line template is highly repetitive.
192
+ assert len(url) < len(sample)
193
+
194
+
195
+ def test_share_url_deterministic(cli):
196
+ """Same input → same URL across calls (mtime=0 in gzip header)."""
197
+ text = "hello world\n"
198
+ assert cli.build_share_url(text) == cli.build_share_url(text)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes