Skip to content

Commit

Permalink
Optionize for active_item_style/inactive_item_style of listbox
Browse files Browse the repository at this point in the history
  • Loading branch information
ynqa committed May 12, 2024
1 parent 69bb69c commit 9a88915
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
15 changes: 9 additions & 6 deletions promkit/src/core/listbox/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub struct State {
pub cursor: String,

/// Style for the selected line.
pub active_item_style: ContentStyle,
pub active_item_style: Option<ContentStyle>,
/// Style for un-selected lines.
pub inactive_item_style: ContentStyle,
pub inactive_item_style: Option<ContentStyle>,

/// Number of lines available for rendering.
pub lines: Option<usize>,
Expand All @@ -38,15 +38,18 @@ impl PaneFactory for State {
.map(|(i, item)| {
if i == self.listbox.position() {
StyledGraphemes::from_iter([&StyledGraphemes::from(self.cursor.clone()), item])
.apply_style(self.active_item_style)
} else {
StyledGraphemes::from_iter([
let init = StyledGraphemes::from_iter([
&StyledGraphemes::from(
" ".repeat(StyledGraphemes::from(self.cursor.clone()).widths()),
),
item,
])
.apply_style(self.inactive_item_style)
]);
if let Some(style) = &self.active_item_style {
init.apply_style(*style)
} else {
init
}
}
})
.fold((vec![], 0), |(mut acc, pos), item| {
Expand Down
8 changes: 4 additions & 4 deletions promkit/src/preset/listbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl Listbox {
listbox_state: listbox::State {
listbox: listbox::Listbox::from_iter(items),
cursor: String::from("❯ "),
active_item_style: StyleBuilder::new().fgc(Color::DarkCyan).build(),
inactive_item_style: StyleBuilder::new().build(),
active_item_style: Some(StyleBuilder::new().fgc(Color::DarkCyan).build()),
inactive_item_style: Some(StyleBuilder::new().build()),
lines: Default::default(),
},
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
Expand All @@ -68,13 +68,13 @@ impl Listbox {

/// Sets the style for active (currently selected) items.
pub fn active_item_style(mut self, style: ContentStyle) -> Self {
self.listbox_state.active_item_style = style;
self.listbox_state.active_item_style = Some(style);
self
}

/// Sets the style for inactive (not currently selected) items.
pub fn inactive_item_style(mut self, style: ContentStyle) -> Self {
self.listbox_state.inactive_item_style = style;
self.listbox_state.inactive_item_style = Some(style);
self
}

Expand Down
8 changes: 4 additions & 4 deletions promkit/src/preset/query_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl QuerySelector {
listbox_state: listbox::State {
listbox: Listbox::from_iter(items),
cursor: String::from("❯ "),
active_item_style: StyleBuilder::new().fgc(Color::DarkCyan).build(),
inactive_item_style: StyleBuilder::new().build(),
active_item_style: Some(StyleBuilder::new().fgc(Color::DarkCyan).build()),
inactive_item_style: Some(StyleBuilder::new().build()),
lines: Default::default(),
},
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
Expand Down Expand Up @@ -133,13 +133,13 @@ impl QuerySelector {

/// Sets the style for active (currently selected) items in the list box component.
pub fn active_item_style(mut self, style: ContentStyle) -> Self {
self.listbox_state.active_item_style = style;
self.listbox_state.active_item_style = Some(style);
self
}

/// Sets the style for inactive (not currently selected) items in the list box component.
pub fn inactive_item_style(mut self, style: ContentStyle) -> Self {
self.listbox_state.inactive_item_style = style;
self.listbox_state.inactive_item_style = Some(style);
self
}

Expand Down
12 changes: 7 additions & 5 deletions promkit/src/preset/readline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ impl Default for Readline {
suggest_state: listbox::State {
listbox: Listbox::from_iter(Vec::<String>::new()),
cursor: String::from("❯ "),
active_item_style: StyleBuilder::new()
.fgc(Color::DarkGrey)
.bgc(Color::DarkYellow)
.build(),
inactive_item_style: StyleBuilder::new().fgc(Color::DarkGrey).build(),
active_item_style: Some(
StyleBuilder::new()
.fgc(Color::DarkGrey)
.bgc(Color::DarkYellow)
.build(),
),
inactive_item_style: Some(StyleBuilder::new().fgc(Color::DarkGrey).build()),
lines: Some(3),
},
validator: Default::default(),
Expand Down

0 comments on commit 9a88915

Please sign in to comment.