-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com>
- Loading branch information
Showing
6 changed files
with
278 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* SPDX-License-Identifier: CC0-1.0 | ||
* | ||
* src/util/bits.rs | ||
* | ||
* This file is a component of ShadyURL by Elizabeth Myers. | ||
* | ||
* To the extent possible under law, the person who associated CC0 with | ||
* ShadyURL has waived all copyright and related or neighboring rights | ||
* to ShadyURL. | ||
* | ||
* You should have received a copy of the CC0 legalcode along with this | ||
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. | ||
*/ | ||
|
||
use num::PrimInt; | ||
|
||
pub fn count_trailing_zeroes<T: PrimInt>(mut n: T) -> u32 { | ||
let mut count = 0; | ||
while n != T::zero() { | ||
count += 1; | ||
n = n >> 1; | ||
} | ||
count | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* SPDX-License-Identifier: CC0-1.0 | ||
* | ||
* src/util/format.rs | ||
* | ||
* This file is a component of ShadyURL by Elizabeth Myers. | ||
* | ||
* To the extent possible under law, the person who associated CC0 with | ||
* ShadyURL has waived all copyright and related or neighboring rights | ||
* to ShadyURL. | ||
* | ||
* You should have received a copy of the CC0 legalcode along with this | ||
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. | ||
*/ | ||
|
||
use time::{ | ||
convert::{Day, Hour, Microsecond, Millisecond, Minute, Nanosecond, Second, Week}, | ||
Duration, | ||
}; | ||
|
||
use super::math::is_close; | ||
|
||
// This implementation is heavily modified from the time crate. | ||
pub fn humanize_duration(duration: Duration) -> String { | ||
const AVERAGE_YEAR: f64 = 365.2425; | ||
const AVERAGE_MONTH: f64 = AVERAGE_YEAR / 12.0; | ||
|
||
let suffix = if duration.is_positive() { | ||
"ago" | ||
} else { | ||
"from now" | ||
}; | ||
|
||
// Concise, rounded representation. | ||
|
||
if duration.is_zero() { | ||
return "now".to_string(); | ||
} | ||
|
||
/// Format the first item that produces a value greater than 1 and then break. | ||
macro_rules! item { | ||
($singular:literal, $plural:literal, $value:expr) => { | ||
let value = $value; | ||
if is_close(value.round(), 1.0) { | ||
return format!("{} {suffix}", $singular); | ||
} else if value > 1.0 { | ||
return format!("{value:.0} {} {suffix}", $plural); | ||
} | ||
}; | ||
} | ||
|
||
// Even if this produces a de-normal float, because we're rounding we don't really care. | ||
let seconds = duration.unsigned_abs().as_secs_f64(); | ||
|
||
item!( | ||
"a year", | ||
"years", | ||
seconds / (f64::from(Second::per(Day)) * AVERAGE_YEAR) | ||
); | ||
item!( | ||
"a month", | ||
"months", | ||
seconds / (f64::from(Second::per(Day)) * AVERAGE_MONTH) | ||
); | ||
item!("a week", "weeks", seconds / f64::from(Second::per(Week))); | ||
item!("a day", "days", seconds / f64::from(Second::per(Day))); | ||
item!("an hour", "hours", seconds / f64::from(Second::per(Hour))); | ||
item!( | ||
"a minute", | ||
"minutes", | ||
seconds / f64::from(Second::per(Minute)) | ||
); | ||
item!("a second", "seconds", seconds); | ||
item!( | ||
"a millisecond", | ||
"milliseconds", | ||
seconds * f64::from(Millisecond::per(Second)) | ||
); | ||
item!( | ||
"a microsecond", | ||
"microseconds", | ||
seconds * f64::from(Microsecond::per(Second)) | ||
); | ||
item!( | ||
"a nanosecond", | ||
"nanoseconds", | ||
seconds * f64::from(Nanosecond::per(Second)) | ||
); | ||
format!("an instant {suffix}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* SPDX-License-Identifier: CC0-1.0 | ||
* | ||
* src/util/macros.rs | ||
* | ||
* This file is a component of ShadyURL by Elizabeth Myers. | ||
* | ||
* To the extent possible under law, the person who associated CC0 with | ||
* ShadyURL has waived all copyright and related or neighboring rights | ||
* to ShadyURL. | ||
* | ||
* You should have received a copy of the CC0 legalcode along with this | ||
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. | ||
*/ | ||
|
||
#[macro_export] | ||
macro_rules! arr { | ||
( | ||
$( #[$attr:meta] )* | ||
$v:vis $id:ident $name:ident: [$ty:ty; _] = $value:expr | ||
) => { | ||
$( #[$attr] )* | ||
$v $id $name: [$ty; $value.len()] = $value; | ||
} | ||
} | ||
|
||
pub(crate) use arr; |
Oops, something went wrong.