Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add shell completions #1213

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions content/docs/pages/docs/cli-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,47 @@ note: if you don't provide a metadata override file, screenpipe will automatical
screenpipe migrate
```


### Shell Completions

The `screenpipe` CLI supports generating shell completions for popular shells. Follow the steps below to enable autocompletion for your shell:

#### Bash
1. Generate completions:
```bash
screenpipe completions bash >> ~/.bashrc
```
2. Reload your profile:
```bash
source ~/.bashrc
```

#### Zsh
1. Generate completions:
```bash
screenpipe completions zsh >> ~/.zshrc
```
2. Reload your profile:
```bash
source ~/.zshrc
```

#### Fish
1. Generate completions:
```fish
screenpipe completions fish > ~/.config/fish/completions/screenpipe.fish
```

#### PowerShell
1. Generate completions:
```powershell
screenpipe completions powershell >> $PROFILE
```
2. Reload your profile:
```powershell
. $PROFILE
```

**Note:** Completions must be redirected to a file before use.

</MotionDiv>
3 changes: 3 additions & 0 deletions screenpipe-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ sysinfo = "0.29.0"
# Color
colored = "2.0"

# Completions
clap_complete_command = "0.6.1"

# Plugins
tower = { version = "0.5", features = ["util"] }
futures = "0.3.17"
Expand Down
5 changes: 5 additions & 0 deletions screenpipe-server/src/bin/screenpipe-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ async fn main() -> anyhow::Result<()> {

let pipe_manager = Arc::new(PipeManager::new(local_data_dir_clone.clone()));

if let Some(Command::Completions { shell }) = &cli.command {
cli.handle_completions(shell.clone())?;
return Ok(());
}
if let Some(command) = cli.command {
match command {
Command::Pipe { subcommand } => {
Expand Down Expand Up @@ -331,6 +335,7 @@ async fn main() -> anyhow::Result<()> {
.await?;
return Ok(());
}
Command::Completions { .. } => todo!(),
}
}

Expand Down
44 changes: 43 additions & 1 deletion screenpipe-server/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::path::PathBuf;
use std::io::{self, IsTerminal};

use clap::{Parser, Subcommand};
use clap::{Parser, Subcommand, CommandFactory};
use clap_complete_command::Shell;
use screenpipe_audio::{vad_engine::VadSensitivity, AudioTranscriptionEngine as CoreAudioTranscriptionEngine};
use screenpipe_vision::{custom_ocr::CustomOcrConfig, utils::OcrEngine as CoreOcrEngine};
use clap::ValueEnum;
use anyhow::Result;
use screenpipe_audio::vad_engine::VadEngineEnum;
use screenpipe_core::Language;

Expand Down Expand Up @@ -281,6 +284,39 @@ impl Cli {
}
Ok(unique_langs.into_iter().collect())
}

pub fn handle_completions(&self, shell: Shell) -> anyhow::Result<()> {
let mut cmd = Self::command();

// Check for terminal output
if io::stdout().is_terminal() {
let shell_instructions = match shell {
Shell::Bash =>
"For Bash:\n\
1. Generate: `screenpipe completions bash >> ~/.bashrc`\n\
2. Reload profile: `source ~/.bashrc`",
Shell::Zsh =>
"For Zsh:\n\
1. Generate: `screenpipe completions zsh >> ~/.zshrc`\n\
2. Reload profile: `source ~/.zshrc`",
Shell::Fish =>
"For Fish:\n\
1. Generate: `screenpipe completions fish > ~/.config/fish/completions/screenpipe.fish`",
Shell::PowerShell =>
"For PowerShell:\n\
1. Generate: `screenpipe completions powershell >> $PROFILE`\n\
2. Reload profile: `. $PROFILE`",
_ => anyhow::bail!("Unsupported shell type"),
};

anyhow::bail!("Completions must be redirected to a file.\n{}", shell_instructions);
}

// Generate completions
shell.generate(&mut cmd, &mut std::io::stdout());

Ok(())
}
}

#[derive(Subcommand)]
Expand Down Expand Up @@ -325,6 +361,12 @@ pub enum Command {
#[arg(long, default_value_t = false)]
enable_beta: bool,
},
/// Generate shell completions
Completions {
/// The shell to generate completions for
#[arg(value_enum)]
shell: Shell,
},
/// Run database migrations
Migrate,
}
Expand Down
Loading