noir 0.1.4 → 0.1.5
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/.travis.yml +1 -0
- data/README.md +5 -2
- data/Rakefile +5 -0
- data/lib/noir/command.rb +3 -1
- data/lib/noir/command/calculate.rb +5 -0
- data/lib/noir/command/calculate/time.rb +79 -0
- data/lib/noir/version.rb +1 -1
- data/spec/noir/command/calculate/time_spec.rb +127 -0
- data/spec/noir/command/calculate_spec.rb +10 -0
- metadata +11 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90bd80b4b1f6e7433d7768ccb5a84795f60a9ad7
|
4
|
+
data.tar.gz: a436503e74bbe62820630f46a22971167dc72412
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0135f35c72625357304488ee8bd959ade2ef844d61564a0a7c53be659d224c318fa9b340a493af9e5dab307ebdfb2b358ea9ae7cbbfab7cf9bcb1652e235856
|
7
|
+
data.tar.gz: f9e42d8a89705329f4e163ce9c072413ae9f3c458cf3b5aeafa5a6085e6f29fa363c4672be9479206833047c25e44889570964f1a990104e4109ab4b02b7b81c
|
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
language: ruby
|
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
# Noir
|
2
|
+
[](http://badge.fury.io/rb/noir)
|
3
|
+
[](https://travis-ci.org/atton-/noir)
|
4
|
+
[](https://codeclimate.com/github/atton-/noir)
|
2
5
|
|
3
6
|
Noir is utility commands for atton.
|
4
7
|
|
@@ -13,9 +16,9 @@ Noir is utility commands for atton.
|
|
13
16
|
|
14
17
|
## Enable Completion
|
15
18
|
|
16
|
-
|
19
|
+
Add this code in .zshrc
|
17
20
|
|
18
|
-
|
21
|
+
if which noir >& /dev/null; then eval "$(noir init zsh)"; fi
|
19
22
|
|
20
23
|
## Contributing
|
21
24
|
|
data/Rakefile
CHANGED
data/lib/noir/command.rb
CHANGED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
class Noir::Command::Calculate::Time < Noir::Base::TerminalCommand
|
4
|
+
@description = 'calculate formatted time in .txt file. time format : (%Y/%m/%d %H:%M:%S)'
|
5
|
+
|
6
|
+
TimeFormat = "%Y/%m/%d %H:%M:%S"
|
7
|
+
TimeRegexp = Regexp.new('^\s*\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}$')
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def execute *args
|
11
|
+
raise 'Please input target on commant arguments. (directory or .txt file)' if args.size.zero?
|
12
|
+
|
13
|
+
files = args.map{|f| extract_path(f)}.flatten
|
14
|
+
|
15
|
+
time_pairs = files.map{|f| pick_up_times(f)}
|
16
|
+
|
17
|
+
total_time = 0
|
18
|
+
files.zip(time_pairs) do |file, pairs|
|
19
|
+
print_file_total file, pairs
|
20
|
+
total_time += calc_total(pairs)
|
21
|
+
puts '-----'
|
22
|
+
end
|
23
|
+
|
24
|
+
puts "all total time : #{seconds_to_string(total_time)}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def extract_path path
|
28
|
+
raise "File not found. #{path}" unless File.exists?(path)
|
29
|
+
return path if File.file?(path)
|
30
|
+
|
31
|
+
if File.directory?(path)
|
32
|
+
return Dir.entries(path).reject{|filename| filename.start_with?('.') || File.directory?(filename)}
|
33
|
+
end
|
34
|
+
|
35
|
+
raise "Invalid path. please input file or directory #{path}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def pick_up_times path
|
39
|
+
text = File.read(path)
|
40
|
+
times = text.split("\n").select{|t| t =~ TimeRegexp}.map{|t| t.match(TimeRegexp).to_s.gsub(/^\s*/, '')}
|
41
|
+
|
42
|
+
raise "Count of formatted time is not even. : #{path}" unless times.size.even?
|
43
|
+
|
44
|
+
times = times.map{|t| Time.strptime(t, TimeFormat)}
|
45
|
+
|
46
|
+
raise "Formatted time was not sorted in #{path}" unless times.sort == times
|
47
|
+
|
48
|
+
diffs = times.map.each_slice(2).to_a
|
49
|
+
end
|
50
|
+
|
51
|
+
def calc_total time_pairs
|
52
|
+
time_pairs.map{|start, finish| finish - start}.inject(:+) || 0
|
53
|
+
end
|
54
|
+
|
55
|
+
def print_file_total file, time_pairs
|
56
|
+
if time_pairs.empty?
|
57
|
+
puts("Formatted time not found in : #{file}")
|
58
|
+
return
|
59
|
+
end
|
60
|
+
|
61
|
+
puts file
|
62
|
+
|
63
|
+
time_pairs.each do |start, finish|
|
64
|
+
diff = finish - start
|
65
|
+
puts "#{start.strftime(TimeFormat)} => #{finish.strftime(TimeFormat)} : #{seconds_to_string(diff)}"
|
66
|
+
end
|
67
|
+
|
68
|
+
puts "file total : #{seconds_to_string(calc_total(time_pairs))}"
|
69
|
+
end
|
70
|
+
|
71
|
+
def seconds_to_string time_sec
|
72
|
+
hours = time_sec.to_i / 3600
|
73
|
+
mins = (time_sec - (3600 * hours)) / 60
|
74
|
+
seconds = (time_sec - (3600 * hours)) % 60
|
75
|
+
|
76
|
+
format("%d:%02d:%02d", hours, mins, seconds)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/lib/noir/version.rb
CHANGED
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'noir'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe 'Noir::Command::Calculate::Time' do
|
5
|
+
before do
|
6
|
+
@paths = 10.times.map{Random.rand.to_s}
|
7
|
+
@path = @paths.first
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'is inherited Noir::Base::TerminalCommand' do
|
11
|
+
expect(Noir::Command::Calculate::Time.superclass).to eq(Noir::Base::TerminalCommand)
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'is raise exception when non-arguments' do
|
15
|
+
expect{ Noir::Command::Calculate::Time.execute }.to raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.extract_directory' do
|
19
|
+
|
20
|
+
def path_is_not_file path
|
21
|
+
allow(File).to receive(:exists?).with(path).and_return(false)
|
22
|
+
end
|
23
|
+
|
24
|
+
def path_is_file path
|
25
|
+
allow(File).to receive(:exists?).with(path).and_return(true)
|
26
|
+
allow(File).to receive(:file?).with(path).and_return(true)
|
27
|
+
end
|
28
|
+
|
29
|
+
def path_is_directory path
|
30
|
+
allow(File).to receive(:exists?).with(path).and_return(true)
|
31
|
+
allow(File).to receive(:file?).with(path).and_return(false)
|
32
|
+
allow(File).to receive(:directory?).with(path).and_return(true)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'is raise when not found file path.' do
|
36
|
+
path_is_not_file @path
|
37
|
+
expect{ Noir::Command::Calculate::Time.extract_path(@path) }.to raise_error(RuntimeError, /^File not/)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'is return raw path by exist file path' do
|
41
|
+
@paths.each do |path|
|
42
|
+
path_is_file @path
|
43
|
+
expect(Noir::Command::Calculate::Time.extract_path(@path)).to eq(@path)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'is return extracted path by exist directory path' do
|
48
|
+
@paths.each do |path|
|
49
|
+
path_is_directory @path
|
50
|
+
entry_of_directory = 'hoge'
|
51
|
+
allow(Dir).to receive(:entries).with(@path).and_return([entry_of_directory])
|
52
|
+
allow(File).to receive(:directory?).with(entry_of_directory).and_return(false)
|
53
|
+
expect(Noir::Command::Calculate::Time.extract_path(@path)).to eq([entry_of_directory])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'is return extracted path rejected start_with by exist directory path' do
|
58
|
+
path_is_directory @path
|
59
|
+
entries_of_directory = ['aaa', '.aaa', 'bbb', '.bbb', 'ccc']
|
60
|
+
entries_of_directory.each do |entry|
|
61
|
+
allow(File).to receive(:directory?).with(entry).and_return(false)
|
62
|
+
end
|
63
|
+
allow(Dir).to receive(:entries).with(@path).and_return(entries_of_directory)
|
64
|
+
expect(Noir::Command::Calculate::Time.extract_path(@path)).to eq(["aaa", "bbb", "ccc"])
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'is return extracted path rejected directory' do
|
68
|
+
path_is_directory @path
|
69
|
+
allow(Dir).to receive(:entries).with(@path).and_return(['aaa', 'bbb'])
|
70
|
+
allow(File).to receive(:directory?).with('aaa').and_return(false)
|
71
|
+
allow(File).to receive(:directory?).with('bbb').and_return(true)
|
72
|
+
expect(Noir::Command::Calculate::Time.extract_path(@path)).to eq(['aaa'])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '.pick_up_times' do
|
77
|
+
it 'is raise error when count of formatted time in txt is odd.' do
|
78
|
+
allow(File).to receive(:read).with(@path).and_return('2014/09/04 18:49:00')
|
79
|
+
expect{ Noir::Command::Calculate::Time.pick_up_times(@path) }.to raise_error(RuntimeError, /not even/)
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'is not raise when formatted time was not found' do
|
83
|
+
allow(File).to receive(:read).with(@path).and_return('')
|
84
|
+
expect(Noir::Command::Calculate::Time.pick_up_times(@path)).to eq([])
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'is return time pair when sorted formatted time pair' do
|
88
|
+
allow(File).to receive(:read).with(@path).and_return("2014/10/04 18:49:00\n2014/09/04 21:09:43")
|
89
|
+
expect{ Noir::Command::Calculate::Time.pick_up_times(@path) }.to raise_error(RuntimeError, /not sorted/)
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'is return time pair when sorted formatted time pair' do
|
93
|
+
allow(File).to receive(:read).with(@path).and_return("2014/09/04 18:49:00\n2014/09/04 21:09:43")
|
94
|
+
expect(Noir::Command::Calculate::Time.pick_up_times(@path)).to eq([[Time.new(2014, 9, 4, 18, 49),
|
95
|
+
Time.new(2014, 9, 4, 21, 9, 43)]])
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'is return time pair when sorted formatted time pair with space' do
|
99
|
+
allow(File).to receive(:read).with(@path).and_return(" 2014/09/04 18:49:00\n 2014/09/04 21:09:43")
|
100
|
+
expect(Noir::Command::Calculate::Time.pick_up_times(@path)).to eq([[Time.new(2014, 9, 4, 18, 49),
|
101
|
+
Time.new(2014, 9, 4, 21, 9, 43)]])
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe '.print_file_total' do
|
106
|
+
it 'is output "not found" when time_pairs was empty' do
|
107
|
+
expect{ Noir::Command::Calculate::Time.print_file_total(@path, []) }.to output(/not found in .* #{@path}/ ).to_stdout
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'is output when time_pairs was not empty' do
|
111
|
+
expect{ Noir::Command::Calculate::Time.print_file_total(@path, [[Time.new(2014, 9, 4, 18, 49),
|
112
|
+
Time.new(2014, 9, 4, 21, 9, 43)]]) }.to output(/total/).to_stdout
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe '.calc_total' do
|
117
|
+
it 'is return 0 when time_pair is empty' do
|
118
|
+
expect(Noir::Command::Calculate::Time.calc_total []).to eq(0)
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'is return summed time diff' do
|
122
|
+
dummy_params = [[5, 10], [20, 100]]
|
123
|
+
expect(Noir::Command::Calculate::Time.calc_total dummy_params).to eq(85)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: noir
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- atton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -61,6 +61,7 @@ extensions: []
|
|
61
61
|
extra_rdoc_files: []
|
62
62
|
files:
|
63
63
|
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
64
65
|
- Gemfile
|
65
66
|
- LICENSE.txt
|
66
67
|
- README.md
|
@@ -71,6 +72,8 @@ files:
|
|
71
72
|
- lib/noir/base/command.rb
|
72
73
|
- lib/noir/base/terminal_command.rb
|
73
74
|
- lib/noir/command.rb
|
75
|
+
- lib/noir/command/calculate.rb
|
76
|
+
- lib/noir/command/calculate/time.rb
|
74
77
|
- lib/noir/command/completion.rb
|
75
78
|
- lib/noir/command/edit.rb
|
76
79
|
- lib/noir/command/edit/note.rb
|
@@ -99,6 +102,8 @@ files:
|
|
99
102
|
- lib/noir/options.rb
|
100
103
|
- lib/noir/version.rb
|
101
104
|
- noir.gemspec
|
105
|
+
- spec/noir/command/calculate/time_spec.rb
|
106
|
+
- spec/noir/command/calculate_spec.rb
|
102
107
|
- spec/noir/command/completion_spec.rb
|
103
108
|
- spec/noir/command/edit/note_spec.rb
|
104
109
|
- spec/noir/command/edit/weekly_report/friday_spec.rb
|
@@ -151,11 +156,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
151
156
|
version: '0'
|
152
157
|
requirements: []
|
153
158
|
rubyforge_project:
|
154
|
-
rubygems_version: 2.
|
159
|
+
rubygems_version: 2.4.1
|
155
160
|
signing_key:
|
156
161
|
specification_version: 4
|
157
162
|
summary: Utilities for atton.
|
158
163
|
test_files:
|
164
|
+
- spec/noir/command/calculate/time_spec.rb
|
165
|
+
- spec/noir/command/calculate_spec.rb
|
159
166
|
- spec/noir/command/completion_spec.rb
|
160
167
|
- spec/noir/command/edit/note_spec.rb
|
161
168
|
- spec/noir/command/edit/weekly_report/friday_spec.rb
|
@@ -188,3 +195,4 @@ test_files:
|
|
188
195
|
- spec/noir/noir_spec.rb
|
189
196
|
- spec/noir/options_spec.rb
|
190
197
|
- spec/spec_helper.rb
|
198
|
+
has_rdoc:
|