mirror of
https://github.com/martinvonz/jj.git
synced 2025-02-01 00:50:57 +00:00
ui: Remove &mut
requirement for prompt functions
This commit is contained in:
parent
fad712811c
commit
2831459a95
2 changed files with 12 additions and 20 deletions
|
@ -17,7 +17,6 @@
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::Stdio;
|
use std::process::Stdio;
|
||||||
use std::sync::Mutex;
|
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use std::{error, iter};
|
use std::{error, iter};
|
||||||
|
|
||||||
|
@ -60,11 +59,11 @@ pub fn is_colocated_git_workspace(workspace: &Workspace, repo: &ReadonlyRepo) ->
|
||||||
git_workdir.canonicalize().ok().as_deref() == dot_git_path.parent()
|
git_workdir.canonicalize().ok().as_deref() == dot_git_path.parent()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn terminal_get_username(ui: &mut Ui, url: &str) -> Option<String> {
|
fn terminal_get_username(ui: &Ui, url: &str) -> Option<String> {
|
||||||
ui.prompt(&format!("Username for {url}")).ok()
|
ui.prompt(&format!("Username for {url}")).ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn terminal_get_pw(ui: &mut Ui, url: &str) -> Option<String> {
|
fn terminal_get_pw(ui: &Ui, url: &str) -> Option<String> {
|
||||||
ui.prompt_password(&format!("Passphrase for {url}: ")).ok()
|
ui.prompt_password(&format!("Passphrase for {url}: ")).ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,13 +137,9 @@ fn get_ssh_keys(_username: &str) -> Vec<PathBuf> {
|
||||||
paths
|
paths
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_remote_git_callbacks<T>(
|
pub fn with_remote_git_callbacks<T>(ui: &Ui, f: impl FnOnce(git::RemoteCallbacks<'_>) -> T) -> T {
|
||||||
ui: &mut Ui,
|
|
||||||
f: impl FnOnce(git::RemoteCallbacks<'_>) -> T,
|
|
||||||
) -> T {
|
|
||||||
let mut ui = Mutex::new(ui);
|
|
||||||
let mut callback = None;
|
let mut callback = None;
|
||||||
if let Some(mut output) = ui.get_mut().unwrap().progress_output() {
|
if let Some(mut output) = ui.progress_output() {
|
||||||
let mut progress = Progress::new(Instant::now());
|
let mut progress = Progress::new(Instant::now());
|
||||||
callback = Some(move |x: &git::Progress| {
|
callback = Some(move |x: &git::Progress| {
|
||||||
_ = progress.update(Instant::now(), x, &mut output);
|
_ = progress.update(Instant::now(), x, &mut output);
|
||||||
|
@ -156,14 +151,11 @@ pub fn with_remote_git_callbacks<T>(
|
||||||
.map(|x| x as &mut dyn FnMut(&git::Progress));
|
.map(|x| x as &mut dyn FnMut(&git::Progress));
|
||||||
let mut get_ssh_keys = get_ssh_keys; // Coerce to unit fn type
|
let mut get_ssh_keys = get_ssh_keys; // Coerce to unit fn type
|
||||||
callbacks.get_ssh_keys = Some(&mut get_ssh_keys);
|
callbacks.get_ssh_keys = Some(&mut get_ssh_keys);
|
||||||
let mut get_pw = |url: &str, _username: &str| {
|
let mut get_pw =
|
||||||
pinentry_get_pw(url).or_else(|| terminal_get_pw(*ui.lock().unwrap(), url))
|
|url: &str, _username: &str| pinentry_get_pw(url).or_else(|| terminal_get_pw(ui, url));
|
||||||
};
|
|
||||||
callbacks.get_password = Some(&mut get_pw);
|
callbacks.get_password = Some(&mut get_pw);
|
||||||
let mut get_user_pw = |url: &str| {
|
let mut get_user_pw =
|
||||||
let ui = &mut *ui.lock().unwrap();
|
|url: &str| Some((terminal_get_username(ui, url)?, terminal_get_pw(ui, url)?));
|
||||||
Some((terminal_get_username(ui, url)?, terminal_get_pw(ui, url)?))
|
|
||||||
};
|
|
||||||
callbacks.get_username_password = Some(&mut get_user_pw);
|
callbacks.get_username_password = Some(&mut get_user_pw);
|
||||||
f(callbacks)
|
f(callbacks)
|
||||||
}
|
}
|
||||||
|
|
|
@ -415,7 +415,7 @@ impl Ui {
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prompt(&mut self, prompt: &str) -> io::Result<String> {
|
pub fn prompt(&self, prompt: &str) -> io::Result<String> {
|
||||||
if !Self::can_prompt() {
|
if !Self::can_prompt() {
|
||||||
return Err(io::Error::new(
|
return Err(io::Error::new(
|
||||||
io::ErrorKind::Unsupported,
|
io::ErrorKind::Unsupported,
|
||||||
|
@ -441,7 +441,7 @@ impl Ui {
|
||||||
|
|
||||||
/// Repeat the given prompt until the input is one of the specified choices.
|
/// Repeat the given prompt until the input is one of the specified choices.
|
||||||
pub fn prompt_choice(
|
pub fn prompt_choice(
|
||||||
&mut self,
|
&self,
|
||||||
prompt: &str,
|
prompt: &str,
|
||||||
choices: &[impl AsRef<str>],
|
choices: &[impl AsRef<str>],
|
||||||
default: Option<&str>,
|
default: Option<&str>,
|
||||||
|
@ -470,7 +470,7 @@ impl Ui {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prompts for a yes-or-no response, with yes = true and no = false.
|
/// Prompts for a yes-or-no response, with yes = true and no = false.
|
||||||
pub fn prompt_yes_no(&mut self, prompt: &str, default: Option<bool>) -> io::Result<bool> {
|
pub fn prompt_yes_no(&self, prompt: &str, default: Option<bool>) -> io::Result<bool> {
|
||||||
let default_str = match &default {
|
let default_str = match &default {
|
||||||
Some(true) => "(Yn)",
|
Some(true) => "(Yn)",
|
||||||
Some(false) => "(yN)",
|
Some(false) => "(yN)",
|
||||||
|
@ -486,7 +486,7 @@ impl Ui {
|
||||||
Ok(choice.starts_with(['y', 'Y']))
|
Ok(choice.starts_with(['y', 'Y']))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prompt_password(&mut self, prompt: &str) -> io::Result<String> {
|
pub fn prompt_password(&self, prompt: &str) -> io::Result<String> {
|
||||||
if !io::stdout().is_terminal() {
|
if !io::stdout().is_terminal() {
|
||||||
return Err(io::Error::new(
|
return Err(io::Error::new(
|
||||||
io::ErrorKind::Unsupported,
|
io::ErrorKind::Unsupported,
|
||||||
|
|
Loading…
Reference in a new issue