Skip to content

Commit

Permalink
perf: ⚡ send update on up/down instead of enter
Browse files Browse the repository at this point in the history
  • Loading branch information
zaghaghi committed Mar 8, 2024
1 parent cd45d80 commit d0ca915
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
19 changes: 10 additions & 9 deletions src/panes/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ impl Pane for ApisPane {
fn update(&mut self, action: Action) -> Result<Option<Action>> {
match action {
Action::Down => {
let state = self.state.read().unwrap();
let mut state = self.state.write().unwrap();
let operations_len = state.operations_len();
if operations_len > 0 {
self.current_operation_index = self.current_operation_index.saturating_add(1) % operations_len;
}
state.active_operation_index = self.current_operation_index;
return Ok(Some(Action::Update));
},
Action::Up => {
let state = self.state.read().unwrap();
let mut state = self.state.write().unwrap();
let operations_len = state.operations_len();
if operations_len > 0 {
self.current_operation_index =
self.current_operation_index.saturating_add(operations_len - 1) % operations_len;
}
},
Action::Submit => {
let mut state = self.state.write().unwrap();
state.active_operation_index = self.current_operation_index;
return Ok(Some(Action::Update));
},
Action::Submit => {},
Action::Update => {
let state = self.state.read().unwrap();
self.current_operation_index = state.active_operation_index;
Expand All @@ -117,7 +117,7 @@ impl Pane for ApisPane {
}
Some(Line::from(vec![
Span::styled(
format!("{:7}", operation.1.as_str()),
format!(" {:7}", operation.1.as_str()),
Style::default().fg(Self::method_color(operation.1.as_str())),
),
Span::styled(
Expand All @@ -128,9 +128,10 @@ impl Pane for ApisPane {
});

let list = List::new(items)
.block(Block::default().title("List").borders(Borders::ALL))
.highlight_style(Style::default().add_modifier(Modifier::BOLD).bg(Color::DarkGray))
.direction(ListDirection::TopToBottom);
.block(Block::default().borders(Borders::ALL))
.highlight_symbol(symbols::scrollbar::HORIZONTAL.end)
.highlight_spacing(HighlightSpacing::Always)
.highlight_style(Style::default().add_modifier(Modifier::BOLD));
let mut list_state = ListState::default().with_selected(Some(self.current_operation_index));

frame.render_stateful_widget(list, area, &mut list_state);
Expand Down
18 changes: 8 additions & 10 deletions src/panes/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct RequestPane {
state: Arc<RwLock<State>>,
request_body: Option<RequestBody>,
request_schema: Option<Schema>,
request_schema_line_offset: Option<usize>,
request_schema_line_offset: usize,
request_schema_styles: Vec<Vec<(Style, String)>>,
highlighter_syntax_set: SyntaxSet,
highlighter_theme_set: ThemeSet,
Expand All @@ -42,7 +42,7 @@ impl RequestPane {
focused_border_style,
request_body: None,
request_schema: None,
request_schema_line_offset: None,
request_schema_line_offset: 0,
request_schema_styles: Vec::default(),
highlighter_syntax_set: SyntaxSet::load_defaults_newlines(),
highlighter_theme_set: ThemeSet::load_defaults(),
Expand Down Expand Up @@ -99,7 +99,7 @@ impl RequestPane {
let state = self.state.read().unwrap();
self.request_body = None;
self.request_schema_styles = vec![];
self.request_schema_line_offset = None;
self.request_schema_line_offset = 0;
if let Some((_path, _method, operation)) = state.active_operation() {
if let Some(oor) = &operation.request_body {
let resolved_oor = oor.resolve(&state.openapi_spec)?;
Expand Down Expand Up @@ -147,13 +147,11 @@ impl Pane for RequestPane {
self.init_request_schema()?;
},
Action::Down => {
self.request_schema_line_offset = match self.request_schema_line_offset {
Some(offset) => Some(offset.saturating_add(1).min(self.request_schema_styles.len() - 1)),
None => Some(0),
};
self.request_schema_line_offset =
self.request_schema_line_offset.saturating_add(1).min(self.request_schema_styles.len() - 1);
},
Action::Up => {
self.request_schema_line_offset = self.request_schema_line_offset.map(|offset| offset.saturating_sub(1));
self.request_schema_line_offset = self.request_schema_line_offset.saturating_sub(1);
},
Action::Submit => {},
_ => {},
Expand All @@ -179,7 +177,7 @@ impl Pane for RequestPane {
inner,
);

let inner_margin: Margin = Margin { horizontal: 2, vertical: 1 };
let inner_margin: Margin = Margin { horizontal: 1, vertical: 1 };
let mut inner = inner.inner(&inner_margin);
inner.height = inner.height.saturating_add(1);
let lines = self.request_schema_styles.iter().map(|items| {
Expand All @@ -192,7 +190,7 @@ impl Pane for RequestPane {
.collect::<Vec<_>>(),
);
});
let mut list_state = ListState::default().with_selected(self.request_schema_line_offset);
let mut list_state = ListState::default().with_selected(Some(self.request_schema_line_offset));

frame.render_stateful_widget(
List::new(lines)
Expand Down
56 changes: 33 additions & 23 deletions src/panes/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ impl TagsPane {
false => BorderType::Plain,
}
}

fn update_active_tag(&mut self) {
let mut state = self.state.write().unwrap();
if self.current_tag_index > 0 {
if let Some(tag) = state.openapi_spec.tags.get(self.current_tag_index - 1) {
state.active_tag_name = Some(tag.name.clone());
state.active_operation_index = 0;
}
} else {
state.active_tag_name = None;
state.active_operation_index = 0;
}
}
}
impl Pane for TagsPane {
fn init(&mut self) -> Result<()> {
Expand Down Expand Up @@ -68,32 +81,28 @@ impl Pane for TagsPane {
fn update(&mut self, action: Action) -> Result<Option<Action>> {
match action {
Action::Down => {
let state = self.state.read().unwrap();
let tags_list_len = state.openapi_spec.tags.len().saturating_add(1);
if tags_list_len > 0 {
self.current_tag_index = self.current_tag_index.saturating_add(1) % tags_list_len;
{
let state = self.state.read().unwrap();
let tags_list_len = state.openapi_spec.tags.len().saturating_add(1);
if tags_list_len > 0 {
self.current_tag_index = self.current_tag_index.saturating_add(1) % tags_list_len;
}
}
self.update_active_tag();
return Ok(Some(Action::Update));
},
Action::Up => {
let state = self.state.read().unwrap();
let tags_list_len = state.openapi_spec.tags.len().saturating_add(1);
if tags_list_len > 0 {
self.current_tag_index = self.current_tag_index.saturating_add(tags_list_len - 1) % tags_list_len;
}
},
Action::Submit => {
let mut state = self.state.write().unwrap();
if self.current_tag_index > 0 {
if let Some(tag) = state.openapi_spec.tags.get(self.current_tag_index - 1) {
state.active_tag_name = Some(tag.name.clone());
state.active_operation_index = 0;
{
let state = self.state.read().unwrap();
let tags_list_len = state.openapi_spec.tags.len().saturating_add(1);
if tags_list_len > 0 {
self.current_tag_index = self.current_tag_index.saturating_add(tags_list_len - 1) % tags_list_len;
}
} else {
state.active_tag_name = None;
state.active_operation_index = 0;
}
self.update_active_tag();
return Ok(Some(Action::Update));
},
Action::Submit => {},
_ => {},
}

Expand All @@ -106,15 +115,16 @@ impl Pane for TagsPane {
.openapi_spec
.tags
.iter()
.map(|tag| Line::from(vec![Span::styled(tag.name.as_str(), Style::default())]))
.map(|tag| Line::from(vec![Span::styled(format!(" {}", tag.name), Style::default())]))
.collect();

items.insert(0, Line::styled("[ALL]", Style::default()));
items.insert(0, Line::styled(" [ALL]", Style::default()));

let list = List::new(items)
.block(Block::default().borders(Borders::ALL))
.highlight_style(Style::default().add_modifier(Modifier::BOLD).bg(Color::DarkGray))
.direction(ListDirection::TopToBottom);
.highlight_symbol(symbols::scrollbar::HORIZONTAL.end)
.highlight_spacing(HighlightSpacing::Always)
.highlight_style(Style::default().add_modifier(Modifier::BOLD));
let mut list_state = ListState::default().with_selected(Some(self.current_tag_index));

frame.render_stateful_widget(list, area, &mut list_state);
Expand Down
Binary file modified static/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d0ca915

Please sign in to comment.