Skip to content
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
41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ clap = { version = "4.5", features = ["derive"] }

tokio-rayon = "2.1.0"
rand = "0.8"
owo-colors = "4.1"
owo-colors = { version = "4.1", features = ["supports-colors"] }
supports-color = "3"
itertools = "0.14.0"


Expand Down
40 changes: 34 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::embeddings::Embedding;
use anyhow::Result;
use clap::Parser;
use owo_colors::OwoColorize;
use supports_color::Stream;
use rand::prelude::*;
use rand::rngs::StdRng;
use std::path::Path;
Expand Down Expand Up @@ -105,6 +106,13 @@ fn sample_random_chunks(
}

/// Fast semantic code search powered by AI embeddings and turbopuffer
#[derive(clap::ValueEnum, Clone, Debug)]
enum ColorChoice {
Auto,
Always,
Never,
}

#[derive(Parser)]
#[command(name = "tg")]
#[command(version = "0.1.0")]
Expand Down Expand Up @@ -173,12 +181,26 @@ struct Cli {
/// Show distance scores in output (lower is better)
#[arg(long)]
scores: bool,

/// When to use colors: auto, always, never (like ripgrep)
#[arg(long, value_enum, default_value_t = ColorChoice::Auto)]
color: ColorChoice,
}

#[tokio::main]
async fn main() {
let cli = Cli::parse();
turbogrep::set_verbose(cli.verbose);
// Determine effective color choice similar to ripgrep
// Priority: --color overrides, then NO_COLOR env disables, then auto based on terminal support
let mut use_color = match cli.color {
ColorChoice::Always => true,
ColorChoice::Never => false,
ColorChoice::Auto => supports_color::on(Stream::Stdout).is_some(),
};
if std::env::var_os("NO_COLOR").is_some() {
use_color = false;
}

if let Err(e) = config::load_or_init_settings().await {
eprintln!("<(°!°)> Error loading settings: {}", e);
Expand Down Expand Up @@ -214,16 +236,19 @@ async fn main() {

for chunk in sampled_chunks {
if let Some(content) = &chunk.content {
println!(
"{}",
format!(
if use_color {
let path = format!("{}", chunk.path.magenta().bold());
let start = format!("{}", chunk.start_line.green());
let end = format!("{}", chunk.end_line.green());
println!("{}:{}:{}", path, start, end);
} else {
println!(
"{path}:{start_line}:{end_line}",
path = chunk.path,
start_line = chunk.start_line,
end_line = chunk.end_line
)
.bright_cyan()
);
);
}
println!("{}", content);
println!(); // Empty line separator
}
Expand Down Expand Up @@ -271,6 +296,7 @@ async fn main() {
cli.max_count,
cli.embedding_concurrency,
cli.scores,
use_color,
)
.await
{
Expand All @@ -288,6 +314,7 @@ async fn main() {
cli.max_count,
cli.embedding_concurrency,
cli.scores,
use_color,
)
.await
{
Expand All @@ -304,6 +331,7 @@ async fn main() {
cli.max_count,
cli.embedding_concurrency,
cli.scores,
use_color,
)
.await
{
Expand Down
37 changes: 34 additions & 3 deletions src/search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{chunker, embeddings, project, sync, turbopuffer, vprintln};
use anyhow::Result;
use embeddings::Embedding;
use owo_colors::OwoColorize;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
Expand Down Expand Up @@ -51,6 +52,7 @@ fn chunks_to_ripgrep_format(
chunks: Vec<chunker::Chunk>,
root_dir: &str,
show_scores: bool,
use_color: bool,
) -> String {
chunks
.into_iter()
Expand All @@ -71,13 +73,38 @@ fn chunks_to_ripgrep_format(

if show_scores {
if let Some(distance) = chunk.distance {
if use_color {
format!(
"{}:{}:{}:{}",
format!("{}", relative_path).magenta().bold(),
format!("{}", chunk.start_line).green(),
format!("{:.4}", distance).yellow(),
preview
)
} else {
format!(
"{}:{}:{:.4}:{}",
relative_path, chunk.start_line, distance, preview
)
}
} else if use_color {
format!(
"{}:{}:{:.4}:{}",
relative_path, chunk.start_line, distance, preview
"{}:{}:{}:{}",
format!("{}", relative_path).magenta().bold(),
format!("{}", chunk.start_line).green(),
"n/a".yellow(),
preview
)
} else {
format!("{}:{}:n/a:{}", relative_path, chunk.start_line, preview)
}
} else if use_color {
format!(
"{}:{}:{}",
format!("{}", relative_path).magenta().bold(),
format!("{}", chunk.start_line).green(),
preview
)
} else {
format!("{}:{}:{}", relative_path, chunk.start_line, preview)
}
Expand All @@ -92,6 +119,7 @@ pub async fn search(
max_count: usize,
embedding_concurrency: Option<usize>,
show_scores: bool,
use_color: bool,
) -> Result<String, SearchError> {
let (namespace, root_dir) = project::namespace_and_dir(directory)
.map_err(|e| SearchError::NamespaceError(e.to_string()))?;
Expand Down Expand Up @@ -147,6 +175,7 @@ pub async fn search(
results_with_content,
&root_dir,
show_scores,
use_color,
))
}

Expand All @@ -159,6 +188,7 @@ pub async fn speculate_search(
max_count: usize,
embedding_concurrency: Option<usize>,
show_scores: bool,
use_color: bool,
) -> Result<String, SearchError> {
loop {
let mut search_task = tokio::spawn({
Expand All @@ -171,6 +201,7 @@ pub async fn speculate_search(
max_count,
embedding_concurrency,
show_scores,
use_color,
)
.await
}
Expand Down Expand Up @@ -275,7 +306,7 @@ mod tests {
distance: None,
}];

let result = chunks_to_ripgrep_format(chunks, "/project", false);
let result = chunks_to_ripgrep_format(chunks, "/project", false, false);
let expected = "src/main.rs:10:fn main() {";

assert_eq!(result, expected);
Expand Down