Skip to content

Commit

Permalink
completions: Remove terminal configurability
Browse files Browse the repository at this point in the history
Summary: Don't need it

Reviewed By: stepancheg

Differential Revision: D63311600

fbshipit-source-id: 973105f6ee4ee86054f9b91afc7848a9295f15b0
  • Loading branch information
JakobDegen authored and facebook-github-bot committed Sep 25, 2024
1 parent e7332d0 commit 416eb5b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
7 changes: 3 additions & 4 deletions shed/completion_verify/src/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::path::Path;

use crate::extract_from_outputs;
use crate::runtime::BashRuntime;
use crate::runtime::Term;

pub(crate) fn run_bash(script: &str, input: &str, tempdir: &Path) -> io::Result<Vec<String>> {
let home = tempdir;
Expand All @@ -30,7 +29,7 @@ PS1='% '
let mut r = BashRuntime::with_home(home.to_owned())?;
r.register("buck2", script)?;

let one_tab = r.complete(&format!("{}\t", input), &Term::new())?;
let one_tab = r.complete(&format!("{}\t", input))?;
// complete_pty turns on echoing in this case to work around a bash bug, so strip that
let one_tab = one_tab.strip_prefix(input).unwrap_or(&one_tab).trim_start();

Expand All @@ -39,8 +38,8 @@ PS1='% '
input,
[
Ok(one_tab.to_owned()),
r.complete(&format!("{}\t\t", input), &Term::new()),
r.complete(&format!("{}\t\t\t", input), &Term::new()),
r.complete(&format!("{}\t\t", input)),
r.complete(&format!("{}\t\t\t", input)),
],
)
}
5 changes: 2 additions & 3 deletions shed/completion_verify/src/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::path::Path;

use crate::extract_from_outputs;
use crate::runtime::FishRuntime;
use crate::runtime::Term;

pub(crate) fn run_fish(script: &str, input: &str, tempdir: &Path) -> io::Result<Vec<String>> {
let home = tempdir;
Expand All @@ -23,8 +22,8 @@ pub(crate) fn run_fish(script: &str, input: &str, tempdir: &Path) -> io::Result<
extract_from_outputs(
input,
[
r.complete(&format!("{}\t", input), &Term::new()),
r.complete(&format!("{}\t\t", input), &Term::new()),
r.complete(&format!("{}\t", input)),
r.complete(&format!("{}\t\t", input)),
],
)
}
37 changes: 12 additions & 25 deletions shed/completion_verify/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,6 @@ use ptyprocess::PtyProcess;

use crate::Shell;

/// Terminal that shell's will run completions in
#[derive(Debug)]
pub(crate) struct Term {
width: u16,
height: u16,
}

impl Term {
pub(crate) fn new() -> Self {
Self {
width: 120,
height: 60,
}
}
}

/// Zsh runtime
#[derive(Debug)]
pub(crate) struct ZshRuntime {
Expand All @@ -76,12 +60,12 @@ impl ZshRuntime {
}

/// Get the output from typing `input` into the shell
pub(crate) fn complete(&mut self, input: &str, term: &Term) -> std::io::Result<String> {
pub(crate) fn complete(&mut self, input: &str) -> std::io::Result<String> {
let mut command = Shell::Zsh.find()?;
command.arg("--noglobalrcs");
command.env("TERM", "xterm").env("ZDOTDIR", &self.home);
let echo = false;
comptest(command, echo, input, term)
comptest(command, echo, input)
}
}

Expand Down Expand Up @@ -113,7 +97,7 @@ impl BashRuntime {
}

/// Get the output from typing `input` into the shell
pub(crate) fn complete(&mut self, input: &str, term: &Term) -> std::io::Result<String> {
pub(crate) fn complete(&mut self, input: &str) -> std::io::Result<String> {
let mut command = Shell::Bash.find()?;
let inputrc_path = self.home.join(".inputrc");
command
Expand All @@ -126,7 +110,7 @@ impl BashRuntime {
self.config.as_os_str(),
]);
let echo = !input.contains("\t\t");
comptest(command, echo, input, term)
comptest(command, echo, input)
}
}

Expand Down Expand Up @@ -171,30 +155,33 @@ end;
}

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

fn comptest(command: Command, echo: bool, input: &str, term: &Term) -> std::io::Result<String> {
const TERM_WIDTH: u16 = 120;
const TERM_HEIGHT: u16 = 60;

fn comptest(command: Command, echo: bool, input: &str) -> std::io::Result<String> {
#![allow(clippy::unwrap_used)] // some unwraps need extra investigation

// spawn a new process, pass it the input was.
//
// This triggers completion loading process which takes some time in shell so we should let it
// run for some time
let mut process = PtyProcess::spawn(command)?;
process.set_window_size(term.width, term.height)?;
process.set_window_size(TERM_WIDTH, TERM_HEIGHT)?;
// for some reason bash does not produce anything with echo disabled...
process.set_echo(echo, None)?;

let mut parser = vt100::Parser::new(term.height, term.width, 0);
let mut parser = vt100::Parser::new(TERM_HEIGHT, TERM_WIDTH, 0);

let mut stream = process.get_raw_handle()?;
// pass the completion input
Expand Down
5 changes: 2 additions & 3 deletions shed/completion_verify/src/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::io;
use std::path::Path;

use crate::extract_from_outputs;
use crate::runtime::Term;
use crate::runtime::ZshRuntime;

pub(crate) fn run_zsh(script: &str, input: &str, tempdir: &Path) -> io::Result<Vec<String>> {
Expand Down Expand Up @@ -61,8 +60,8 @@ _buck2 >/dev/null 2>/dev/null ; # Force the completion to be loaded
extract_from_outputs(
input,
[
r.complete(&format!("{}\t", input), &Term::new()),
r.complete(&format!("{}\t\t", input), &Term::new()),
r.complete(&format!("{}\t", input)),
r.complete(&format!("{}\t\t", input)),
],
)
}

0 comments on commit 416eb5b

Please sign in to comment.