|
1 | 1 | package pm; |
2 | 2 |
|
3 | 3 | import pm.cli.OutputFormatter; |
| 4 | +import pm.completion.CompletionHandler; |
| 5 | +import pm.completion.CompletionScripts; |
4 | 6 | import pm.core.Project; |
5 | 7 | import pm.detector.ProjectType; |
6 | 8 | import pm.detector.ProjectTypeDetector; |
@@ -68,6 +70,12 @@ public class ProjectManager { |
68 | 70 | * @param args command-line arguments |
69 | 71 | */ |
70 | 72 | public static void main(String[] args) { |
| 73 | + // Fast path: shell completion callback (no banner, no update check) |
| 74 | + if (args.length > 0 && "--complete".equals(args[0])) { |
| 75 | + CompletionHandler.handle(args); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
71 | 79 | printBanner(); |
72 | 80 |
|
73 | 81 | // Check for updates in the background (non-blocking, 2s timeout) |
@@ -95,6 +103,7 @@ public static void main(String[] args) { |
95 | 103 | case "env" -> handleEnv(args); |
96 | 104 | case "hooks" -> handleHooks(args); |
97 | 105 | case "refresh" -> handleRefresh(args); |
| 106 | + case "completions" -> handleCompletions(args); |
98 | 107 | case "update" -> UpdateChecker.performUpdate(); |
99 | 108 | case "doctor" -> handleDoctor(); |
100 | 109 | case "help", "-h", "--help" -> printHelp(); |
@@ -1944,6 +1953,7 @@ private static void printHelp() { |
1944 | 1953 | env <subcommand> <name> [options] Manage environment variables |
1945 | 1954 | refresh <name> Re-detect type and update commands |
1946 | 1955 | refresh --all Refresh all registered projects |
| 1956 | + completions <shell> Generate completion script (bash/zsh/fish/powershell) |
1947 | 1957 | update Update to the latest version |
1948 | 1958 | doctor Check environment and runtimes |
1949 | 1959 | help Show this help |
@@ -1980,6 +1990,39 @@ Environment Variables (pm env): |
1980 | 1990 | """); |
1981 | 1991 | } |
1982 | 1992 |
|
| 1993 | + private static void handleCompletions(String[] args) { |
| 1994 | + if (args.length < 2) { |
| 1995 | + OutputFormatter.error("Shell name required."); |
| 1996 | + System.out.println("Usage: pm completions <shell>"); |
| 1997 | + System.out.println("Supported shells: bash, zsh, fish, powershell"); |
| 1998 | + System.out.println(); |
| 1999 | + System.out.println("Setup:"); |
| 2000 | + System.out.println(" Bash: eval \"$(pm completions bash)\" # add to ~/.bashrc"); |
| 2001 | + System.out.println(" Zsh: eval \"$(pm completions zsh)\" # add to ~/.zshrc"); |
| 2002 | + System.out.println(" Fish: pm completions fish > ~/.config/fish/completions/pm.fish"); |
| 2003 | + System.out.println(" PowerShell: pm completions powershell | Out-String | Invoke-Expression # add to $PROFILE"); |
| 2004 | + return; |
| 2005 | + } |
| 2006 | + |
| 2007 | + String shell = args[1].toLowerCase(); |
| 2008 | + String script = switch (shell) { |
| 2009 | + case "bash" -> CompletionScripts.bash(); |
| 2010 | + case "zsh" -> CompletionScripts.zsh(); |
| 2011 | + case "fish" -> CompletionScripts.fish(); |
| 2012 | + case "powershell", "pwsh" -> CompletionScripts.powershell(); |
| 2013 | + default -> null; |
| 2014 | + }; |
| 2015 | + |
| 2016 | + if (script == null) { |
| 2017 | + OutputFormatter.error("Unsupported shell: " + args[1]); |
| 2018 | + System.out.println("Supported: bash, zsh, fish, powershell"); |
| 2019 | + return; |
| 2020 | + } |
| 2021 | + |
| 2022 | + // Print raw script to stdout (no ANSI, suitable for eval/sourcing) |
| 2023 | + System.out.print(script); |
| 2024 | + } |
| 2025 | + |
1983 | 2026 | private static void printVersion() { |
1984 | 2027 | System.out.println("ProjectManager " + Constants.VERSION); |
1985 | 2028 | System.out.println("Java " + System.getProperty("java.version")); |
|
0 commit comments