chronolog 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2121db8e20b57a6cd648cf542b0d83daabf643122a535fc4eaf4d3a3c5f14fc9
4
+ data.tar.gz: 0e266057d3ef6ac6efe377b30669d15819d6c6dee267753de280c319d8e2a920
5
+ SHA512:
6
+ metadata.gz: 5d6688dc5c6f56dc2afb7abb6d83fa248c350688e9e3a512db73ae46c7572cdfa1bdd2ad82d67c7d2efcc29ee66fa41b560352ee3db7add105faab5bc572a2af
7
+ data.tar.gz: 8607f17597c5e907575e08d134451f77c092f3a8e5f580c1bfeb40c9d6f60bb776c990b3722f2799d0d894b49d2e8fca1c945e7bfc5515df083c05b73b9ea9ea
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Vincent Ollivier
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ Chronolog
2
+ =========
3
+
4
+ [![Gem](https://img.shields.io/gem/v/chronolog.svg)](https://rubygems.org/gems/chronolog)
5
+
6
+
7
+ Install
8
+ -------
9
+
10
+ Get it from RubyGems:
11
+
12
+ $ gem install chronolog
13
+
14
+ Or build it from GitHub:
15
+
16
+ $ git clone git://github.com/vinc/chronolog.git
17
+ $ cd chronolog
18
+ $ gem build chronolog.gemspec
19
+ $ gem install chronolog-*.gem
20
+
21
+
22
+ License
23
+ -------
24
+
25
+ Copyright (c) 2018 Vincent Ollivier. Released under MIT.
data/bin/chronolog ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "chronolog"
4
+
5
+ path = ARGV.pop
6
+ cmd = ARGV.join(" ")
7
+ Chronolog::CLI.new(path).run(cmd)
data/lib/chronolog.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "chronolog/cli"
2
+ require "chronolog/engine"
3
+ require "chronolog/version"
@@ -0,0 +1,49 @@
1
+ require "readline"
2
+
3
+ module Chronolog
4
+ class CLI
5
+ CLI_CMDS = %w[quit help].freeze
6
+ LIB_CMDS = %w[print start stop].freeze
7
+ CMDS = (CLI_CMDS + LIB_CMDS).sort.freeze
8
+
9
+ def initialize(path)
10
+ @interactive = false
11
+ raise usage if path.nil?
12
+ @chronolog = Chronolog::Engine.new(File.open(path, "a+"))
13
+ end
14
+
15
+ def run(cmd)
16
+ if cmd.empty?
17
+ @interactive = true
18
+ comp = proc { |s| CMDS.grep(/^#{Regexp.escape(s)}/) }
19
+ Readline.completion_append_character = ""
20
+ Readline.completion_proc = comp
21
+ exec(cmd) while (cmd = Readline.readline("> ", true))
22
+ else
23
+ exec(cmd)
24
+ end
25
+ rescue Interrupt
26
+ exit
27
+ end
28
+
29
+ protected
30
+
31
+ def exec(cmd)
32
+ args = cmd.split
33
+ exit if cmd =~ /^q/
34
+ return @chronolog.send(args.shift, *args) if LIB_CMDS.include?(args[0])
35
+ usage
36
+ rescue StandardError => e
37
+ puts "Error: #{e.message}"
38
+ end
39
+
40
+ def usage
41
+ unless @interactive
42
+ puts "Usage: chronolog [command] <file.log>"
43
+ puts
44
+ end
45
+ commands = (@interactive ? CMDS : LIB_CMDS).join("', '")
46
+ puts "Available commands: '#{commands}'"
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,92 @@
1
+ require "csv"
2
+
3
+ module Chronolog
4
+ class Engine
5
+ def initialize(file)
6
+ @csv = CSV.new(file, col_sep: " ")
7
+
8
+ setup_variables
9
+ read_csv
10
+ end
11
+
12
+ def start
13
+ raise "Received 'start' after 'start'" unless @started_at.nil?
14
+ @started_at = Time.now
15
+ @csv << ["started", "at", @started_at.to_i]
16
+ end
17
+
18
+ def stop
19
+ raise "Received 'stop' before 'start'" if @started_at.nil?
20
+ @stopped_at = Time.now
21
+ @csv << ["stopped", "at", @stopped_at.to_i]
22
+ update_variables
23
+ @started_at = nil
24
+ end
25
+
26
+ def print(unit = "hours")
27
+ started_at = @started_at || Time.now
28
+ stopped_at = Time.now
29
+ current = stopped_at - started_at
30
+ %w[year month day session].each do |period|
31
+ duration = (current + previous(period, started_at)) / unit_length(unit)
32
+ puts format("This %- 10s % 7.2f %s", "#{period}:", duration, unit)
33
+ end
34
+ end
35
+
36
+ protected
37
+
38
+ def setup_variables
39
+ @years = Hash.new(0)
40
+ @months = Hash.new(0)
41
+ @days = Hash.new(0)
42
+ @csv.rewind
43
+ end
44
+
45
+ def read_csv
46
+ @csv.each do |row|
47
+ timestamp = row[2].to_i
48
+ case row[0]
49
+ when "started"
50
+ @started_at = Time.at(timestamp)
51
+ when "stopped"
52
+ @stopped_at = Time.at(timestamp)
53
+ raise "Found a 'stop' without a 'start'" if @started_at.nil?
54
+ raise "Found a 'stop' before a 'start'" if @started_at > @stopped_at
55
+ update_variables
56
+ @started_at = nil
57
+ end
58
+ end
59
+ end
60
+
61
+ def update_variables
62
+ duration = @stopped_at - @started_at
63
+ @years[@started_at.year] += duration
64
+ @months[@started_at.month] += duration
65
+ @days[@started_at.day] += duration
66
+ end
67
+
68
+ def previous(period, started_at)
69
+ return 0 if period == "session"
70
+ h = instance_variable_get("@#{period}s")
71
+ k = started_at.send(period)
72
+ h.fetch(k, 0)
73
+ end
74
+
75
+ def unit_length(unit)
76
+ case unit
77
+ when "seconds"
78
+ 1.0
79
+ when "kiloseconds"
80
+ 1000.0
81
+ when "hours"
82
+ 3600.0
83
+ when "days"
84
+ 86400.0
85
+ when "centidays"
86
+ 864.0
87
+ else
88
+ raise "Unsupported time unit"
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module Chronolog
2
+ VERSION = "0.0.1".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chronolog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vincent Ollivier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: v@vinc.cc
15
+ executables:
16
+ - chronolog
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - bin/chronolog
23
+ - lib/chronolog.rb
24
+ - lib/chronolog/cli.rb
25
+ - lib/chronolog/engine.rb
26
+ - lib/chronolog/version.rb
27
+ homepage: https://github.com/vinc/chronolog
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.7.6
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: Log elapsed time
51
+ test_files: []