Skip to content

Commit

Permalink
feat: make things faster
Browse files Browse the repository at this point in the history
  • Loading branch information
pxseu committed Oct 10, 2023
1 parent 8b73a0e commit bbed289
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
18 changes: 3 additions & 15 deletions src/commands/webhooks/create.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use anyhow::{Context, Result};
use clap::Parser;
use hop::webhooks::types::{PossibleEvents, EVENT_CATEGORIES, EVENT_NAMES};
use hop::webhooks::types::{PossibleEvents, EVENT_NAMES};

use super::utils::string_to_event;
use crate::commands::webhooks::utils::get_formatted_events;
use crate::state::State;
use crate::utils::urlify;

Expand Down Expand Up @@ -30,23 +31,10 @@ pub async fn handle(options: Options, state: State) -> Result<()> {
let events = if !options.events.is_empty() {
options.events
} else {
let mut events = vec![];
let mut start_idx = 0usize;

for (name, end_idx) in EVENT_CATEGORIES {
let end_idx = end_idx as usize + start_idx;

for (_, event) in &EVENT_NAMES[start_idx..end_idx] {
events.push(format!("{name}: {event}"))
}

start_idx = end_idx;
}

let dialoguer_events = loop {
let test = dialoguer::MultiSelect::new()
.with_prompt("Select events")
.items(&events)
.items(&get_formatted_events()?)
.interact()?;

if !test.is_empty() {
Expand Down
21 changes: 4 additions & 17 deletions src/commands/webhooks/update.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::{Context, Result};
use clap::Parser;
use hop::webhooks::types::{PossibleEvents, EVENT_CATEGORIES, EVENT_NAMES};
use hop::webhooks::types::{PossibleEvents, EVENT_NAMES};

use super::utils::string_to_event;
use crate::commands::webhooks::utils::format_webhooks;
use crate::commands::webhooks::utils::{format_webhooks, get_formatted_events};
use crate::state::State;

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -51,23 +51,10 @@ pub async fn handle(options: Options, state: State) -> Result<()> {
let events = if !options.events.is_empty() {
options.events
} else {
let mut events = vec![];
let mut start_idx = 0usize;

for (name, end_idx) in EVENT_CATEGORIES {
let end_idx = end_idx as usize + start_idx;

for (_, event) in &EVENT_NAMES[start_idx..end_idx] {
events.push(format!("{name}: {event}"))
}

start_idx = end_idx;
}

let dialoguer_events = loop {
let test = dialoguer::MultiSelect::new()
.with_prompt("Select events")
.items(&events)
.items(&get_formatted_events()?)
.defaults(&EVENT_NAMES.map(|(event, _)| old.events.contains(&event)))
.interact()?;

Expand All @@ -79,7 +66,7 @@ pub async fn handle(options: Options, state: State) -> Result<()> {
EVENT_NAMES
.into_iter()
.enumerate()
.filter(|(idx, _)| dialoguer_events.contains(idx))
.filter(|(idx, _)| dialoguer_events.binary_search(idx).is_ok())
.map(|(_, (event, _))| event)
.collect()
};
Expand Down
20 changes: 19 additions & 1 deletion src/commands/webhooks/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::Write;

use anyhow::Result;
use hop::webhooks::types::{PossibleEvents, Webhook};
use hop::webhooks::types::{PossibleEvents, Webhook, EVENT_CATEGORIES, EVENT_NAMES};
use tabwriter::TabWriter;

pub fn format_webhooks(webhooks: &[Webhook], title: bool) -> Vec<String> {
Expand Down Expand Up @@ -32,3 +32,21 @@ pub fn format_webhooks(webhooks: &[Webhook], title: bool) -> Vec<String> {
pub fn string_to_event(string: &str) -> Result<PossibleEvents> {
serde_json::from_str(string).map_err(|e| e.into())
}

pub fn get_formatted_events() -> Result<Vec<String>> {
let mut events = vec![];

let mut start_idx = 0usize;

for (name, end_idx) in EVENT_CATEGORIES {
let end_idx = end_idx as usize + start_idx;

for (_, event) in &EVENT_NAMES[start_idx..end_idx] {
events.push(format!("{name}: {event}"));
}

start_idx = end_idx;
}

Ok(events)
}

0 comments on commit bbed289

Please sign in to comment.