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: Allow users to override the timeout #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
64 changes: 52 additions & 12 deletions crates/completest-pty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ PROMPT='%% '
&self.home
}

/// Set the timeout for completion
pub fn timeout(&mut self, timeout: Duration) {
self.timeout = timeout;
}

/// Register a completion script
pub fn register(&mut self, name: &str, content: &str) -> std::io::Result<()> {
let path = self.home.join(format!("zsh/_{name}"));
Expand All @@ -110,15 +115,20 @@ PROMPT='%% '
}

/// Get the output from typing `input` into the shell
pub fn complete(&mut self, input: &str, term: &Term) -> std::io::Result<String> {
pub fn complete(
&mut self,
input: &str,
term: &Term,
timeout: Duration,
) -> std::io::Result<String> {
let mut command = Command::new("zsh");
command.arg("--noglobalrcs");
command
.env("PATH", &self.path)
.env("TERM", "xterm")
.env("ZDOTDIR", &self.home);
let echo = false;
comptest(command, echo, input, term, self.timeout)
comptest(command, echo, input, term, timeout)
Comment on lines +122 to +131
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.timeout should be used

}
}

Expand All @@ -132,7 +142,7 @@ impl Runtime for ZshRuntime {
}

fn complete(&mut self, input: &str, term: &Term) -> std::io::Result<String> {
self.complete(input, term)
self.complete(input, term, self.timeout)
}
}

Expand Down Expand Up @@ -207,6 +217,11 @@ PS1='% '
&self.home
}

/// Set the timeout for completion
pub fn timeout(&mut self, timeout: Duration) {
self.timeout = timeout;
}

/// Register a completion script
pub fn register(&mut self, _name: &str, content: &str) -> std::io::Result<()> {
let mut file = std::fs::OpenOptions::new()
Expand All @@ -217,7 +232,12 @@ PS1='% '
}

/// Get the output from typing `input` into the shell
pub fn complete(&mut self, input: &str, term: &Term) -> std::io::Result<String> {
pub fn complete(
&mut self,
input: &str,
term: &Term,
timeout: Duration,
) -> std::io::Result<String> {
let mut command = Command::new("bash");
let inputrc_path = self.home.join(".inputrc");
command
Expand All @@ -226,7 +246,7 @@ PS1='% '
.env("INPUTRC", &inputrc_path)
.args([OsStr::new("--rcfile"), self.config.as_os_str()]);
let echo = !input.contains("\t\t");
comptest(command, echo, input, term, self.timeout)
comptest(command, echo, input, term, timeout)
}
}

Expand All @@ -240,7 +260,7 @@ impl Runtime for BashRuntime {
}

fn complete(&mut self, input: &str, term: &Term) -> std::io::Result<String> {
self.complete(input, term)
self.complete(input, term, self.timeout)
}
}

Expand Down Expand Up @@ -312,6 +332,11 @@ end;
&self.home
}

/// Set the timeout for completion
pub fn timeout(&mut self, timeout: Duration) {
self.timeout = timeout;
}

/// Register a completion script
pub fn register(&mut self, name: &str, content: &str) -> std::io::Result<()> {
let path = self.home.join(format!("fish/completions/{name}.fish"));
Expand All @@ -320,15 +345,20 @@ end;
}

/// Get the output from typing `input` into the shell
pub fn complete(&mut self, input: &str, term: &Term) -> std::io::Result<String> {
pub fn complete(
&mut self,
input: &str,
term: &Term,
timeout: Duration,
) -> std::io::Result<String> {
let mut command = Command::new("fish");
command
.env("PATH", &self.path)
// fish requires TERM to be set.
.env("TERM", "xterm")
.env("XDG_CONFIG_HOME", &self.home);
let echo = false;
comptest(command, echo, input, term, self.timeout)
comptest(command, echo, input, term, timeout)
}
}

Expand All @@ -342,7 +372,7 @@ impl Runtime for FishRuntime {
}

fn complete(&mut self, input: &str, term: &Term) -> std::io::Result<String> {
self.complete(input, term)
self.complete(input, term, self.timeout)
}
}

Expand Down Expand Up @@ -412,6 +442,11 @@ set edit:prompt = (constantly \"% \")
&self.home
}

/// Set the timeout for completion
pub fn timeout(&mut self, timeout: Duration) {
self.timeout = timeout;
}

/// Register a completion script
pub fn register(&mut self, _name: &str, content: &str) -> std::io::Result<()> {
let mut file = std::fs::OpenOptions::new()
Expand All @@ -422,13 +457,18 @@ set edit:prompt = (constantly \"% \")
}

/// Get the output from typing `input` into the shell
pub fn complete(&mut self, input: &str, term: &Term) -> std::io::Result<String> {
pub fn complete(
&mut self,
input: &str,
term: &Term,
timeout: Duration,
) -> std::io::Result<String> {
let mut command = Command::new("elvish");
command
.env("PATH", &self.path)
.env("XDG_CONFIG_HOME", &self.home);
let echo = false;
comptest(command, echo, input, term, self.timeout)
comptest(command, echo, input, term, timeout)
}
}

Expand All @@ -442,7 +482,7 @@ impl Runtime for ElvishRuntime {
}

fn complete(&mut self, input: &str, term: &Term) -> std::io::Result<String> {
self.complete(input, term)
self.complete(input, term, self.timeout)
}
}

Expand Down
Loading