use std::ffi::OsString;
use std::path::PathBuf;
use super::redaction::{redact_argv, redact_text};
use crate::command::preview_argv;
use crate::{ExecutionMode, GlobalOptions, JjCommandSpec};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CommandIdentity {
pub argv: Vec<OsString>,
pub spec_preview: String,
pub command_family: CommandFamily,
pub title: String,
}
impl CommandIdentity {
#[must_use]
pub fn from_spec(spec: &JjCommandSpec) -> Self {
Self {
argv: redact_argv(spec.process_argv()),
spec_preview: redact_text(&spec.preview()).0,
command_family: CommandFamily::from_spec(spec),
title: redact_text(spec.title()).0,
}
}
#[must_use]
pub fn process_preview(&self) -> String {
preview_argv(&self.argv)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum CommandFamily {
JjDefault,
JjLog,
JjDiff,
JjShow,
JjStatus,
JjDescribe,
JjNew,
JjEdit,
JjEvolog,
JjWorkspace,
JjOperation,
UserJjCommand,
ExternalCommand,
Other(String),
}
impl CommandFamily {
fn from_spec(spec: &JjCommandSpec) -> Self {
if spec.mode() == ExecutionMode::CommandMode {
return Self::UserJjCommand;
}
let Some(command) = spec.argv().first() else {
return Self::JjDefault;
};
match command.to_string_lossy().as_ref() {
"log" => Self::JjLog,
"diff" => Self::JjDiff,
"show" => Self::JjShow,
"status" => Self::JjStatus,
"describe" => Self::JjDescribe,
"new" => Self::JjNew,
"edit" => Self::JjEdit,
"evolog" => Self::JjEvolog,
"workspace" => Self::JjWorkspace,
"op" => Self::JjOperation,
other => Self::Other(other.to_owned()),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CommandSource {
pub view: SourceView,
pub action: SourceAction,
pub key: Option<String>,
}
impl CommandSource {
#[must_use]
pub const fn new(view: SourceView, action: SourceAction) -> Self {
Self {
view,
action,
key: None,
}
}
#[must_use]
pub fn with_key(mut self, key: impl Into<String>) -> Self {
self.key = Some(key.into());
self
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum SourceView {
Log,
Diff,
Show,
Status,
Evolog,
Workspaces,
WorkspaceLog,
WorkspaceStatus,
WorkspaceDiff,
CommandHistory,
OperationLog,
OperationShow,
OperationDiff,
Other(String),
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum SourceAction {
InitialLoad,
Refresh,
OpenDiff,
OpenShow,
OpenStatus,
OpenEvolog,
DescribeRevision,
AbandonRevision,
NewRevision,
EditRevision,
WorkspaceList,
WorkspaceStatus,
WorkspaceLog,
WorkspaceDiff,
WorkspaceUpdateStale,
OperationLog,
OperationShow,
OperationDiff,
Undo,
Redo,
UserJjCommand,
Other(String),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CommandExecutionContext {
pub cwd: Option<PathBuf>,
pub repository: Option<PathBuf>,
pub global_options: GlobalOptionsSnapshot,
}
impl CommandExecutionContext {
#[must_use]
pub fn from_spec(spec: &JjCommandSpec) -> Self {
Self {
cwd: spec.cwd().map(PathBuf::from),
repository: spec.repository().map(PathBuf::from),
global_options: GlobalOptionsSnapshot::from_global_options(spec.global_options()),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GlobalOptionsSnapshot {
pub argv: Vec<OsString>,
}
impl GlobalOptionsSnapshot {
#[must_use]
pub fn from_global_options(options: &GlobalOptions) -> Self {
Self {
argv: redact_argv(options.argv()),
}
}
}