Skip to content

Commit

Permalink
Add tab autocompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
tedbauer committed Apr 28, 2024
1 parent 28644ba commit 4d3c244
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
45 changes: 37 additions & 8 deletions Formula/up-path-gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
use std::env;

fn generate_completion_options(working_dir: &str, arg: &str) -> Vec<String> {
vec![
"apple".to_string(),
"banana".to_string(),
"cherry".to_string(),
]
fn generate_completion_options(working_dir: &str, arg: &str, comp_cword: usize) -> Vec<String> {
if comp_cword > 1 {
return vec![];
}

let directories = working_dir
.split('/')
.map(|s| String::from(s))
.collect::<Vec<String>>();

let arg = match arg.split(" ").nth(1) {
Some(arg) => arg,
None => return directories,
};

let mut result = Vec::new();
for (_, directory) in directories.iter().enumerate() {
if directory.starts_with(arg) {
result.push(directory.to_string());
}
}

if result.is_empty() {
directories
} else {
result
}
}

fn generate_path(working_dir: &str, arg: &str) -> Option<String> {
Expand Down Expand Up @@ -42,8 +63,16 @@ fn main() {
}

if &args[1] == "--complete" {
let completions = generate_completion_options(&current_dir_string, &args[2]);
let completion_string = completions.join("\n");
let completions = generate_completion_options(
&current_dir_string,
&args[3..]
.iter()
.map(|s| String::from(s))
.collect::<Vec<String>>()
.join(" "),
args[2].parse::<usize>().unwrap(),
);
let completion_string = completions.join(" ");
println!("{completion_string}");
return;
}
Expand Down
13 changes: 11 additions & 2 deletions Formula/up.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ class Up < Formula
version "0.1.0"

def install

# Check for CARGO_HOME environment variable
cargo_path = ENV["CARGO_HOME"] ? "#{ENV["CARGO_HOME"]}/bin/cargo" : nil

# Fallback: Check for Homebrew-managed Cargo (if applicable)
unless cargo_path
cargo_path = "#{HOMEBREW_PREFIX}/opt/rust/bin/cargo" if OS.mac?
end
potential_cargo_paths = [
"/opt/homebrew/bin", # macOS installation
"/home/#{ENV['USER']}/.cargo/bin", # Common user-level installation
"#{HOMEBREW_PREFIX}/opt/rust/bin" # Possible Homebrew Rust location
]

cargo_path = potential_cargo_paths.find { |path| File.exist? "#{path}/cargo" }
cargo_path = potential_cargo_paths.find { |path| File.exist? "#{path}/cargo" }
unless cargo_path
odie <<~EOS
Cargo (the Rust package manager) is required to install 'up'.
Expand All @@ -29,6 +36,8 @@ def install
lib.install "target/release/up-path-gen"
end

ENV["BINARY_PATH"] = "#{lib}/up-path-gen"

cd formula_path.dirname do
lib.install "up.sh"
end
Expand Down
8 changes: 4 additions & 4 deletions Formula/up.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash

binary_path="$(brew --prefix)/lib/up-path-gen"
# binary_path="$(brew --prefix)/lib/up-path-gen"

up_completion() {
completions=$($binary_path --complete $COMP_CWORD)
echo -e "$completions"
completions=$($BINARY_PATH --complete $COMP_CWORD $COMP_LINE)
COMPREPLY=($completions)
}

up() {
Expand All @@ -14,7 +14,7 @@ up() {
fi

target="$1"
full_command="$binary_path $target"
full_command="$BINARY_PATH $target"

target_path=$($full_command)
cd "$target_path"
Expand Down

0 comments on commit 4d3c244

Please sign in to comment.