Skip to content

Commit

Permalink
feat: add prefers_reduced_motion flag
Browse files Browse the repository at this point in the history
  • Loading branch information
conradludgate committed Jan 29, 2024
1 parent 0faf414 commit eebf971
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
8 changes: 8 additions & 0 deletions atuin-client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ pub struct Settings {
pub word_chars: String,
pub scroll_context_lines: usize,
pub history_format: String,
pub prefers_reduced_motion: bool,

#[serde(with = "serde_regex", default = "RegexSet::empty")]
pub history_filter: RegexSet,
Expand Down Expand Up @@ -524,6 +525,13 @@ impl Settings {
.set_default("keymap_mode", "emacs")?
.set_default("keymap_mode_shell", "auto")?
.set_default("keymap_cursor", HashMap::<String, String>::new())?
.set_default(
"prefers_reduced_motion",
std::env::var("PREFERS_REDUCED_MOTION")
.ok()
.map(|s| config::Value::new(None, config::ValueKind::String(s)))
.unwrap_or_else(|| config::Value::new(None, config::ValueKind::Boolean(false))),
)?
.add_source(
Environment::with_prefix("atuin")
.prefix_separator("_")
Expand Down
13 changes: 11 additions & 2 deletions atuin/src/command/client/search/history_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct HistoryList<'a> {
inverted: bool,
/// Apply an alternative highlighting to the selected row
alternate_highlight: bool,
now: &'a dyn Fn() -> OffsetDateTime,
}

#[derive(Default)]
Expand Down Expand Up @@ -68,6 +69,7 @@ impl<'a> StatefulWidget for HistoryList<'a> {
state,
inverted: self.inverted,
alternate_highlight: self.alternate_highlight,
now: &self.now,
};

for item in self.history.iter().skip(state.offset).take(end - start) {
Expand All @@ -84,12 +86,18 @@ impl<'a> StatefulWidget for HistoryList<'a> {
}

impl<'a> HistoryList<'a> {
pub fn new(history: &'a [History], inverted: bool, alternate_highlight: bool) -> Self {
pub fn new(
history: &'a [History],
inverted: bool,
alternate_highlight: bool,
now: &'a dyn Fn() -> OffsetDateTime,
) -> Self {
Self {
history,
block: None,
inverted,
alternate_highlight,
now,
}
}

Expand Down Expand Up @@ -121,6 +129,7 @@ struct DrawState<'a> {
state: &'a ListState,
inverted: bool,
alternate_highlight: bool,
now: &'a dyn Fn() -> OffsetDateTime,
}

// longest line prefix I could come up with
Expand Down Expand Up @@ -160,7 +169,7 @@ impl DrawState<'_> {
// would fail.
// If the timestamp would otherwise be in the future, display
// the time since as 0.
let since = OffsetDateTime::now_utc() - h.timestamp;
let since = (self.now)() - h.timestamp;
let time = format_duration(since.try_into().unwrap_or_default());

// pad the time a little bit before we write. this aligns things nicely
Expand Down
26 changes: 20 additions & 6 deletions atuin/src/command/client/search/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crossterm::{
use eyre::Result;
use futures_util::FutureExt;
use semver::Version;
use time::OffsetDateTime;
use unicode_width::UnicodeWidthStr;

use atuin_client::{
Expand Down Expand Up @@ -69,6 +70,7 @@ pub struct State {

search: SearchState,
engine: Box<dyn SearchEngine>,
now: Box<dyn Fn() -> OffsetDateTime + Send>,
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -550,7 +552,8 @@ impl State {

match self.tab_index {
0 => {
let results_list = Self::build_results_list(style, results, self.keymap_mode);
let results_list =
Self::build_results_list(style, results, self.keymap_mode, &self.now);
f.render_stateful_widget(results_list, results_list_chunk, &mut self.results_state);
}

Expand Down Expand Up @@ -652,13 +655,18 @@ impl State {
stats
}

fn build_results_list(
fn build_results_list<'a>(
style: StyleState,
results: &[History],
results: &'a [History],
keymap_mode: KeymapMode,
) -> HistoryList<'_> {
let results_list =
HistoryList::new(results, style.invert, keymap_mode == KeymapMode::VimNormal);
now: &'a dyn Fn() -> OffsetDateTime,
) -> HistoryList<'a> {
let results_list = HistoryList::new(
results,
style.invert,
keymap_mode == KeymapMode::VimNormal,
now,
);

if style.compact {
results_list
Expand Down Expand Up @@ -882,6 +890,12 @@ pub async fn history(
value => value,
},
current_cursor: None,
now: if settings.prefers_reduced_motion {
let now = OffsetDateTime::now_utc();
Box::new(move || now)
} else {
Box::new(OffsetDateTime::now_utc)
},
};

app.initialize_keymap_cursor(settings);
Expand Down

0 comments on commit eebf971

Please sign in to comment.