2025-08-07 09:27:38 -07:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
use codex_core::config::set_project_trusted;
|
2025-08-22 13:54:51 -07:00
|
|
|
use codex_core::git_info::resolve_root_git_project_for_trust;
|
2025-08-07 09:27:38 -07:00
|
|
|
use crossterm::event::KeyCode;
|
|
|
|
|
use crossterm::event::KeyEvent;
|
2025-10-04 17:41:40 -07:00
|
|
|
use crossterm::event::KeyEventKind;
|
2025-08-07 09:27:38 -07:00
|
|
|
use ratatui::buffer::Buffer;
|
|
|
|
|
use ratatui::layout::Rect;
|
|
|
|
|
use ratatui::style::Stylize;
|
|
|
|
|
use ratatui::text::Line;
|
|
|
|
|
use ratatui::widgets::Paragraph;
|
|
|
|
|
use ratatui::widgets::WidgetRef;
|
|
|
|
|
use ratatui::widgets::Wrap;
|
|
|
|
|
|
2025-10-09 10:39:45 -07:00
|
|
|
use crate::key_hint;
|
2025-08-07 09:27:38 -07:00
|
|
|
use crate::onboarding::onboarding_screen::KeyboardHandler;
|
|
|
|
|
use crate::onboarding::onboarding_screen::StepStateProvider;
|
2025-10-09 10:39:45 -07:00
|
|
|
use crate::render::Insets;
|
|
|
|
|
use crate::render::renderable::ColumnRenderable;
|
|
|
|
|
use crate::render::renderable::Renderable;
|
|
|
|
|
use crate::render::renderable::RenderableExt as _;
|
2025-10-15 16:11:20 -07:00
|
|
|
use crate::selection_list::selection_option_row;
|
2025-08-07 09:27:38 -07:00
|
|
|
|
|
|
|
|
use super::onboarding_screen::StepState;
|
|
|
|
|
pub(crate) struct TrustDirectoryWidget {
|
|
|
|
|
pub codex_home: PathBuf,
|
|
|
|
|
pub cwd: PathBuf,
|
|
|
|
|
pub is_git_repo: bool,
|
|
|
|
|
pub selection: Option<TrustDirectorySelection>,
|
|
|
|
|
pub highlighted: TrustDirectorySelection,
|
|
|
|
|
pub error: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
2025-08-20 13:47:24 -07:00
|
|
|
pub enum TrustDirectorySelection {
|
2025-08-07 09:27:38 -07:00
|
|
|
Trust,
|
|
|
|
|
DontTrust,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WidgetRef for &TrustDirectoryWidget {
|
|
|
|
|
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
|
2025-10-09 10:39:45 -07:00
|
|
|
let mut column = ColumnRenderable::new();
|
2025-08-07 09:27:38 -07:00
|
|
|
|
2025-10-09 10:39:45 -07:00
|
|
|
column.push(Line::from(vec![
|
|
|
|
|
"> ".into(),
|
|
|
|
|
"You are running Codex in ".bold(),
|
|
|
|
|
self.cwd.to_string_lossy().to_string().into(),
|
|
|
|
|
]));
|
|
|
|
|
column.push("");
|
|
|
|
|
|
|
|
|
|
let guidance = if self.is_git_repo {
|
|
|
|
|
"Since this folder is version controlled, you may wish to allow Codex to work in this folder without asking for approval."
|
2025-08-07 09:27:38 -07:00
|
|
|
} else {
|
2025-10-09 10:39:45 -07:00
|
|
|
"Since this folder is not version controlled, we recommend requiring approval of all edits and commands."
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
column.push(
|
|
|
|
|
Paragraph::new(guidance.to_string())
|
|
|
|
|
.wrap(Wrap { trim: true })
|
|
|
|
|
.inset(Insets::tlbr(0, 2, 0, 0)),
|
|
|
|
|
);
|
|
|
|
|
column.push("");
|
2025-08-07 09:27:38 -07:00
|
|
|
|
2025-10-09 10:39:45 -07:00
|
|
|
let mut options: Vec<(&str, TrustDirectorySelection)> = Vec::new();
|
2025-08-07 09:27:38 -07:00
|
|
|
if self.is_git_repo {
|
2025-10-09 10:39:45 -07:00
|
|
|
options.push((
|
2025-08-07 09:27:38 -07:00
|
|
|
"Yes, allow Codex to work in this folder without asking for approval",
|
2025-10-09 10:39:45 -07:00
|
|
|
TrustDirectorySelection::Trust,
|
2025-08-07 09:27:38 -07:00
|
|
|
));
|
2025-10-09 10:39:45 -07:00
|
|
|
options.push((
|
2025-08-07 09:27:38 -07:00
|
|
|
"No, ask me to approve edits and commands",
|
2025-10-09 10:39:45 -07:00
|
|
|
TrustDirectorySelection::DontTrust,
|
2025-08-07 09:27:38 -07:00
|
|
|
));
|
|
|
|
|
} else {
|
2025-10-09 10:39:45 -07:00
|
|
|
options.push((
|
2025-08-07 09:27:38 -07:00
|
|
|
"Allow Codex to work in this folder without asking for approval",
|
2025-10-09 10:39:45 -07:00
|
|
|
TrustDirectorySelection::Trust,
|
2025-08-07 09:27:38 -07:00
|
|
|
));
|
2025-10-09 10:39:45 -07:00
|
|
|
options.push((
|
2025-08-07 09:27:38 -07:00
|
|
|
"Require approval of edits and commands",
|
2025-10-09 10:39:45 -07:00
|
|
|
TrustDirectorySelection::DontTrust,
|
2025-08-07 09:27:38 -07:00
|
|
|
));
|
|
|
|
|
}
|
2025-10-09 10:39:45 -07:00
|
|
|
|
|
|
|
|
for (idx, (text, selection)) in options.iter().enumerate() {
|
2025-10-15 16:11:20 -07:00
|
|
|
column.push(selection_option_row(
|
2025-10-09 10:39:45 -07:00
|
|
|
idx,
|
|
|
|
|
text.to_string(),
|
|
|
|
|
self.highlighted == *selection,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
column.push("");
|
|
|
|
|
|
2025-08-07 09:27:38 -07:00
|
|
|
if let Some(error) = &self.error {
|
2025-10-09 10:39:45 -07:00
|
|
|
column.push(
|
|
|
|
|
Paragraph::new(error.to_string())
|
|
|
|
|
.red()
|
|
|
|
|
.wrap(Wrap { trim: true })
|
|
|
|
|
.inset(Insets::tlbr(0, 2, 0, 0)),
|
|
|
|
|
);
|
|
|
|
|
column.push("");
|
2025-08-07 09:27:38 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-09 10:39:45 -07:00
|
|
|
column.push(
|
|
|
|
|
Line::from(vec![
|
|
|
|
|
"Press ".dim(),
|
|
|
|
|
key_hint::plain(KeyCode::Enter).into(),
|
|
|
|
|
" to continue".dim(),
|
|
|
|
|
])
|
|
|
|
|
.inset(Insets::tlbr(0, 2, 0, 0)),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
column.render(area, buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-07 09:27:38 -07:00
|
|
|
impl KeyboardHandler for TrustDirectoryWidget {
|
|
|
|
|
fn handle_key_event(&mut self, key_event: KeyEvent) {
|
2025-10-04 17:41:40 -07:00
|
|
|
if key_event.kind == KeyEventKind::Release {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-07 09:27:38 -07:00
|
|
|
match key_event.code {
|
|
|
|
|
KeyCode::Up | KeyCode::Char('k') => {
|
|
|
|
|
self.highlighted = TrustDirectorySelection::Trust;
|
|
|
|
|
}
|
|
|
|
|
KeyCode::Down | KeyCode::Char('j') => {
|
|
|
|
|
self.highlighted = TrustDirectorySelection::DontTrust;
|
|
|
|
|
}
|
2025-10-09 10:39:45 -07:00
|
|
|
KeyCode::Char('1') | KeyCode::Char('y') => self.handle_trust(),
|
|
|
|
|
KeyCode::Char('2') | KeyCode::Char('n') => self.handle_dont_trust(),
|
2025-08-07 09:27:38 -07:00
|
|
|
KeyCode::Enter => match self.highlighted {
|
|
|
|
|
TrustDirectorySelection::Trust => self.handle_trust(),
|
|
|
|
|
TrustDirectorySelection::DontTrust => self.handle_dont_trust(),
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl StepStateProvider for TrustDirectoryWidget {
|
|
|
|
|
fn get_step_state(&self) -> StepState {
|
|
|
|
|
match self.selection {
|
|
|
|
|
Some(_) => StepState::Complete,
|
|
|
|
|
None => StepState::InProgress,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TrustDirectoryWidget {
|
|
|
|
|
fn handle_trust(&mut self) {
|
2025-08-22 13:54:51 -07:00
|
|
|
let target =
|
|
|
|
|
resolve_root_git_project_for_trust(&self.cwd).unwrap_or_else(|| self.cwd.clone());
|
|
|
|
|
if let Err(e) = set_project_trusted(&self.codex_home, &target) {
|
2025-08-07 09:27:38 -07:00
|
|
|
tracing::error!("Failed to set project trusted: {e:?}");
|
2025-08-22 13:54:51 -07:00
|
|
|
self.error = Some(format!("Failed to set trust for {}: {e}", target.display()));
|
2025-08-07 09:27:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.selection = Some(TrustDirectorySelection::Trust);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle_dont_trust(&mut self) {
|
|
|
|
|
self.highlighted = TrustDirectorySelection::DontTrust;
|
|
|
|
|
self.selection = Some(TrustDirectorySelection::DontTrust);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-04 17:41:40 -07:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2025-10-09 10:39:45 -07:00
|
|
|
use crate::test_backend::VT100Backend;
|
|
|
|
|
|
2025-10-04 17:41:40 -07:00
|
|
|
use super::*;
|
|
|
|
|
use crossterm::event::KeyCode;
|
|
|
|
|
use crossterm::event::KeyEvent;
|
|
|
|
|
use crossterm::event::KeyEventKind;
|
|
|
|
|
use crossterm::event::KeyModifiers;
|
|
|
|
|
use pretty_assertions::assert_eq;
|
2025-10-09 10:39:45 -07:00
|
|
|
use ratatui::Terminal;
|
|
|
|
|
|
2025-10-04 17:41:40 -07:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn release_event_does_not_change_selection() {
|
|
|
|
|
let mut widget = TrustDirectoryWidget {
|
|
|
|
|
codex_home: PathBuf::from("."),
|
|
|
|
|
cwd: PathBuf::from("."),
|
|
|
|
|
is_git_repo: false,
|
|
|
|
|
selection: None,
|
|
|
|
|
highlighted: TrustDirectorySelection::DontTrust,
|
|
|
|
|
error: None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let release = KeyEvent {
|
|
|
|
|
kind: KeyEventKind::Release,
|
|
|
|
|
..KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)
|
|
|
|
|
};
|
|
|
|
|
widget.handle_key_event(release);
|
|
|
|
|
assert_eq!(widget.selection, None);
|
|
|
|
|
|
|
|
|
|
let press = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE);
|
|
|
|
|
widget.handle_key_event(press);
|
|
|
|
|
assert_eq!(widget.selection, Some(TrustDirectorySelection::DontTrust));
|
|
|
|
|
}
|
2025-10-09 10:39:45 -07:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn renders_snapshot_for_git_repo() {
|
|
|
|
|
let widget = TrustDirectoryWidget {
|
|
|
|
|
codex_home: PathBuf::from("."),
|
|
|
|
|
cwd: PathBuf::from("/workspace/project"),
|
|
|
|
|
is_git_repo: true,
|
|
|
|
|
selection: None,
|
|
|
|
|
highlighted: TrustDirectorySelection::Trust,
|
|
|
|
|
error: None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut terminal = Terminal::new(VT100Backend::new(70, 14)).expect("terminal");
|
|
|
|
|
terminal
|
|
|
|
|
.draw(|f| (&widget).render_ref(f.area(), f.buffer_mut()))
|
|
|
|
|
.expect("draw");
|
|
|
|
|
|
|
|
|
|
insta::assert_snapshot!(terminal.backend());
|
|
|
|
|
}
|
2025-10-04 17:41:40 -07:00
|
|
|
}
|