humane 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +31 -31
- data/lib/humane/time_formatter.rb +5 -5
- data/lib/humane/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 433a88d823e187b4481fb3c2632ba816b487b3b8047f272b5c4b17a930b6227b
|
|
4
|
+
data.tar.gz: 5c90d3a7ee778274382e8ca25d604d39e672333ddfb8985941b97e7ce345f335
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 42ceb33a1bd570aa85b8cc5a9417204fcd9d6041e79e6e44c3b7a65469f9139c2897019afef22aab9269bd3956599c2e1ed430021a64f399a2fc6fac0e996c30
|
|
7
|
+
data.tar.gz: fc9eaa4b043bf6547444631fde9fa2bba69c493ecc07d5e43375ac65f6a20c9f607c73776c7f3e535080a4d2c682747cb8bff8e017e5ddfe67a0f79e3869a4ae
|
data/README.md
CHANGED
|
@@ -1,14 +1,37 @@
|
|
|
1
1
|
# humane-ruby
|
|
2
2
|
|
|
3
|
-
[](https://rubygems.org/gems/humane)
|
|
4
3
|
[](https://www.ruby-lang.org/)
|
|
5
4
|
[](https://github.com/woodie/humane-ruby/actions/workflows/ci.yml)
|
|
6
5
|
[](https://github.com/woodie/humane-ruby/releases/latest)
|
|
7
6
|
[](LICENSE)
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
Getting human-readable file sizes with 1000-based math
|
|
9
|
+
(as the Mac Finder displays) and relative times worded the way Swift's
|
|
10
|
+
`RelativeDateTimeFormatter` does turned out to be a real challenge to get
|
|
11
|
+
both right and simple. The `humane` library exists so a Ruby application can share
|
|
12
|
+
consistent size and time formatting with a Swift application, instead of
|
|
13
|
+
reaching for a library whose output doesn't match Swift's or that's
|
|
14
|
+
complicated to drop in.
|
|
10
15
|
|
|
11
|
-
|
|
16
|
+
```ruby
|
|
17
|
+
require "humane"
|
|
18
|
+
|
|
19
|
+
Humane::SizeFormatter.new.string(from_byte_count: 225_935) # "226 KB"
|
|
20
|
+
|
|
21
|
+
time_formatter = Humane::TimeFormatter.new
|
|
22
|
+
time_formatter.string(at: Time.now - 180, relative_to: Time.now) # "3 minutes ago"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Corresponding functions in Swift will have consistent output.
|
|
26
|
+
|
|
27
|
+
```swift
|
|
28
|
+
import Foundation
|
|
29
|
+
|
|
30
|
+
ByteCountFormatter.string(fromByteCount: Int64(225935), countStyle: .file) // "226 KB"
|
|
31
|
+
|
|
32
|
+
let formatter = RelativeDateTimeFormatter(); formatter.unitsStyle = .full
|
|
33
|
+
formatter.localizedString(for: time, relativeTo: now) // "3 minutes ago"
|
|
34
|
+
```
|
|
12
35
|
|
|
13
36
|
## Install
|
|
14
37
|
|
|
@@ -22,33 +45,10 @@ or in a `Gemfile`:
|
|
|
22
45
|
gem "humane"
|
|
23
46
|
```
|
|
24
47
|
|
|
25
|
-
## Usage
|
|
26
|
-
|
|
27
|
-
```ruby
|
|
28
|
-
require "humane"
|
|
29
|
-
|
|
30
|
-
size_formatter = Humane::SizeFormatter.new
|
|
31
|
-
size_formatter.string(from_byte_count: 225_935) # "226 KB" -- 1000-based math,
|
|
32
|
-
# capitalized units, matching
|
|
33
|
-
# Finder, not Rails'
|
|
34
|
-
# number_to_human_size (1024-
|
|
35
|
-
# based despite the same label)
|
|
36
|
-
|
|
37
|
-
time_formatter = Humane::TimeFormatter.new # collapse_minute: true
|
|
38
|
-
time_formatter.string(at: scanned_at, relative_to: Time.now)
|
|
39
|
-
# "3 minutes ago" / "in 3 minutes" / "less than a minute ago"
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
`Humane::TimeFormatter.new` uses `at:`, not `for:` -- Ruby's `for` is a
|
|
43
|
-
reserved word, and while a `for:` keyword argument technically parses, reading
|
|
44
|
-
it back out inside the method needs `binding.local_variable_get(:for)`. Not
|
|
45
|
-
worth it just to match Swift's `localizedString(for:relativeTo:)` label
|
|
46
|
-
literally.
|
|
47
|
-
|
|
48
48
|
## Scope
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
`
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
Finder's `.file` byte-count style, and a numeric (non-calendar-aware)
|
|
51
|
+
relative time style -- that's the whole surface area today.
|
|
52
|
+
`allowed_units`/alternate `count_style`s and a `:named` style
|
|
53
|
+
(`"yesterday"`, calendar-boundary-aware) aren't implemented -- contributions
|
|
54
|
+
welcome.
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Humane
|
|
4
|
-
# Formats one time relative to another the way Finder-adjacent tools do
|
|
4
|
+
# Formats one time relative to another the way Finder-adjacent tools do.
|
|
5
5
|
class TimeFormatter
|
|
6
|
-
#
|
|
7
|
-
def initialize(
|
|
8
|
-
@
|
|
6
|
+
# include_seconds shows exact seconds under a minute instead of collapsing to "less than a minute ago/in less than a minute". Defaults to false, matching ActionView's include_seconds.
|
|
7
|
+
def initialize(include_seconds: false)
|
|
8
|
+
@include_seconds = include_seconds
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
# Returns the time at `at` relative to `relative_to` as a human-readable string.
|
|
@@ -14,7 +14,7 @@ module Humane
|
|
|
14
14
|
future = seconds.negative?
|
|
15
15
|
seconds = seconds.abs
|
|
16
16
|
|
|
17
|
-
if
|
|
17
|
+
if !@include_seconds && seconds < 60
|
|
18
18
|
return future ? "in less than a minute" : "less than a minute ago"
|
|
19
19
|
end
|
|
20
20
|
|
data/lib/humane/version.rb
CHANGED