Coverage for src/workstack/cli/commands/completion.py: 38%

34 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-10-19 09:31 -0400

1import os 

2import shutil 

3import subprocess 

4import sys 

5 

6import click 

7 

8 

9@click.group("completion") 

10def completion_group() -> None: 

11 """Generate shell completion scripts.""" 

12 

13 

14@completion_group.command("bash") 

15def completion_bash() -> None: 

16 """Generate bash completion script. 

17 

18 \b 

19 For automatic setup of both completion and auto-activation: 

20 workstack init --shell 

21 

22 \b 

23 To load completions in your current shell session: 

24 source <(workstack completion bash) 

25 

26 \b 

27 To load completions permanently, add to your ~/.bashrc: 

28 echo 'source <(workstack completion bash)' >> ~/.bashrc 

29 

30 \b 

31 Alternatively, you can save the completion script to bash_completion.d: 

32 workstack completion bash > /usr/local/etc/bash_completion.d/workstack 

33 

34 \b 

35 You will need to start a new shell for this setup to take effect. 

36 """ 

37 # Find the workstack executable 

38 workstack_exe = shutil.which("workstack") 

39 if not workstack_exe: 

40 # Fallback to current Python + module 

41 workstack_exe = sys.argv[0] 

42 

43 env = os.environ.copy() 

44 env["_WORKSTACK_COMPLETE"] = "bash_source" 

45 result = subprocess.run([workstack_exe], env=env, capture_output=True, text=True) 

46 click.echo(result.stdout, nl=False) 

47 

48 

49@completion_group.command("zsh") 

50def completion_zsh() -> None: 

51 """Generate zsh completion script. 

52 

53 \b 

54 For automatic setup of both completion and auto-activation: 

55 workstack init --shell 

56 

57 \b 

58 To load completions in your current shell session: 

59 source <(workstack completion zsh) 

60 

61 \b 

62 To load completions permanently, add to your ~/.zshrc: 

63 echo 'source <(workstack completion zsh)' >> ~/.zshrc 

64 

65 \b 

66 Note: Make sure compinit is called in your ~/.zshrc after loading completions. 

67 

68 \b 

69 You will need to start a new shell for this setup to take effect. 

70 """ 

71 # Find the workstack executable 

72 workstack_exe = shutil.which("workstack") 

73 if not workstack_exe: 

74 # Fallback to current Python + module 

75 workstack_exe = sys.argv[0] 

76 

77 env = os.environ.copy() 

78 env["_WORKSTACK_COMPLETE"] = "zsh_source" 

79 result = subprocess.run([workstack_exe], env=env, capture_output=True, text=True) 

80 click.echo(result.stdout, nl=False) 

81 

82 

83@completion_group.command("fish") 

84def completion_fish() -> None: 

85 """Generate fish completion script. 

86 

87 \b 

88 For automatic setup of both completion and auto-activation: 

89 workstack init --shell 

90 

91 \b 

92 To load completions in your current shell session: 

93 workstack completion fish | source 

94 

95 \b 

96 To load completions permanently: 

97 mkdir -p ~/.config/fish/completions && \\ 

98 workstack completion fish > ~/.config/fish/completions/workstack.fish 

99 

100 \b 

101 You will need to start a new shell for this setup to take effect. 

102 """ 

103 # Find the workstack executable 

104 workstack_exe = shutil.which("workstack") 

105 if not workstack_exe: 

106 # Fallback to current Python + module 

107 workstack_exe = sys.argv[0] 

108 

109 env = os.environ.copy() 

110 env["_WORKSTACK_COMPLETE"] = "fish_source" 

111 result = subprocess.run([workstack_exe], env=env, capture_output=True, text=True) 

112 click.echo(result.stdout, nl=False)