howlong 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 0758ed1eaa5aa03c8e55ea63085ac9c943491e71
4
- data.tar.gz: 80869a140b7317ad14791a4e991c764d691e9bc1
2
+ SHA256:
3
+ metadata.gz: c0bdc9700e0ec8b6a26d9a7193008b619e6b61e1953dc3819db035fafa11cd4d
4
+ data.tar.gz: f8853feaa049dd5a947df928a3e86960aec5fd605a24518b1acfaa3a70518fee
5
5
  SHA512:
6
- metadata.gz: d0755b4f7b3472f804ad3c9c8ec32bf9dc74420e8475fb8f4b6fd0ae7bfcf782fbc8b6658e976e561ce7cbbed4bb3856a89ff75da99fea40c78a25f1e55c35aa
7
- data.tar.gz: c585487b2d250dcc205a0679e4e98e2010dcb9b7ccd63eaf359572c5077c44c745786aacd6929391f21d59ce5e5c50feb18610427937260e83057294a74ae398
6
+ metadata.gz: bbc3ddc575275a37e93bc2781b56d7282ed1e40d7d0a6dd7f54abb4a210beba6c7645e80b2bc2f8e829a1857fbdce6888ebdcaed45d0940b7af9a22c18064e71
7
+ data.tar.gz: 2869aaa3018df8cb8ce3645df5ae75740c480496993e48fb735cd6595015e9c1fe85910166394d834c3e97b1a657bc485505670b17856683ad2f487fcac12ed7
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .byebug_history
@@ -0,0 +1,18 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=0136d7c3032da51340862387d190f8d420a8dfa088cce8eb41c873e56afd7c9e
4
+ language: ruby
5
+ rvm:
6
+ - 2.4.9
7
+ - 2.5.7
8
+ - 2.6.5
9
+ - ruby-head
10
+ before_script:
11
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
12
+ - chmod +x ./cc-test-reporter
13
+ - ./cc-test-reporter before-build
14
+ install: bundle install
15
+ script:
16
+ - bundle exec rake spec
17
+ after_script:
18
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0
4
+
5
+ * Adds specs, Travis build, Code Climate coverage
6
+ * Changes the API a bit
7
+
8
+ ## 0.0.1
9
+
10
+ * First release
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in howlong.gemspec
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # How long
2
2
 
