Skip to content

Commit

Permalink
replace with output selector
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Oct 16, 2023
1 parent cdbfd07 commit 0c7828f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub async fn account_prompt_internal(
}
AccountCommand::NewAddress => new_address_command(account).await,
AccountCommand::NodeInfo => node_info_command(account).await,
AccountCommand::Output { output_id } => output_command(account, output_id).await,
AccountCommand::Output { selector } => output_command(account, selector).await,
AccountCommand::Outputs => outputs_command(account).await,
AccountCommand::Send {
address,
Expand Down
35 changes: 31 additions & 4 deletions cli/src/command/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ pub enum AccountCommand {
NodeInfo,
/// Display an output.
Output {
/// Output ID to be displayed.
output_id: String,
/// Selector for output.
/// Either by ID (e.g. 0xbce525324af12eda02bf7927e92cea3a8e8322d0f41966271443e6c3b245a4400000) or index.
selector: OutputSelector,
},
/// List all outputs.
Outputs,
Expand Down Expand Up @@ -281,6 +282,25 @@ impl FromStr for TransactionSelector {
}
}

/// Select by output ID or list index
#[derive(Debug, Copy, Clone)]
pub enum OutputSelector {
Id(OutputId),
Index(usize),
}

impl FromStr for OutputSelector {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(if let Ok(index) = s.parse() {
Self::Index(index)
} else {
Self::Id(s.parse()?)
})
}
}

/// `addresses` command
pub async fn addresses_command(account: &Account) -> Result<(), Error> {
let addresses = account.addresses().await?;
Expand Down Expand Up @@ -644,8 +664,15 @@ pub async fn node_info_command(account: &Account) -> Result<(), Error> {
}

/// `output` command
pub async fn output_command(account: &Account, output_id: String) -> Result<(), Error> {
let output = account.get_output(&OutputId::from_str(&output_id)?).await;
pub async fn output_command(account: &Account, selector: OutputSelector) -> Result<(), Error> {
let output = match selector {
OutputSelector::Id(id) => account.get_output(&id).await,
OutputSelector::Index(index) => {
let mut outputs = account.outputs(None).await?;
outputs.sort_by(|a, b| a.output_id.cmp(&b.output_id));
outputs.into_iter().nth(index)
}
};

if let Some(output) = output {
println_log_info!("{output:#?}");
Expand Down

0 comments on commit 0c7828f

Please sign in to comment.