1macro_rules! qprintln {
2 ($($t:tt)*) => {
3 if !super::quiet_enabled() {
4 println!($($t)*);
5 }
6 };
7}
8
9pub fn print_hook_stdout(shell: &str) {
10 let binary = crate::core::portable_binary::resolve_portable_binary();
11 let binary = hook_binary_for_shell(shell, &binary);
12
13 let code = match shell {
14 "bash" | "zsh" => generate_hook_posix(&binary),
15 "fish" => generate_hook_fish(&binary),
16 "powershell" | "pwsh" => generate_hook_powershell(&binary),
17 _ => {
18 tracing::error!("lean-ctx: unsupported shell '{shell}'");
19 eprintln!("Supported: bash, zsh, fish, powershell");
20 std::process::exit(1);
21 }
22 };
23 print!("{code}");
24}
25
26fn hook_binary_for_shell(shell: &str, binary: &str) -> String {
34 match shell {
35 "powershell" | "pwsh" => binary.to_string(),
36 _ => crate::hooks::to_bash_compatible_path(binary),
37 }
38}
39
40fn backup_shell_config(path: &std::path::Path) {
41 if !path.exists() {
42 return;
43 }
44 let bak = path.with_extension("lean-ctx.bak");
45 if std::fs::copy(path, &bak).is_ok() {
46 qprintln!(
47 " Backup: {}",
48 bak.file_name().map_or_else(
49 || bak.display().to_string(),
50 |n| format!("~/{}", n.to_string_lossy())
51 )
52 );
53 }
54}
55
56fn config_artifact_dir() -> Option<std::path::PathBuf> {
61 crate::core::paths::config_dir().ok()
62}
63
64fn write_hook_file(filename: &str, content: &str) -> Option<std::path::PathBuf> {
65 let dir = config_artifact_dir()?;
66 let _ = std::fs::create_dir_all(&dir);
67 let path = dir.join(filename);
68 match std::fs::write(&path, content) {
69 Ok(()) => {
70 #[cfg(unix)]
71 {
72 use std::os::unix::fs::PermissionsExt;
73 let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644));
74 }
75 Some(path)
76 }
77 Err(e) => {
78 tracing::error!("Error writing {}: {e}", path.display());
79 None
80 }
81 }
82}
83
84fn resolved_hook_dir_display() -> String {
85 config_artifact_dir().map_or_else(
86 || "$HOME/.config/lean-ctx".to_string(),
87 |p| p.to_string_lossy().to_string(),
88 )
89}
90
91fn source_line_posix(shell_ext: &str) -> String {
92 let mut dir = resolved_hook_dir_display();
93 if cfg!(windows) {
95 dir = crate::hooks::to_bash_compatible_path(&dir);
96 }
97 format!(
98 "# lean-ctx shell hook — begin\n\
99 if [ -f \"{dir}/shell-hook.{shell_ext}\" ]; then\n\
100 . \"{dir}/shell-hook.{shell_ext}\"\n\
101 fi\n\
102 # lean-ctx shell hook — end\n"
103 )
104}
105
106fn source_line_fish() -> String {
107 let mut dir = resolved_hook_dir_display();
108 if cfg!(windows) {
110 dir = crate::hooks::to_bash_compatible_path(&dir);
111 }
112 format!(
113 "# lean-ctx shell hook — begin\n\
114 if test -f \"{dir}/shell-hook.fish\"\n\
115 source \"{dir}/shell-hook.fish\"\n\
116 end\n\
117 # lean-ctx shell hook — end\n"
118 )
119}
120
121fn source_line_powershell() -> String {
122 let dir = resolved_hook_dir_display();
123 let dir_ps = dir.replace('/', "\\");
124 format!(
125 "# lean-ctx shell hook — begin\n\
126 $leanCtxHook = \"{dir_ps}\\shell-hook.ps1\"\n\
127 if ((Test-Path $leanCtxHook) -and -not [Console]::IsOutputRedirected) {{ . $leanCtxHook }}\n"
128 )
129}
130
131fn upsert_source_line(rc_path: &std::path::Path, source_line: &str) {
132 backup_shell_config(rc_path);
133
134 if let Ok(existing) = std::fs::read_to_string(rc_path) {
135 if existing.contains(source_line.trim()) {
136 return;
137 }
138
139 let cleaned = remove_lean_ctx_block(&existing);
141 let cleaned = cleaned
142 .lines()
143 .filter(|line| {
144 !line.contains("lean-ctx/shell-hook.")
145 && !line.contains("lean-ctx\\shell-hook.")
146 && line.trim() != "lean-ctx shell hook"
147 })
148 .collect::<Vec<_>>()
149 .join("\n");
150 let cleaned = if cleaned.ends_with('\n') {
151 cleaned
152 } else {
153 format!("{cleaned}\n")
154 };
155
156 match std::fs::write(rc_path, format!("{cleaned}{source_line}")) {
157 Ok(()) => {
158 qprintln!("Updated lean-ctx hook in {}", rc_path.display());
159 }
160 Err(e) => {
161 tracing::error!("Error updating {}: {e}", rc_path.display());
162 print_shell_write_error(rc_path, source_line, &e);
163 }
164 }
165 return;
166 }
167
168 match std::fs::OpenOptions::new()
169 .append(true)
170 .create(true)
171 .open(rc_path)
172 {
173 Ok(mut f) => {
174 use std::io::Write;
175 let _ = f.write_all(source_line.as_bytes());
176 qprintln!("Added lean-ctx hook to {}", rc_path.display());
177 }
178 Err(e) => {
179 tracing::error!("Error writing {}: {e}", rc_path.display());
180 print_shell_write_error(rc_path, source_line, &e);
181 }
182 }
183}
184
185fn print_shell_write_error(rc_path: &std::path::Path, source_line: &str, err: &std::io::Error) {
186 eprintln!();
187 eprintln!(" \x1B[33m⚠ Cannot write to {}\x1B[0m", rc_path.display());
188 eprintln!(" Error: {err}");
189 if err.kind() == std::io::ErrorKind::PermissionDenied {
190 eprintln!();
191 eprintln!(" Your shell config is read-only (nix-darwin, Home Manager, or similar).");
192 eprintln!(" Add the following to a writable shell config file manually:");
193 } else {
194 eprintln!();
195 eprintln!(" Add the following to your shell config manually:");
196 }
197 eprintln!();
198 for line in source_line.lines() {
199 eprintln!(" {line}");
200 }
201 eprintln!();
202 eprintln!(" Or source it from a writable file (e.g. ~/.zshrc.local):");
203 eprintln!(" echo 'source ~/.zshrc.local' # (add to nix config)");
204 eprintln!(" Then add the hook lines to ~/.zshrc.local");
205 eprintln!();
206}
207
208pub fn generate_hook_powershell(binary: &str) -> String {
209 let config = crate::core::config::Config::load();
210 let activation = config.shell_activation_effective();
211 let baked_default = match activation {
212 crate::core::config::ShellActivation::Always => "always",
213 crate::core::config::ShellActivation::AgentsOnly => "agents-only",
214 crate::core::config::ShellActivation::Off => "off",
215 };
216 let binary_escaped = binary.replace('\\', "\\\\");
217 format!(
218 r#"# lean-ctx shell hook — transparent CLI compression (95+ patterns)
219$_leanCtxActivation = if ($env:LEAN_CTX_SHELL_ACTIVATION) {{ $env:LEAN_CTX_SHELL_ACTIVATION }} else {{ "{baked_default}" }}
220$_leanCtxShouldActivate = $false
221if (-not $env:LEAN_CTX_ACTIVE -and -not $env:LEAN_CTX_DISABLED -and -not $env:LEAN_CTX_NO_HOOK) {{
222 switch ($_leanCtxActivation) {{
223 {{ $_ -in 'off','none','manual' }} {{ $_leanCtxShouldActivate = $false }}
224 {{ $_ -in 'agents-only','agents_only','agentsonly' }} {{
225 $_leanCtxShouldActivate = $env:LEAN_CTX_AGENT -or $env:CLAUDECODE -or $env:CODEBUDDY -or $env:CODEX_CLI_SESSION -or $env:GEMINI_SESSION
226 }}
227 default {{ $_leanCtxShouldActivate = $true }}
228 }}
229}}
230if ($_leanCtxShouldActivate) {{
231 $LeanCtxBin = "{binary_escaped}"
232 function _lc {{
233 $nativeCmd = Get-Command $args[0] -CommandType Application -ErrorAction SilentlyContinue
234 if ($env:LEAN_CTX_DISABLED -or $env:LEAN_CTX_NO_HOOK -or [Console]::IsOutputRedirected) {{
235 if ($nativeCmd) {{ & $nativeCmd.Source $args[1..$args.Length] }} else {{ Write-Error "Command not found: $($args[0])" }}
236 return
237 }}
238 & $LeanCtxBin -c @args
239 if ($LASTEXITCODE -eq 127 -or $LASTEXITCODE -eq 126) {{
240 if ($nativeCmd) {{ & $nativeCmd.Source $args[1..$args.Length] }} else {{ Write-Error "Command not found: $($args[0])" }}
241 }}
242 }}
243 function lean-ctx-raw {{ $env:LEAN_CTX_RAW = '1'; & @args; Remove-Item Env:LEAN_CTX_RAW -ErrorAction SilentlyContinue }}
244 if (Get-Command lean-ctx -ErrorAction SilentlyContinue) {{
245 function git {{ _lc git @args }}
246 function cargo {{ _lc cargo @args }}
247 function docker {{ _lc docker @args }}
248 function kubectl {{ _lc kubectl @args }}
249 function gh {{ _lc gh @args }}
250 function pip {{ _lc pip @args }}
251 function pip3 {{ _lc pip3 @args }}
252 function ruff {{ _lc ruff @args }}
253 function go {{ _lc go @args }}
254 function curl {{ _lc curl @args }}
255 function wget {{ _lc wget @args }}
256 foreach ($c in @('npm','pnpm','yarn','eslint','prettier','tsc')) {{
257 if (Get-Command $c -CommandType Application -ErrorAction SilentlyContinue) {{
258 $body = "_lc $c `@args"
259 New-Item -Path "function:$c" -Value ([scriptblock]::Create($body)) -Force | Out-Null
260 }}
261 }}
262 }}
263}}
264"#
265 )
266}
267
268pub fn init_powershell(binary: &str) {
269 let profile_path = if let Some(home) = dirs::home_dir() {
273 let path = crate::shell::platform::resolve_powershell_profile_path(&home);
274 if let Some(dir) = path.parent() {
275 let _ = std::fs::create_dir_all(dir);
276 }
277 path
278 } else {
279 tracing::error!("Could not resolve PowerShell profile directory");
280 return;
281 };
282
283 let hook_content = generate_hook_powershell(binary);
284
285 if write_hook_file("shell-hook.ps1", &hook_content).is_some() {
286 upsert_source_line(&profile_path, &source_line_powershell());
287 qprintln!(" Binary: {binary}");
288 }
289}
290
291pub fn remove_lean_ctx_block_ps(content: &str) -> String {
292 let mut result = String::new();
293 let mut in_block = false;
294 let mut brace_depth = 0i32;
295
296 for line in content.lines() {
297 if line.contains("lean-ctx shell hook") {
298 in_block = true;
299 continue;
300 }
301 if in_block {
302 brace_depth += line.matches('{').count() as i32;
303 brace_depth -= line.matches('}').count() as i32;
304 if brace_depth <= 0 && (line.trim() == "}" || line.trim().is_empty()) {
305 if line.trim() == "}" {
306 in_block = false;
307 brace_depth = 0;
308 }
309 continue;
310 }
311 continue;
312 }
313 result.push_str(line);
314 result.push('\n');
315 }
316 result
317}
318
319pub fn generate_hook_fish(binary: &str) -> String {
320 let config = crate::core::config::Config::load();
321 let activation = config.shell_activation_effective();
322 let baked_default = match activation {
323 crate::core::config::ShellActivation::Always => "always",
324 crate::core::config::ShellActivation::AgentsOnly => "agents-only",
325 crate::core::config::ShellActivation::Off => "off",
326 };
327 let alias_list = crate::rewrite_registry::shell_alias_list();
328 format!(
329 "# lean-ctx shell hook — smart shell mode (track-by-default)\n\
330 set -g _lean_ctx_cmds {alias_list}\n\
331 \n\
332 function _lc_is_agent\n\
333 \tset -q LEAN_CTX_AGENT; or set -q CODEX_CLI_SESSION; or set -q CLAUDECODE; or set -q CODEBUDDY; or set -q GEMINI_SESSION\n\
334 end\n\
335 \n\
336 function _lc\n\
337 \tif set -q LEAN_CTX_DISABLED; or set -q LEAN_CTX_NO_HOOK\n\
338 \t\tcommand $argv\n\
339 \t\treturn\n\
340 \tend\n\
341 \tif not isatty stdout; and not _lc_is_agent\n\
342 \t\tcommand $argv\n\
343 \t\treturn\n\
344 \tend\n\
345 \t'{binary}' -t $argv\n\
346 \tset -l _lc_rc $status\n\
347 \tif test $_lc_rc -eq 127 -o $_lc_rc -eq 126\n\
348 \t\tcommand $argv\n\
349 \telse\n\
350 \t\treturn $_lc_rc\n\
351 \tend\n\
352 end\n\
353 \n\
354 function _lc_compress\n\
355 \tif set -q LEAN_CTX_DISABLED; or set -q LEAN_CTX_NO_HOOK\n\
356 \t\tcommand $argv\n\
357 \t\treturn\n\
358 \tend\n\
359 \tif not isatty stdout; and not _lc_is_agent\n\
360 \t\tcommand $argv\n\
361 \t\treturn\n\
362 \tend\n\
363 \t'{binary}' -c $argv\n\
364 \tset -l _lc_rc $status\n\
365 \tif test $_lc_rc -eq 127 -o $_lc_rc -eq 126\n\
366 \t\tcommand $argv\n\
367 \telse\n\
368 \t\treturn $_lc_rc\n\
369 \tend\n\
370 end\n\
371 \n\
372 function lean-ctx-on\n\
373 \tfor _lc_cmd in $_lean_ctx_cmds\n\
374 \t\talias $_lc_cmd '_lc '$_lc_cmd\n\
375 \tend\n\
376 \talias k '_lc kubectl'\n\
377 \tset -gx LEAN_CTX_ENABLED 1\n\
378 \tisatty stdout; and echo 'lean-ctx: ON (track mode — output unchanged, token savings recorded)'\n\
379 end\n\
380 \n\
381 function lean-ctx-off\n\
382 \tfor _lc_cmd in $_lean_ctx_cmds\n\
383 \t\tfunctions --erase $_lc_cmd 2>/dev/null; true\n\
384 \tend\n\
385 \tfunctions --erase k 2>/dev/null; true\n\
386 \tset -gx LEAN_CTX_ENABLED 0\n\
387 \tisatty stdout; and echo 'lean-ctx: OFF'\n\
388 end\n\
389 \n\
390 function lean-ctx-mode\n\
391 \tswitch $argv[1]\n\
392 \t\tcase compress\n\
393 \t\t\tfor _lc_cmd in $_lean_ctx_cmds\n\
394 \t\t\t\talias $_lc_cmd '_lc_compress '$_lc_cmd\n\
395 \t\t\t\tend\n\
396 \t\t\talias k '_lc_compress kubectl'\n\
397 \t\t\tset -gx LEAN_CTX_ENABLED 1\n\
398 \t\t\tisatty stdout; and echo 'lean-ctx: COMPRESS mode (all output compressed)'\n\
399 \t\tcase track\n\
400 \t\t\tlean-ctx-on\n\
401 \t\tcase off\n\
402 \t\t\tlean-ctx-off\n\
403 \t\tcase '*'\n\
404 \t\t\techo 'Usage: lean-ctx-mode <track|compress|off>'\n\
405 \t\t\techo ' track — Full output, stats recorded (default)'\n\
406 \t\t\techo ' compress — Compressed output for all commands'\n\
407 \t\t\techo ' off — No aliases, raw shell'\n\
408 \tend\n\
409 end\n\
410 \n\
411 function lean-ctx-raw\n\
412 \tset -lx LEAN_CTX_RAW 1\n\
413 \tcommand $argv\n\
414 end\n\
415 \n\
416 function lean-ctx-status\n\
417 \tif set -q LEAN_CTX_DISABLED\n\
418 \t\tisatty stdout; and echo 'lean-ctx: DISABLED (LEAN_CTX_DISABLED is set)'\n\
419 \telse if set -q LEAN_CTX_ENABLED\n\
420 \t\tisatty stdout; and echo 'lean-ctx: ON'\n\
421 \telse\n\
422 \t\tisatty stdout; and echo 'lean-ctx: OFF'\n\
423 \tend\n\
424 end\n\
425 \n\
426 function _lean_ctx_should_activate\n\
427 \tif set -q LEAN_CTX_ACTIVE; or set -q LEAN_CTX_DISABLED; or test (set -q LEAN_CTX_ENABLED; and echo $LEAN_CTX_ENABLED; or echo 1) = '0'\n\
428 \t\treturn 1\n\
429 \tend\n\
430 \tset -l _lc_mode (set -q LEAN_CTX_SHELL_ACTIVATION; and echo $LEAN_CTX_SHELL_ACTIVATION; or echo '{baked_default}')\n\
431 \tswitch $_lc_mode\n\
432 \t\tcase off none manual\n\
433 \t\t\treturn 1\n\
434 \t\tcase 'agents-only' agents_only agentsonly\n\
435 \t\t\tif set -q LEAN_CTX_AGENT; or set -q CLAUDECODE; or set -q CODEBUDDY; or set -q CODEX_CLI_SESSION; or set -q GEMINI_SESSION\n\
436 \t\t\t\treturn 0\n\
437 \t\t\tend\n\
438 \t\t\treturn 1\n\
439 \t\tcase '*'\n\
440 \t\t\treturn 0\n\
441 \tend\n\
442 end\n\
443 \n\
444 if _lean_ctx_should_activate\n\
445 \tif command -q lean-ctx\n\
446 \t\tlean-ctx-on\n\
447 \tend\n\
448 end\n"
449 )
450}
451
452pub fn init_fish(binary: &str) {
453 let config = dirs::home_dir()
454 .map(|h| h.join(".config/fish/config.fish"))
455 .unwrap_or_default();
456
457 let hook_content = generate_hook_fish(binary);
458
459 if write_hook_file("shell-hook.fish", &hook_content).is_some() {
460 upsert_source_line(&config, &source_line_fish());
461 qprintln!(" Binary: {binary}");
462 }
463}
464
465pub fn generate_hook_posix(binary: &str) -> String {
466 let config = crate::core::config::Config::load();
467 let activation = config.shell_activation_effective();
468 let baked_default = match activation {
469 crate::core::config::ShellActivation::Always => "always",
470 crate::core::config::ShellActivation::AgentsOnly => "agents-only",
471 crate::core::config::ShellActivation::Off => "off",
472 };
473 let alias_list = crate::rewrite_registry::shell_alias_list();
474 format!(
475 r#"# lean-ctx shell hook — smart shell mode (track-by-default)
476_lean_ctx_cmds=({alias_list})
477
478_lc_is_agent() {{
479 [ -n "${{LEAN_CTX_AGENT:-}}" ] || [ -n "${{CODEX_CLI_SESSION:-}}" ] || [ -n "${{CLAUDECODE:-}}" ] || [ -n "${{CODEBUDDY:-}}" ] || [ -n "${{GEMINI_SESSION:-}}" ]
480}}
481
482_lc() {{
483 if [ -n "${{LEAN_CTX_DISABLED:-}}" ] || [ -n "${{LEAN_CTX_NO_HOOK:-}}" ]; then
484 command "$@"
485 return
486 fi
487 if [ ! -t 1 ] && ! _lc_is_agent; then
488 command "$@"
489 return
490 fi
491 '{binary}' -t "$@"
492 local _lc_rc=$?
493 if [ "$_lc_rc" -eq 127 ] || [ "$_lc_rc" -eq 126 ]; then
494 command "$@"
495 else
496 return "$_lc_rc"
497 fi
498}}
499
500_lc_compress() {{
501 if [ -n "${{LEAN_CTX_DISABLED:-}}" ] || [ -n "${{LEAN_CTX_NO_HOOK:-}}" ]; then
502 command "$@"
503 return
504 fi
505 if [ ! -t 1 ] && ! _lc_is_agent; then
506 command "$@"
507 return
508 fi
509 '{binary}' -c "$@"
510 local _lc_rc=$?
511 if [ "$_lc_rc" -eq 127 ] || [ "$_lc_rc" -eq 126 ]; then
512 command "$@"
513 else
514 return "$_lc_rc"
515 fi
516}}
517
518lean-ctx-on() {{
519 for _lc_cmd in "${{_lean_ctx_cmds[@]}}"; do
520 # shellcheck disable=SC2139
521 alias "$_lc_cmd"='_lc '"$_lc_cmd"
522 done
523 alias k='_lc kubectl'
524 export LEAN_CTX_ENABLED=1
525 [ -t 1 ] && echo "lean-ctx: ON (track mode — output unchanged, token savings recorded)"
526}}
527
528lean-ctx-off() {{
529 for _lc_cmd in "${{_lean_ctx_cmds[@]}}"; do
530 unalias "$_lc_cmd" 2>/dev/null || true
531 done
532 unalias k 2>/dev/null || true
533 export LEAN_CTX_ENABLED=0
534 [ -t 1 ] && echo "lean-ctx: OFF"
535}}
536
537lean-ctx-mode() {{
538 case "${{1:-}}" in
539 compress)
540 for _lc_cmd in "${{_lean_ctx_cmds[@]}}"; do
541 # shellcheck disable=SC2139
542 alias "$_lc_cmd"='_lc_compress '"$_lc_cmd"
543 done
544 alias k='_lc_compress kubectl'
545 export LEAN_CTX_ENABLED=1
546 [ -t 1 ] && echo "lean-ctx: COMPRESS mode (all output compressed)"
547 ;;
548 track)
549 lean-ctx-on
550 ;;
551 off)
552 lean-ctx-off
553 ;;
554 *)
555 echo "Usage: lean-ctx-mode <track|compress|off>"
556 echo " track — Full output, stats recorded (default)"
557 echo " compress — Compressed output for all commands"
558 echo " off — No aliases, raw shell"
559 ;;
560 esac
561}}
562
563lean-ctx-raw() {{
564 LEAN_CTX_RAW=1 command "$@"
565}}
566
567lean-ctx-status() {{
568 if [ -n "${{LEAN_CTX_DISABLED:-}}" ]; then
569 [ -t 1 ] && echo "lean-ctx: DISABLED (LEAN_CTX_DISABLED is set)"
570 elif [ -n "${{LEAN_CTX_ENABLED:-}}" ]; then
571 [ -t 1 ] && echo "lean-ctx: ON"
572 else
573 [ -t 1 ] && echo "lean-ctx: OFF"
574 fi
575}}
576
577if [ -n "${{ZSH_VERSION:-}}" ]; then
578 _lean_ctx_comp() {{
579 shift words
580 (( CURRENT-- ))
581 _normal
582 }}
583 compdef _lean_ctx_comp _lc 2>/dev/null
584 compdef _lean_ctx_comp _lc_compress 2>/dev/null
585fi
586
587_lean_ctx_should_activate() {{
588 [ -z "${{LEAN_CTX_ACTIVE:-}}" ] && [ -z "${{LEAN_CTX_DISABLED:-}}" ] && [ "${{LEAN_CTX_ENABLED:-1}}" != "0" ] || return 1
589 case "${{LEAN_CTX_SHELL_ACTIVATION:-{baked_default}}}" in
590 off|none|manual) return 1 ;;
591 agents-only|agents_only|agentsonly)
592 [ -n "${{LEAN_CTX_AGENT:-}}" ] || [ -n "${{CLAUDECODE:-}}" ] || [ -n "${{CODEBUDDY:-}}" ] || [ -n "${{CODEX_CLI_SESSION:-}}" ] || [ -n "${{GEMINI_SESSION:-}}" ] ;;
593 *) return 0 ;;
594 esac
595}}
596
597if _lean_ctx_should_activate; then
598 command -v lean-ctx >/dev/null 2>&1 && lean-ctx-on
599fi
600"#
601 )
602}
603
604pub fn init_posix(is_zsh: bool, binary: &str) {
605 let rc_file = if is_zsh {
606 dirs::home_dir()
607 .map(|h| h.join(".zshrc"))
608 .unwrap_or_default()
609 } else {
610 dirs::home_dir()
611 .map(|h| h.join(".bashrc"))
612 .unwrap_or_default()
613 };
614
615 let shell_ext = if is_zsh { "zsh" } else { "bash" };
616 let hook_content = generate_hook_posix(binary);
617
618 if let Some(hook_path) = write_hook_file(&format!("shell-hook.{shell_ext}"), &hook_content) {
619 upsert_source_line(&rc_file, &source_line_posix(shell_ext));
620
621 if !is_zsh {
624 ensure_bash_login_sources_bashrc();
625 }
626
627 qprintln!(" Binary: {binary}");
628
629 write_env_sh_for_containers(&hook_content);
630 write_lc_path_shims(binary);
631 print_docker_env_hints(is_zsh);
632
633 let _ = hook_path;
634 }
635}
636
637fn ensure_bash_login_sources_bashrc() {
644 let Some(home) = dirs::home_dir() else {
645 return;
646 };
647
648 let target = [".bash_profile", ".bash_login", ".profile"]
651 .iter()
652 .map(|f| home.join(f))
653 .find(|p| p.exists())
654 .unwrap_or_else(|| home.join(".bash_profile"));
655
656 if let Ok(existing) = std::fs::read_to_string(&target) {
658 let sources_bashrc = existing
659 .lines()
660 .any(|l| !l.trim_start().starts_with('#') && l.contains(".bashrc"));
661 if sources_bashrc {
662 return;
663 }
664 }
665
666 let snippet = "\n# lean-ctx: load ~/.bashrc in login shells (e.g. macOS Terminal) — begin\n\
667 if [ -f \"$HOME/.bashrc\" ]; then . \"$HOME/.bashrc\"; fi\n\
668 # lean-ctx: load ~/.bashrc in login shells (e.g. macOS Terminal) — end\n";
669
670 backup_shell_config(&target);
671 match std::fs::OpenOptions::new()
672 .append(true)
673 .create(true)
674 .open(&target)
675 {
676 Ok(mut f) => {
677 use std::io::Write;
678 if f.write_all(snippet.as_bytes()).is_ok() {
679 qprintln!(" Login shell: {} now sources ~/.bashrc", target.display());
680 }
681 }
682 Err(e) => {
683 tracing::warn!("could not update {}: {e}", target.display());
684 }
685 }
686}
687
688pub fn write_env_sh_for_containers(aliases: &str) {
689 let env_sh = match crate::core::paths::config_dir() {
691 Ok(d) => d.join("env.sh"),
692 Err(_) => return,
693 };
694 if let Some(parent) = env_sh.parent() {
695 let _ = std::fs::create_dir_all(parent);
696 }
697 let sanitized_aliases = crate::core::sanitize::neutralize_shell_content(aliases);
698 let mut content = String::from(
699 r#"# lean-ctx: passthrough stubs for non-interactive subshells (fixes #255).
700# These ensure _lc/_lc_compress exist so inherited aliases don't break.
701# The full hook definitions override these when the interactive shell loads.
702_lc() { command "$@"; }
703_lc_compress() { command "$@"; }
704
705"#,
706 );
707 content.push_str(&sanitized_aliases);
708 content.push_str(
709 r#"
710
711# lean-ctx docker self-heal: re-inject Claude MCP config if Claude overwrote ~/.claude.json
712# Guards: container-only + no recursion + no re-entry via BASH_ENV + 60s cooldown + PID-lock
713if [ -f /.dockerenv ] || grep -qsE '/docker/|/lxc/' /proc/1/cgroup 2>/dev/null; then
714 if [ -z "${LEAN_CTX_ACTIVE:-}" ] && [ -z "${_LEAN_CTX_HEAL:-}" ]; then
715 # XDG-only paths (GL #623): never touch ~/.lean-ctx, which would re-collapse
716 # a committed XDG layout. heal_ts is STATE, locks live in the DATA dir
717 # (matches process_guard::lock_dir defaults).
718 _LEAN_CTX_STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/lean-ctx"
719 _LEAN_CTX_HEAL_TS="${_LEAN_CTX_STATE_DIR}/.heal_ts"
720 _LEAN_CTX_HEAL_COOLDOWN=60
721 _lean_ctx_heal_needed=1
722 if [ -f "$_LEAN_CTX_HEAL_TS" ]; then
723 _last_heal=$(cat "$_LEAN_CTX_HEAL_TS" 2>/dev/null || echo 0)
724 _now=$(date +%s 2>/dev/null || echo 0)
725 if [ $(( _now - _last_heal )) -lt $_LEAN_CTX_HEAL_COOLDOWN ]; then
726 _lean_ctx_heal_needed=0
727 fi
728 fi
729 _lean_ctx_lock_count=0
730 for _lf in "${XDG_DATA_HOME:-$HOME/.local/share}/lean-ctx/locks"/slot-*.lock; do
731 [ -f "$_lf" ] && _lean_ctx_lock_count=$(( _lean_ctx_lock_count + 1 ))
732 done
733 if [ "$_lean_ctx_heal_needed" = "1" ] && [ "$_lean_ctx_lock_count" -lt 4 ]; then
734 export _LEAN_CTX_HEAL=1
735 if command -v claude >/dev/null 2>&1 && command -v lean-ctx >/dev/null 2>&1; then
736 if ! claude mcp list 2>/dev/null | grep -q "lean-ctx"; then
737 LEAN_CTX_ACTIVE=1 LEAN_CTX_QUIET=1 lean-ctx init --agent claude >/dev/null 2>&1
738 mkdir -p "$_LEAN_CTX_STATE_DIR" 2>/dev/null
739 date +%s > "$_LEAN_CTX_HEAL_TS" 2>/dev/null
740 fi
741 fi
742 fi
743 fi
744fi
745"#,
746 );
747 match std::fs::write(&env_sh, content) {
748 Ok(()) => {
749 if !super::quiet_enabled() {
751 eprintln!(" env.sh: {}", env_sh.display());
752 }
753 }
754 Err(e) => tracing::warn!("could not write {}: {e}", env_sh.display()),
755 }
756}
757
758fn lc_shim_dir() -> Option<std::path::PathBuf> {
762 std::env::current_exe()
763 .ok()
764 .and_then(|p| p.parent().map(std::path::Path::to_path_buf))
765}
766
767fn shim_script(name: &str, binary: &str, flag: &str) -> String {
772 format!(
773 "#!/bin/sh\n\
774 # lean-ctx PATH fallback for the `{name}` shell function -- DO NOT EDIT.\n\
775 # Shell resolves alias -> function -> PATH, so the hook's shell function\n\
776 # shadows this whenever it is loaded (identical behavior there). This runs\n\
777 # only where the function is absent: non-interactive subshells, scripts,\n\
778 # xargs/find -exec, a pipeline's outer shell, and agent harnesses that\n\
779 # snapshot+replay the shell and drop the function but keep the aliases\n\
780 # that call it. Without it those contexts fail `{name}: command not found`.\n\
781 if [ -n \"${{LEAN_CTX_DISABLED:-}}\" ] || [ -n \"${{LEAN_CTX_NO_HOOK:-}}\" ]; then\n\
782 \texec \"$@\"\n\
783 fi\n\
784 if [ ! -t 1 ] && [ -z \"${{LEAN_CTX_AGENT:-}}\" ] && [ -z \"${{CODEX_CLI_SESSION:-}}\" ] \\\n\
785 \t&& [ -z \"${{CLAUDECODE:-}}\" ] && [ -z \"${{CODEBUDDY:-}}\" ] && [ -z \"${{GEMINI_SESSION:-}}\" ]; then\n\
786 \texec \"$@\"\n\
787 fi\n\
788 '{binary}' {flag} \"$@\"\n\
789 _lc_rc=$?\n\
790 if [ \"$_lc_rc\" -eq 127 ] || [ \"$_lc_rc\" -eq 126 ]; then\n\
791 \texec \"$@\"\n\
792 fi\n\
793 exit \"$_lc_rc\"\n"
794 )
795}
796
797fn write_lc_path_shims_in(dir: &std::path::Path, binary: &str) {
799 for (name, flag) in [("_lc", "-t"), ("_lc_compress", "-c")] {
800 let path = dir.join(name);
801 if let Err(e) = std::fs::write(&path, shim_script(name, binary, flag)) {
802 tracing::warn!("could not write shim {}: {e}", path.display());
803 continue;
804 }
805 #[cfg(unix)]
806 {
807 use std::os::unix::fs::PermissionsExt;
808 let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755));
809 }
810 }
811}
812
813fn write_lc_path_shims(binary: &str) {
818 if let Some(dir) = lc_shim_dir() {
819 write_lc_path_shims_in(&dir, binary);
820 }
821}
822
823fn print_docker_env_hints(is_zsh: bool) {
824 if is_zsh || !crate::shell::is_container() {
825 return;
826 }
827 let env_sh = crate::core::paths::config_dir().map_or_else(
828 |_| "/root/.config/lean-ctx/env.sh".to_string(),
829 |d| d.join("env.sh").to_string_lossy().to_string(),
830 );
831
832 let has_bash_env = std::env::var("BASH_ENV").is_ok();
833 let has_claude_env = std::env::var("CLAUDE_ENV_FILE").is_ok();
834
835 if has_bash_env && has_claude_env {
836 return;
837 }
838
839 eprintln!();
840 eprintln!(" \x1b[33m⚠ Docker detected — environment hints:\x1b[0m");
841
842 if !has_bash_env {
843 eprintln!(" For generic bash -c usage (non-interactive shells):");
844 eprintln!(" \x1b[1mENV BASH_ENV=\"{env_sh}\"\x1b[0m");
845 }
846 if !has_claude_env {
847 eprintln!(" For Claude Code (sources before each command):");
848 eprintln!(" \x1b[1mENV CLAUDE_ENV_FILE=\"{env_sh}\"\x1b[0m");
849 }
850 eprintln!();
851}
852
853pub fn remove_lean_ctx_block(content: &str) -> String {
854 if content.contains("# lean-ctx shell hook — end") {
855 return remove_lean_ctx_block_by_marker(content);
856 }
857 remove_lean_ctx_block_legacy(content)
858}
859
860fn remove_lean_ctx_block_by_marker(content: &str) -> String {
861 let mut result = String::new();
862 let mut in_block = false;
863
864 for line in content.lines() {
865 if !in_block && line.contains("lean-ctx shell hook") && !line.contains("end") {
866 in_block = true;
867 continue;
868 }
869 if in_block {
870 if line.trim() == "# lean-ctx shell hook — end" {
871 in_block = false;
872 }
873 continue;
874 }
875 result.push_str(line);
876 result.push('\n');
877 }
878 result
879}
880
881fn remove_lean_ctx_block_legacy(content: &str) -> String {
882 let mut result = String::new();
883 let mut in_block = false;
884
885 for line in content.lines() {
886 if line.contains("lean-ctx shell hook") {
887 in_block = true;
888 continue;
889 }
890 if in_block {
891 if line.trim() == "fi" || line.trim() == "end" || line.trim().is_empty() {
892 if line.trim() == "fi" || line.trim() == "end" {
893 in_block = false;
894 }
895 continue;
896 }
897 if !line.starts_with("alias ") && !line.starts_with('\t') && !line.starts_with("if ") {
898 in_block = false;
899 result.push_str(line);
900 result.push('\n');
901 }
902 continue;
903 }
904 result.push_str(line);
905 result.push('\n');
906 }
907 result
908}
909
910#[cfg(test)]
911mod tests {
912 use super::*;
913
914 #[test]
915 fn lc_shim_script_is_self_contained_fallback() {
916 let s = shim_script("_lc", "/usr/bin/lean-ctx", "-t");
917 assert!(s.starts_with("#!/bin/sh\n"), "needs a shebang: {s}");
918 assert!(s.contains("'/usr/bin/lean-ctx' -t \"$@\""), "{s}");
919 assert!(s.contains("exec \"$@\""), "{s}");
920 assert!(s.contains("CLAUDECODE"), "{s}");
921 assert!(s.contains("LEAN_CTX_DISABLED"), "{s}");
922 }
923
924 #[test]
925 fn lc_compress_shim_uses_compress_flag() {
926 let s = shim_script("_lc_compress", "/usr/bin/lean-ctx", "-c");
927 assert!(s.contains("'/usr/bin/lean-ctx' -c \"$@\""), "{s}");
928 }
929
930 #[test]
931 fn write_lc_path_shims_writes_both_executables() {
932 let tmp = tempfile::tempdir().expect("tempdir");
933 write_lc_path_shims_in(tmp.path(), "/usr/bin/lean-ctx");
934 for name in ["_lc", "_lc_compress"] {
935 assert!(tmp.path().join(name).exists(), "missing shim {name}");
936 }
937 }
938
939 #[test]
940 fn test_remove_lean_ctx_block_posix() {
941 let input = r#"# existing config
942export PATH="$HOME/bin:$PATH"
943
944# lean-ctx shell hook — transparent CLI compression (95+ patterns)
945if [ -z "$LEAN_CTX_ACTIVE" ]; then
946alias git='lean-ctx -c git'
947alias npm='lean-ctx -c npm'
948fi
949
950# other stuff
951export EDITOR=vim
952"#;
953 let result = remove_lean_ctx_block(input);
954 assert!(!result.contains("lean-ctx"), "block should be removed");
955 assert!(result.contains("export PATH"), "other content preserved");
956 assert!(
957 result.contains("export EDITOR"),
958 "trailing content preserved"
959 );
960 }
961
962 #[test]
963 fn test_remove_lean_ctx_block_fish() {
964 let input = "# other fish config\nset -x FOO bar\n\n# lean-ctx shell hook — transparent CLI compression (95+ patterns)\nif not set -q LEAN_CTX_ACTIVE\n\talias git 'lean-ctx -c git'\n\talias npm 'lean-ctx -c npm'\nend\n\n# more config\nset -x BAZ qux\n";
965 let result = remove_lean_ctx_block(input);
966 assert!(!result.contains("lean-ctx"), "block should be removed");
967 assert!(result.contains("set -x FOO"), "other content preserved");
968 assert!(result.contains("set -x BAZ"), "trailing content preserved");
969 }
970
971 #[test]
972 fn test_remove_lean_ctx_block_ps() {
973 let input = "# PowerShell profile\n$env:FOO = 'bar'\n\n# lean-ctx shell hook — transparent CLI compression (95+ patterns)\nif (-not $env:LEAN_CTX_ACTIVE) {\n $LeanCtxBin = \"C:\\\\bin\\\\lean-ctx.exe\"\n function git { & $LeanCtxBin -c \"git $($args -join ' ')\" }\n}\n\n# other stuff\n$env:EDITOR = 'vim'\n";
974 let result = remove_lean_ctx_block_ps(input);
975 assert!(
976 !result.contains("lean-ctx shell hook"),
977 "block should be removed"
978 );
979 assert!(result.contains("$env:FOO"), "other content preserved");
980 assert!(result.contains("$env:EDITOR"), "trailing content preserved");
981 }
982
983 #[test]
984 fn test_remove_lean_ctx_block_ps_nested() {
985 let input = "# PowerShell profile\n$env:FOO = 'bar'\n\n# lean-ctx shell hook — transparent CLI compression (95+ patterns)\nif (-not $env:LEAN_CTX_ACTIVE) {\n $LeanCtxBin = \"lean-ctx\"\n function _lc {\n & $LeanCtxBin -c \"$($args -join ' ')\"\n }\n if (Get-Command lean-ctx -ErrorAction SilentlyContinue) {\n function git { _lc git @args }\n foreach ($c in @('npm','pnpm')) {\n if ($a) {\n Set-Variable -Name \"_lc_$c\" -Value $a.Source -Scope Script\n }\n }\n }\n}\n\n# other stuff\n$env:EDITOR = 'vim'\n";
986 let result = remove_lean_ctx_block_ps(input);
987 assert!(
988 !result.contains("lean-ctx shell hook"),
989 "block should be removed"
990 );
991 assert!(!result.contains("_lc"), "function should be removed");
992 assert!(result.contains("$env:FOO"), "other content preserved");
993 assert!(result.contains("$env:EDITOR"), "trailing content preserved");
994 }
995
996 #[test]
997 fn test_remove_block_no_lean_ctx() {
998 let input = "# normal bashrc\nexport PATH=\"$HOME/bin:$PATH\"\n";
999 let result = remove_lean_ctx_block(input);
1000 assert!(result.contains("export PATH"), "content unchanged");
1001 }
1002
1003 #[test]
1004 fn test_bash_hook_contains_pipe_guard_and_agent_bypass() {
1005 let output = generate_hook_posix("/usr/local/bin/lean-ctx");
1006 assert!(
1007 output.contains("! -t 1"),
1008 "bash/zsh hook must contain pipe guard [ ! -t 1 ]"
1009 );
1010 assert!(
1011 output.contains("_lc_is_agent"),
1012 "bash/zsh hook must have agent-aware bypass"
1013 );
1014 assert!(
1015 output.contains("CODEX_CLI_SESSION"),
1016 "agent check must include CODEX_CLI_SESSION"
1017 );
1018 }
1019
1020 #[test]
1021 fn test_lc_uses_track_mode_by_default() {
1022 let binary = "/usr/local/bin/lean-ctx";
1023 let alias_list = crate::rewrite_registry::shell_alias_list();
1024 let aliases = format!(
1025 r#"_lc() {{
1026 '{binary}' -t "$@"
1027}}
1028_lc_compress() {{
1029 '{binary}' -c "$@"
1030}}"#
1031 );
1032 assert!(
1033 aliases.contains("-t \"$@\""),
1034 "_lc must use -t (track mode) by default"
1035 );
1036 assert!(
1037 aliases.contains("-c \"$@\""),
1038 "_lc_compress must use -c (compress mode)"
1039 );
1040 let _ = alias_list;
1041 }
1042
1043 #[test]
1044 fn test_posix_shell_has_lean_ctx_mode() {
1045 let alias_list = crate::rewrite_registry::shell_alias_list();
1046 let aliases = r#"
1047lean-ctx-mode() {{
1048 case "${{1:-}}" in
1049 compress) echo compress ;;
1050 track) echo track ;;
1051 off) echo off ;;
1052 esac
1053}}
1054"#
1055 .to_string();
1056 assert!(
1057 aliases.contains("lean-ctx-mode()"),
1058 "lean-ctx-mode function must exist"
1059 );
1060 assert!(
1061 aliases.contains("compress"),
1062 "compress mode must be available"
1063 );
1064 assert!(aliases.contains("track"), "track mode must be available");
1065 let _ = alias_list;
1066 }
1067
1068 #[test]
1069 fn test_fish_hook_contains_pipe_guard_and_agent_bypass() {
1070 let output = generate_hook_fish("/usr/local/bin/lean-ctx");
1071 assert!(
1072 output.contains("isatty stdout"),
1073 "fish hook must contain pipe guard (isatty stdout)"
1074 );
1075 assert!(
1076 output.contains("_lc_is_agent"),
1077 "fish hook must have agent-aware bypass"
1078 );
1079 }
1080
1081 #[test]
1082 fn test_powershell_hook_contains_pipe_guard() {
1083 let hook = "function _lc { if ($env:LEAN_CTX_DISABLED -or [Console]::IsOutputRedirected) { & @args; return } }";
1084 assert!(
1085 hook.contains("IsOutputRedirected"),
1086 "PowerShell hook must contain pipe guard ([Console]::IsOutputRedirected)"
1087 );
1088 }
1089
1090 #[test]
1091 fn powershell_hook_binary_is_native_not_msys() {
1092 let win = "C:/Users/Dawid/.cargo/bin/lean-ctx.exe";
1095 assert_eq!(hook_binary_for_shell("powershell", win), win);
1096 assert_eq!(hook_binary_for_shell("pwsh", win), win);
1097 assert!(!hook_binary_for_shell("powershell", win).contains("/c/"));
1098 }
1099
1100 #[test]
1101 fn posix_hook_binary_keeps_msys_form_on_windows_drive() {
1102 let win = "C:/Users/Dawid/.cargo/bin/lean-ctx.exe";
1105 let msys = "/c/Users/Dawid/.cargo/bin/lean-ctx.exe";
1106 assert_eq!(hook_binary_for_shell("bash", win), msys);
1107 assert_eq!(hook_binary_for_shell("zsh", win), msys);
1108 assert_eq!(hook_binary_for_shell("fish", win), msys);
1109 }
1110
1111 #[test]
1112 fn test_remove_lean_ctx_block_new_format_with_end_marker() {
1113 let input = r#"# existing config
1114export PATH="$HOME/bin:$PATH"
1115
1116# lean-ctx shell hook — transparent CLI compression (95+ patterns)
1117_lean_ctx_cmds=(git npm pnpm)
1118
1119lean-ctx-on() {
1120 for _lc_cmd in "${_lean_ctx_cmds[@]}"; do
1121 alias "$_lc_cmd"='lean-ctx -c '"$_lc_cmd"
1122 done
1123 export LEAN_CTX_ENABLED=1
1124 [ -t 1 ] && echo "lean-ctx: ON"
1125}
1126
1127lean-ctx-off() {
1128 export LEAN_CTX_ENABLED=0
1129 [ -t 1 ] && echo "lean-ctx: OFF"
1130}
1131
1132if [ -z "${LEAN_CTX_ACTIVE:-}" ] && [ "${LEAN_CTX_ENABLED:-1}" != "0" ]; then
1133 lean-ctx-on
1134fi
1135# lean-ctx shell hook — end
1136
1137# other stuff
1138export EDITOR=vim
1139"#;
1140 let result = remove_lean_ctx_block(input);
1141 assert!(!result.contains("lean-ctx-on"), "block should be removed");
1142 assert!(!result.contains("lean-ctx shell hook"), "marker removed");
1143 assert!(result.contains("export PATH"), "other content preserved");
1144 assert!(
1145 result.contains("export EDITOR"),
1146 "trailing content preserved"
1147 );
1148 }
1149
1150 #[test]
1151 fn env_sh_for_containers_includes_self_heal() {
1152 let _g = crate::core::data_dir::test_env_lock();
1153 let tmp = tempfile::tempdir().expect("tempdir");
1154 let config_dir = tmp.path().join("config");
1156 std::fs::create_dir_all(&config_dir).expect("mkdir config");
1157 crate::test_env::set_var("LEAN_CTX_CONFIG_DIR", &config_dir);
1158
1159 write_env_sh_for_containers("alias git='lean-ctx -c git'\n");
1160 let env_sh = config_dir.join("env.sh");
1161 let content = std::fs::read_to_string(&env_sh).expect("env.sh exists");
1162 if !cfg!(windows)
1163 && let Ok(mut bash) = std::process::Command::new("bash")
1164 .arg("-n")
1165 .arg(&env_sh)
1166 .spawn()
1167 {
1168 let ok = bash.wait().is_ok_and(|s| s.success());
1169 assert!(ok, "generated env.sh must be valid bash");
1170 }
1171 assert!(
1172 content.contains(r#"_lc() { command "$@"; }"#),
1173 "env.sh must contain _lc passthrough stub for non-interactive shells"
1174 );
1175 assert!(
1176 content.contains(r#"_lc_compress() { command "$@"; }"#),
1177 "env.sh must contain _lc_compress passthrough stub"
1178 );
1179 assert!(content.contains("lean-ctx docker self-heal"));
1180 assert!(content.contains("claude mcp list"));
1181 assert!(content.contains("lean-ctx init --agent claude"));
1182 assert!(
1183 content.contains("_LEAN_CTX_HEAL"),
1184 "env.sh must guard against recursive self-heal"
1185 );
1186 assert!(
1187 content.contains("LEAN_CTX_ACTIVE"),
1188 "env.sh must check LEAN_CTX_ACTIVE to prevent re-entry"
1189 );
1190 assert!(
1191 content.contains("/.dockerenv"),
1192 "env.sh self-heal must be gated to container environments"
1193 );
1194 assert!(
1198 !content.contains("$HOME/.lean-ctx") && !content.contains("${HOME}/.lean-ctx"),
1199 "self-heal must not touch ~/.lean-ctx (GL #623)"
1200 );
1201 assert!(
1202 content.contains("${XDG_STATE_HOME:-$HOME/.local/state}/lean-ctx"),
1203 "heal_ts must live under the XDG state dir"
1204 );
1205 assert!(
1206 content.contains("${XDG_DATA_HOME:-$HOME/.local/share}/lean-ctx/locks"),
1207 "lock count must read the XDG data lock dir"
1208 );
1209
1210 crate::test_env::remove_var("LEAN_CTX_CONFIG_DIR");
1211 }
1212
1213 #[cfg(unix)]
1214 #[test]
1215 fn bash_login_profile_sources_bashrc_idempotently() {
1216 let _g = crate::core::data_dir::test_env_lock();
1217 let tmp = tempfile::tempdir().expect("tempdir");
1218 let home = tmp.path();
1219 let prev = std::env::var_os("HOME");
1220 crate::test_env::set_var("HOME", home);
1221
1222 std::fs::write(home.join(".bashrc"), "# bashrc\n").expect("write .bashrc");
1223 ensure_bash_login_sources_bashrc();
1226 let profile = home.join(".bash_profile");
1227 let first = std::fs::read_to_string(&profile).expect(".bash_profile created");
1228 assert!(
1229 first.contains(". \"$HOME/.bashrc\""),
1230 "login profile must source ~/.bashrc: {first}"
1231 );
1232 let markers = first.matches("load ~/.bashrc in login shells").count();
1233
1234 ensure_bash_login_sources_bashrc();
1236 let second = std::fs::read_to_string(&profile).expect("read profile");
1237 assert_eq!(
1238 second.matches("load ~/.bashrc in login shells").count(),
1239 markers,
1240 "snippet must not be duplicated on re-run"
1241 );
1242
1243 match prev {
1244 Some(v) => crate::test_env::set_var("HOME", v),
1245 None => crate::test_env::remove_var("HOME"),
1246 }
1247 }
1248
1249 #[test]
1250 fn test_source_line_posix() {
1251 let line = source_line_posix("zsh");
1252 assert!(line.contains("shell-hook.zsh"));
1253 assert!(line.contains("[ -f"));
1254 }
1255
1256 #[test]
1257 fn test_source_line_fish() {
1258 let line = source_line_fish();
1259 assert!(line.contains("shell-hook.fish"));
1260 assert!(line.contains("source"));
1261 }
1262
1263 #[test]
1264 fn test_source_line_powershell() {
1265 let line = source_line_powershell();
1266 assert!(line.contains("shell-hook.ps1"));
1267 assert!(line.contains("Test-Path"));
1268 }
1269}