3
+ [![Build Status](https://travis-ci.org/picandocodigo/howlong.svg?branch=master)](https://travis-ci.org/picandocodigo/howlong) <a href="https://codeclimate.com/github/picandocodigo/howlong/maintainability"><img src="https://api.codeclimate.com/v1/badges/72c9febd52a3b6293ae7/maintainability" /></a> <a href="https://codeclimate.com/github/picandocodigo/howlong/test_coverage"><img src="https://api.codeclimate.com/v1/badges/72c9febd52a3b6293ae7/test_coverage" /></a>
4
+
3
5
  Ruby script to search for a running process and tell you how long it's been running:
4
6
 
5
7
  ```bash
@@ -18,4 +20,4 @@ $ rake console
18
20
  Process emacs has been active for 33 minutes and 25 seconds
19
21
  => nil
20
22
  2.1.2 :002 >
21
- ```
23
+ ```
data/Rakefile CHANGED
@@ -1,8 +1,13 @@
1
- require "bundler/gem_tasks"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
2
4
  require 'rake/testtask'
3
5
 
4
- Rake::TestTask.new("test") do |t|
5
- t.pattern = "test/**/*_test.rb"
6
+ begin
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:spec)
9
+ task default: :spec
10
+ rescue LoadError
6
11
  end
7
12
 
8
13
  task :console do
@@ -12,5 +17,3 @@ task :console do
12
17
  ARGV.clear
13
18
  IRB.start
14
19
  end
15
-
16
- task :default => 'test'
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  require 'howlong'
3
5
 
4
6
  unless ARGV.length == 1
@@ -7,4 +9,4 @@ unless ARGV.length == 1
7
9
  exit
8
10
  end
9
11
 
10
- Howlong.run(ARGV[0])
12
+ puts Howlong.run(ARGV[0])
@@ -1,24 +1,35 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'howlong/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "howlong"
8
+ spec.name = 'howlong'
8
9
  spec.version = Howlong::VERSION
9
- spec.authors = ["Fernando Briano"]
10
- spec.email = ["fernando@picandocodigo.net"]
11
- spec.summary = %q{ Search for a process and display how long it's been running on your system in a friendly way}
12
- spec.description = %q{ A simple gem which allows you to see how long a process has been running on your system in a friendly way}
13
- spec.homepage = "https://github.com/picandocodigo/howlong"
14
- spec.license = "MIT"
10
+ spec.authors = ['Fernando Briano']
11
+ spec.email = ['fernando@picandocodigo.net']
12
+ spec.summary = " Search for a process and display how long it's been running on your system in a friendly way"
13
+ spec.description = ' A simple gem which allows you to see how long a process has been running on your system in a friendly way'
14
+ spec.homepage = 'https://github.com/picandocodigo/howlong'
15
+ spec.license = 'MIT'
15
16
 
16
- spec.files = `git ls-files`.split($/)
17
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler'
23
+ spec.add_development_dependency 'byebug'
24
+ spec.add_development_dependency 'rake', '~> 13.0'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'timecop'
20
27
 
21
- spec.add_development_dependency "bundler", "~> 1.5"
22
- spec.add_development_dependency "rake", "~> 10.3"
23
- spec.add_development_dependency "byebug"
28
+ spec.metadata = {
29
+ "bug_tracker_uri" => "https://github.com/picandocodigo/howlong/issues",
30
+ "changelog_uri" => "https://github.com/picandocodigo/howlong/blob/master/CHANGELOG.md",
31
+ "documentation_uri" => "https://github.com/picandocodigo/howlong/blob/master/README.md",
32
+ "homepage_uri" => "https://github.com/picandocodigo/howlong",
33
+ "source_code_uri" => "https://github.com/picandocodigo/howlong"
34
+ }
24
35
  end
@@ -1,9 +1,11 @@
1
- require "howlong/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'howlong/version'
2
4
  require 'date'
3
5
 
4
6
  module Howlong
5
7
  def self.find_processes(search)
6
- processes = `ps -eo lstart,args | grep #{search}`.split(/\n/)
8
+ processes = processes_from_system(search).split(/\n/)
7
9
  names = []
8
10
  processes.each do |p|
9
11
  process = p.match(/\S+#{search}/)
@@ -17,50 +19,64 @@ module Howlong
17
19
  names
18
20
  end
19
21
 
20
- def self.show(process, delayed)
21
- unless delayed == nil
22
- print = "Process #{process} has been active for "
23
- if delayed[:days] > 0
24
- print += "#{delayed[:days]} days, #{(delayed[:seconds].to_int / 86_400) % 24} hours, #{delayed[:minutes] % 60} minutes "
25
- elsif delayed[:hours] > 0
26
- print += "#{delayed[:hours]} hours, #{delayed[:minutes] % 60} minutes "
22
+ def self.build_sentence(process, delayed)
23
+ return if delayed.nil?
24
+
25
+ printable = "Process #{process} has been active for "
26
+ if (delayed[:days]).positive?
27
+ printable += "#{delayed[:days]} days, #{(delayed[:seconds].to_int / 86_400) % 24} hours, #{delayed[:minutes] % 60} minutes "
28
+ elsif (delayed[:hours]).positive?
29
+ printable += "#{delayed[:hours]} hours, #{delayed[:minutes] % 60} minutes "
27
30
  else
28
- print += "#{delayed[:minutes]} minutes "
29
- end
30
- puts print + "and #{delayed[:seconds] % delayed[:minutes]} seconds"
31
+ printable += "#{delayed[:minutes]} minutes "
31
32
  end
33
+ printable + "and #{delayed[:seconds] % delayed[:minutes]} seconds"
34
+ end
35
+
36
+ def self.sentences_string(process)
37
+ sentences_array(process).join("\n")
32
38
  end
33
39
 
34
40
  def self.run(process)
41
+ puts sentences_string(process)
42
+ end
43
+
44
+ def self.sentences_array(process)
35
45
  processes = find_processes(process)
46
+ result = []
36
47
  processes.each do |p|
37
- show(p[0], p[1])
48
+ result << build_sentence(p[0], p[1])
38
49
  end
50
+ result
39
51
  end
40
52
 
53
+ class << self
54
+ private
41
55
 
42
- private
43
- def self.elapsed_time(process)
44
- p = process.split(/\s/)[6]
45
- # We need the current offset so we can make time operations in the
46
- # same timezone, yey timezones!
47
- offset = Time.now.gmt_offset / 3600
48
- # Get the date from ps in the "Day Mon %m hh:mm:ss yyyy" format,
49
- # substract offset and pass it to time
50
- start = (DateTime.parse(process.match(/[a-zA-Z\s0-9:]+[0-9]{4}/)[0]) - (offset / 24.0)).to_time
51
- length = Time.now - start
56
+ def processes_from_system(search)
57
+ `ps -eo lstart,args | grep -i #{search}`
58
+ end
52
59
 
53
- seconds = length.to_int
54
- minutes = seconds / 60
55
- hours = seconds / 3_600
56
- days = seconds / 86_400
60
+ def elapsed_time(process)
61
+ # We need the current offset so we can make time operations in the
62
+ # same timezone, yey timezones!
63
+ offset = Time.now.gmt_offset / 3600
64
+ # Get the date from ps in the "Day Mon %m hh:mm:ss yyyy" format,
65
+ # substract offset and pass it to time
66
+ start = (DateTime.parse(process.match(/[a-zA-Z\s0-9:]+[0-9]{4}/)[0]) - (offset / 24.0)).to_time
67
+ length = Time.now - start
57
68
 
58
- return {
59
- seconds: length.to_int,
60
- minutes: minutes,
61
- hours: hours,
62
- days: days
63
- }
64
- end
69
+ seconds = length.to_int
70
+ minutes = seconds / 60
71
+ hours = seconds / 3_600
72
+ days = seconds / 86_400
65
73
 
74
+ {
75
+ seconds: length.to_int,
76
+ minutes: minutes,
77
+ hours: hours,
78
+ days: days
79
+ }
80
+ end
81
+ end
66
82
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Howlong
2
- VERSION = "0.0.1"
4
+ VERSION = '0.1.0'
3
5
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'howlong'
4
+ require 'timecop'
5
+
6
+ describe Howlong do
7
+ let(:app) { 'emacs' }
8
+
9
+ context 'basic usage' do
10
+ it 'should return the desired format' do
11
+ allow(Howlong).to receive(:processes_from_system) do
12
+ 'Wed May 15 17:00:14 2019 /usr/bin/emacs'
13
+ end
14
+
15
+ Timecop.freeze(Time.local(2019, 10, 10, 18, 0, 0)) do
16
+ expect(Howlong.sentences_array(app)).to eq ['Process /usr/bin/emacs has been active for 148 days, 4 hours, 59 minutes and 46 seconds']
17
+
18
+ expect(Howlong.sentences_string(app)).to eq 'Process /usr/bin/emacs has been active for 148 days, 4 hours, 59 minutes and 46 seconds'
19
+
20
+ expect do
21
+ Howlong.run(app)
22
+ end.to output(
23
+ "Process /usr/bin/emacs has been active for 148 days, 4 hours, 59 minutes and 46 seconds\n"
24
+ ).to_stdout
25
+ end
26
+ end
27
+ end
28
+ end
metadata CHANGED
@@ -1,45 +1,73 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: howlong
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fernando Briano
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-10 00:00:00.000000000 Z
11
+ date: 2019-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
25
39
  - !ruby/object:Gem::Version
26
- version: '1.5'
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '10.3'
47
+ version: '13.0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '10.3'
54
+ version: '13.0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: byebug
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: timecop
43
71
  requirement: !ruby/object:Gem::Requirement
44
72
  requirements:
45
73
  - - ">="
@@ -62,6 +90,8 @@ extensions: []
62
90
  extra_rdoc_files: []
63
91
  files:
64
92
  - ".gitignore"
93
+ - ".travis.yml"
94
+ - CHANGELOG.md
65
95
  - Gemfile
66
96
  - LICENSE.txt
67
97
  - README.md
@@ -70,10 +100,16 @@ files:
70
100
  - howlong.gemspec
71
101
  - lib/howlong.rb
72
102
  - lib/howlong/version.rb
103
+ - spec/howlong_spec.rb
73
104
  homepage: https://github.com/picandocodigo/howlong
74
105
  licenses:
75
106
  - MIT
76
- metadata: {}
107
+ metadata:
108
+ bug_tracker_uri: https://github.com/picandocodigo/howlong/issues
109
+ changelog_uri: https://github.com/picandocodigo/howlong/blob/master/CHANGELOG.md
110
+ documentation_uri: https://github.com/picandocodigo/howlong/blob/master/README.md
111
+ homepage_uri: https://github.com/picandocodigo/howlong
112
+ source_code_uri: https://github.com/picandocodigo/howlong
77
113
  post_install_message:
78
114
  rdoc_options: []
79
115
  require_paths:
@@ -89,10 +125,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
125
  - !ruby/object:Gem::Version
90
126
  version: '0'
91
127
  requirements: []
92
- rubyforge_project:
93
- rubygems_version: 2.4.5
128
+ rubygems_version: 3.0.6
94
129
  signing_key:
95
130
  specification_version: 4
96
131
  summary: Search for a process and display how long it's been running on your system
97
132
  in a friendly way
98
- test_files: []
133
+ test_files:
134
+ - spec/howlong_spec.rb