Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,12 +416,10 @@ impl MultiState {
self.ordering.insert(pos, idx);
}
InsertLocation::After(after_idx) => {
let pos = self.ordering.iter().position(|i| *i == after_idx).unwrap();
self.ordering.insert(pos + 1, idx);
self.ordering.insert(self.visual_index(after_idx) + 1, idx);
}
InsertLocation::Before(before_idx) => {
let pos = self.ordering.iter().position(|i| *i == before_idx).unwrap();
self.ordering.insert(pos, idx);
self.ordering.insert(self.visual_index(before_idx), idx);
}
}

Expand Down Expand Up @@ -465,6 +463,14 @@ impl MultiState {
fn len(&self) -> usize {
self.members.len() - self.free_set.len()
}

/// Map from opaque index to position on screen.
pub(crate) fn visual_index(&self, opaque_index: usize) -> usize {
self.ordering
.iter()
.position(|v| *v == opaque_index)
.expect("no such member")
}
}

#[derive(Default)]
Expand Down
17 changes: 16 additions & 1 deletion src/progress_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,26 @@ impl ProgressBar {
self.state().tab_width
}

/// Index in the `MultiState`
/// Opaque index in the `MultiState`
///
/// This value is an implementation detail and does not necessarily correspond to visual
/// position on the screen.
///
/// It is an index into `MultiState::members`, which is not in any particular order (due to
/// reclaiming of indices from the `MultiState::free_set`).
pub(crate) fn index(&self) -> Option<usize> {
self.state().draw_target.remote().map(|(_, idx)| idx)
}

/// If this [`ProgressBar`] is a member of a [`MultiProgress`](crate::MultiProgress), then return visual position
/// on screen. Otherwise, `None`.
pub fn visual_index(&self) -> Option<usize> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think there's value in wrapping this up in a newtype VisualIndex? In some future we might be able to then use that in InsertLocation variants...

Or maybe, should this just be named index and we rename the private method to something else (members_index() maybe)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the second option - I'll work on it later this weekend/Monday. In that case, I would still suggest we newtype the "members index" type, so that we don't accidentally get them confused internally either.

self.state()
.draw_target
.remote()
.map(|(remote, idx)| remote.read().unwrap().visual_index(idx))
}

/// Current message
pub fn message(&self) -> String {
self.state().state.message.expanded().to_string()
Expand Down