Skip to content

Commit

Permalink
Minimize usage of MessageExt::maybe_update method
Browse files Browse the repository at this point in the history
We use the MessageExt::maybe_update() helper method in quite a few
locations, including where it arguably is unnecessary, because, at least
with somewhat recent versions of Rust, we can achieve shorter code by
using the bool::then_some() combinator instead.
With this change we adjust a bunch of occurrences of the former with the
latter construct.
  • Loading branch information
d-e-s-o committed May 23, 2024
1 parent fec6af7 commit 76cea55
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
32 changes: 15 additions & 17 deletions src/ui/tab_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,23 +423,21 @@ impl Handleable<Event, Message> for TabBar {
let data = self.data_mut::<TabBarData>(cap);
match event {
Event::Key(key, _) => match key {
Key::Char('1') => MessageExt::maybe_update(None, self.set_select(cap, 0)).into_event(),
Key::Char('2') => MessageExt::maybe_update(None, self.set_select(cap, 1)).into_event(),
Key::Char('3') => MessageExt::maybe_update(None, self.set_select(cap, 2)).into_event(),
Key::Char('4') => MessageExt::maybe_update(None, self.set_select(cap, 3)).into_event(),
Key::Char('5') => MessageExt::maybe_update(None, self.set_select(cap, 4)).into_event(),
Key::Char('6') => MessageExt::maybe_update(None, self.set_select(cap, 5)).into_event(),
Key::Char('7') => MessageExt::maybe_update(None, self.set_select(cap, 6)).into_event(),
Key::Char('8') => MessageExt::maybe_update(None, self.set_select(cap, 7)).into_event(),
Key::Char('9') => MessageExt::maybe_update(None, self.set_select(cap, 8)).into_event(),
Key::Char('0') => {
MessageExt::maybe_update(None, self.set_select(cap, isize::MAX)).into_event()
},
Key::Char('`') => MessageExt::maybe_update(None, self.select_previous(cap)).into_event(),
Key::Char('h') => MessageExt::maybe_update(None, self.select(cap, -1)).into_event(),
Key::Char('l') => MessageExt::maybe_update(None, self.select(cap, 1)).into_event(),
Key::Char('H') => MessageExt::maybe_update(None, self.swap(cap, true)).into_event(),
Key::Char('L') => MessageExt::maybe_update(None, self.swap(cap, false)).into_event(),
Key::Char('1') => self.set_select(cap, 0).then_some(Event::Updated),
Key::Char('2') => self.set_select(cap, 1).then_some(Event::Updated),
Key::Char('3') => self.set_select(cap, 2).then_some(Event::Updated),
Key::Char('4') => self.set_select(cap, 3).then_some(Event::Updated),
Key::Char('5') => self.set_select(cap, 4).then_some(Event::Updated),
Key::Char('6') => self.set_select(cap, 5).then_some(Event::Updated),
Key::Char('7') => self.set_select(cap, 6).then_some(Event::Updated),
Key::Char('8') => self.set_select(cap, 7).then_some(Event::Updated),
Key::Char('9') => self.set_select(cap, 8).then_some(Event::Updated),
Key::Char('0') => self.set_select(cap, isize::MAX).then_some(Event::Updated),
Key::Char('`') => self.select_previous(cap).then_some(Event::Updated),
Key::Char('h') => self.select(cap, -1).then_some(Event::Updated),
Key::Char('l') => self.select(cap, 1).then_some(Event::Updated),
Key::Char('H') => self.swap(cap, true).then_some(Event::Updated),
Key::Char('L') => self.swap(cap, false).then_some(Event::Updated),
Key::Char('n') | Key::Char('N') => {
let event = match data.search.take() {
Search::Preparing(..) | Search::Unset => {
Expand Down
18 changes: 10 additions & 8 deletions src/ui/tag_dialog.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2021-2023 Daniel Mueller ([email protected])
// Copyright (C) 2021-2024 Daniel Mueller ([email protected])
// SPDX-License-Identifier: GPL-3.0-or-later

use std::cmp::Ordering;
Expand Down Expand Up @@ -284,7 +284,7 @@ impl TagDialog {

Some(Message::Updated)
},
Key::Char(' ') => MessageExt::maybe_update(None, data.toggle_tag()),
Key::Char(' ') => data.toggle_tag().then_some(Message::Updated),
Key::Char('f') => {
data
.data
Expand All @@ -299,10 +299,10 @@ impl TagDialog {
.map(|data| data.jump_to = Some(Direction::Backward));
None
},
Key::Char('g') => MessageExt::maybe_update(None, data.select(0)),
Key::Char('G') => MessageExt::maybe_update(None, data.select(isize::MAX)),
Key::Char('j') => MessageExt::maybe_update(None, data.change_selection(1)),
Key::Char('k') => MessageExt::maybe_update(None, data.change_selection(-1)),
Key::Char('g') => data.select(0).then_some(Message::Updated),
Key::Char('G') => data.select(isize::MAX).then_some(Message::Updated),
Key::Char('j') => data.change_selection(1).then_some(Message::Updated),
Key::Char('k') => data.change_selection(-1).then_some(Message::Updated),
_ => None,
}
}
Expand All @@ -325,8 +325,10 @@ impl TagDialog {

match key {
Key::Char(c) => {
let updated = data.select_tag_beginning_with(c, direction);
Some(MessageExt::maybe_update(None, updated))
let message = data
.select_tag_beginning_with(c, direction)
.then_some(Message::Updated);
Some(message)
},
// All non-char keys just reset the "jump to" flag directly and
// will be handled they same way they would have been had it not
Expand Down
26 changes: 12 additions & 14 deletions src/ui/task_list_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,10 @@ impl TaskListBox {
.iter(|mut iter| iter.position(|_task| Rc::ptr_eq(_task, &task)));

if let Some(idx) = idx {
let update = data.select(idx as isize);
if let Some(done) = done {
*done = true
}
MessageExt::maybe_update(None, update)
data.select(idx as isize).then_some(Message::Updated)
} else {
// If there is no `done` we were called directly from within the
// widget and not in response to a message from the TabBar. In
Expand Down Expand Up @@ -244,8 +243,7 @@ impl TaskListBox {
*search_state = SearchState::Done;

let data = self.data_mut::<TaskListBoxData>(cap);
let update = data.select(idx as isize);
MessageExt::maybe_update(None, update)
data.select(idx as isize).then_some(Message::Updated)
} else {
None
}
Expand Down Expand Up @@ -305,8 +303,8 @@ impl Handleable<Event, Message> for TaskListBox {
},
Key::Char('d') => {
if let Some(task) = data.selected_task() {
data.tasks.remove(task);
MessageExt::maybe_update(None, true).into_event()
let () = data.tasks.remove(task);
Some(Event::Updated)
} else {
None
}
Expand Down Expand Up @@ -352,8 +350,8 @@ impl Handleable<Event, Message> for TaskListBox {
.view
.iter(|mut iter| iter.nth(data.selection(1)).cloned());
if let Some(other) = other {
data.tasks.move_after(to_move, other);
MessageExt::maybe_update(None, data.change_selection(1)).into_event()
let () = data.tasks.move_after(to_move, other);
data.change_selection(1).then_some(Event::Updated)
} else {
None
}
Expand All @@ -368,8 +366,8 @@ impl Handleable<Event, Message> for TaskListBox {
.view
.iter(|mut iter| iter.nth(data.selection(-1)).cloned());
if let Some(other) = other {
data.tasks.move_before(to_move, other);
MessageExt::maybe_update(None, data.change_selection(-1)).into_event()
let () = data.tasks.move_before(to_move, other);
data.change_selection(-1).then_some(Event::Updated)
} else {
None
}
Expand All @@ -380,10 +378,10 @@ impl Handleable<Event, Message> for TaskListBox {
None
}
},
Key::Char('g') => MessageExt::maybe_update(None, data.select(0)).into_event(),
Key::Char('G') => MessageExt::maybe_update(None, data.select(isize::MAX)).into_event(),
Key::Char('j') => MessageExt::maybe_update(None, data.change_selection(1)).into_event(),
Key::Char('k') => MessageExt::maybe_update(None, data.change_selection(-1)).into_event(),
Key::Char('g') => data.select(0).then_some(Event::Updated),
Key::Char('G') => data.select(isize::MAX).then_some(Event::Updated),
Key::Char('j') => data.change_selection(1).then_some(Event::Updated),
Key::Char('k') => data.change_selection(-1).then_some(Event::Updated),
Key::Char('*') => {
if let Some(selected) = data.selected_task() {
let message = Message::StartTaskSearch(selected.summary());
Expand Down

0 comments on commit 76cea55

Please sign in to comment.