humane 0.1.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +53 -0
- data/lib/humane/size_formatter.rb +22 -0
- data/lib/humane/time_formatter.rb +41 -0
- data/lib/humane/version.rb +5 -0
- data/lib/humane.rb +8 -0
- metadata +52 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: fb176a1cd9cc6c573a593535d45a97d2e64677c54c55726b647374a0213e8f06
|
|
4
|
+
data.tar.gz: 80474100589a2447566088e8fd5d0c4ea9eb232c7c2a1ebc7a054a7d802c539b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4dd0a51d9b1571f64fdbab8e8708b9eaf5e018772fdf5d22bb0107eeaef8dcf778048046ab025f93e7d4c4dc7f6c51c344cb4efb2d9a35ad09a9adfec8d53acf
|
|
7
|
+
data.tar.gz: 7e190db93ce2f15afb477f207fc11519587da08bdcb684d46bd363c7756934579cac460b89364b42c5cbe1dea967d7052eba2d78e662a9114abbca010a60505f
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 John Woodell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# humane-ruby
|
|
2
|
+
|
|
3
|
+
[](https://www.ruby-lang.org/)
|
|
4
|
+
[](https://github.com/woodie/humane-ruby/actions/workflows/ci.yml)
|
|
5
|
+
[](https://github.com/woodie/humane-ruby/releases/latest)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
Swift's file sizes and relative dates for Ruby
|
|
9
|
+
|
|
10
|
+
Finder-accurate file sizes and relative dates for Ruby, modeled on Swift's [`ByteCountFormatter`](https://developer.apple.com/documentation/foundation/bytecountformatter) and [`RelativeDateTimeFormatter`](https://developer.apple.com/documentation/foundation/relativedatetimeformatter) -- not literal ports (both are closed-source, and `TimeFormatter`'s wording is a deliberate departure), but the same idea: a small, configurable formatter object instead of a bare helper method.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
gem install humane
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
or in a `Gemfile`:
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
gem "humane"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
require "humane"
|
|
28
|
+
|
|
29
|
+
size_formatter = Humane::SizeFormatter.new
|
|
30
|
+
size_formatter.string(from_byte_count: 225_935) # "226 KB" -- 1000-based math,
|
|
31
|
+
# capitalized units, matching
|
|
32
|
+
# Finder, not Rails'
|
|
33
|
+
# number_to_human_size (1024-
|
|
34
|
+
# based despite the same label)
|
|
35
|
+
|
|
36
|
+
time_formatter = Humane::TimeFormatter.new # collapse_minute: true
|
|
37
|
+
time_formatter.string(at: scanned_at, relative_to: Time.now)
|
|
38
|
+
# "3 minutes ago" / "3 minutes from now" / "less than a minute ago"
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`Humane::TimeFormatter.new` uses `at:`, not `for:` -- Ruby's `for` is a
|
|
42
|
+
reserved word, and while a `for:` keyword argument technically parses, reading
|
|
43
|
+
it back out inside the method needs `binding.local_variable_get(:for)`. Not
|
|
44
|
+
worth it just to match Swift's `localizedString(for:relativeTo:)` label
|
|
45
|
+
literally.
|
|
46
|
+
|
|
47
|
+
## Scope
|
|
48
|
+
|
|
49
|
+
Only what lambada and scandalous actually need today: Finder's `.file`
|
|
50
|
+
byte-count style, and a numeric (non-calendar-aware) relative time style.
|
|
51
|
+
`ByteCountFormatter`'s `allowed_units`/alternate `count_style`s and
|
|
52
|
+
`RelativeDateTimeFormatter`'s `:named` style (`"yesterday"`, calendar-
|
|
53
|
+
boundary-aware) aren't implemented -- contributions welcome.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Humane
|
|
4
|
+
# Formats byte counts the way Finder does: 1000-based math, capitalized unit labels.
|
|
5
|
+
class SizeFormatter
|
|
6
|
+
UNITS = %w[B KB MB GB TB PB EB].freeze
|
|
7
|
+
|
|
8
|
+
# Returns from_byte_count as a Finder-style human-readable string.
|
|
9
|
+
def string(from_byte_count:)
|
|
10
|
+
return "#{from_byte_count} B" if from_byte_count < 1000
|
|
11
|
+
|
|
12
|
+
exponent = [(Math.log(from_byte_count) / Math.log(1000)).to_i, UNITS.size - 1].min
|
|
13
|
+
rounded = (from_byte_count / (1000.0**exponent) * 10).round / 10.0
|
|
14
|
+
|
|
15
|
+
if rounded < 10
|
|
16
|
+
format("%.1f %s", rounded, UNITS[exponent])
|
|
17
|
+
else
|
|
18
|
+
format("%.0f %s", rounded, UNITS[exponent])
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Humane
|
|
4
|
+
# Formats one time relative to another the way Finder-adjacent tools do, symmetric "X ago"/"X from now".
|
|
5
|
+
class TimeFormatter
|
|
6
|
+
# collapse_minute buckets anything under a minute as "less than a minute ago/from now". Defaults to true.
|
|
7
|
+
def initialize(collapse_minute: true)
|
|
8
|
+
@collapse_minute = collapse_minute
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Returns the time at `at` relative to `relative_to` as a human-readable string.
|
|
12
|
+
def string(at:, relative_to:)
|
|
13
|
+
seconds = relative_to - at
|
|
14
|
+
future = seconds.negative?
|
|
15
|
+
seconds = seconds.abs
|
|
16
|
+
|
|
17
|
+
if @collapse_minute && seconds < 60
|
|
18
|
+
return future ? "less than a minute from now" : "less than a minute ago"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
text =
|
|
22
|
+
if seconds < 60
|
|
23
|
+
pluralize(seconds.to_i, "second")
|
|
24
|
+
elsif seconds < 3600
|
|
25
|
+
pluralize((seconds / 60.0).round, "minute")
|
|
26
|
+
elsif seconds < 86_400
|
|
27
|
+
pluralize((seconds / 3600.0).round, "hour")
|
|
28
|
+
else
|
|
29
|
+
pluralize((seconds / 86_400.0).round, "day")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
future ? "#{text} from now" : "#{text} ago"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def pluralize(count, unit)
|
|
38
|
+
count == 1 ? "1 #{unit}" : "#{count} #{unit}s"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
data/lib/humane.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: humane
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- John Woodell
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-07-08 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Finder-accurate file sizes and relative dates for Ruby, modeled on Swift's
|
|
14
|
+
ByteCountFormatter and RelativeDateTimeFormatter.
|
|
15
|
+
email:
|
|
16
|
+
- woodie@netpress.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- lib/humane.rb
|
|
24
|
+
- lib/humane/size_formatter.rb
|
|
25
|
+
- lib/humane/time_formatter.rb
|
|
26
|
+
- lib/humane/version.rb
|
|
27
|
+
homepage: https://github.com/woodie/humane-ruby
|
|
28
|
+
licenses:
|
|
29
|
+
- MIT
|
|
30
|
+
metadata:
|
|
31
|
+
homepage_uri: https://github.com/woodie/humane-ruby
|
|
32
|
+
source_code_uri: https://github.com/woodie/humane-ruby
|
|
33
|
+
post_install_message:
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '3.0'
|
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
requirements: []
|
|
48
|
+
rubygems_version: 3.3.5
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: Swift's file sizes and relative dates for Ruby
|
|
52
|
+
test_files: []
|