cli_logger 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6b84d0722c4c14f6a4b133bbd6885c5c467eb3db
4
+ data.tar.gz: 9a145e375a05149a81b18fbc2f01bd6714445ed2
5
+ SHA512:
6
+ metadata.gz: d2f2400a85cd6c3d3c4a256d3796818f24f096338c989c82f1b6240991c17d929d2314a0dc6cf5fdfe500c56bd9395665e3697fde0199038606840a3a7538ceb
7
+ data.tar.gz: 7b7eb8385520e57f692cf9cc55f1f656d0b23b924990d25effb32b882847745e12d024ca567f77ea43c0b86aff2f096899f953a4fc31551f23840c2cbfe066d5
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --color
3
+ --format documentation
@@ -0,0 +1,8 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+ Metrics/MethodLength:
4
+ Max: 20
5
+ Style/SignalException:
6
+ Enabled: false
7
+ Style/ModuleFunction:
8
+ Enabled: false
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ install:
3
+ - gem install bundler --version '1.8.5'
4
+ - bundle install
5
+ rvm:
6
+ - 2.1.5
7
+ - 2.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cli_logger.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Tomasz Maczukin <tomasz@maczukin.pl>
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.
22
+
@@ -0,0 +1,34 @@
1
+ # CliLogger
2
+
3
+ ![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)
4
+ [![Build Status](https://travis-ci.org/tmaczukin/cli_logger.svg)](https://travis-ci.org/tmaczukin/cli_logger)
5
+
6
+ Another loggerm gem for ruby based CLI applications
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'cli_logger'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install cli_logger
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it ( https://github.com/tmaczukin/cli_logger/fork )
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create a new Pull Request
31
+
32
+ ## License
33
+
34
+ This is free sofware licensed under MIT license. See LICENSE file.
@@ -0,0 +1,13 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new('spec')
6
+ RuboCop::RakeTask.new
7
+
8
+ task :test do
9
+ Rake::Task['rubocop'].invoke
10
+ Rake::Task['spec'].invoke
11
+ end
12
+
13
+ task default: :test
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'cli_logger'
5
+ require 'pry'
6
+ Pry.start
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'cli_logger'
5
+
6
+ CliLogger.current_level = :debug
7
+ # CliLogger.set_options(:disable_spinner, :disable_progress_bar)
8
+
9
+ CliLogger.error('Error') do
10
+ CliLogger.warning('Warning') do |pb|
11
+ CliLogger.info('Info') do
12
+ 3.times do
13
+ CliLogger.debug('test')
14
+ sleep 0.4
15
+ end
16
+ sleep 0.4
17
+ end
18
+
19
+ (1..10).each do |tick|
20
+ sleep 0.4
21
+ CliLogger.debug("Step #{tick}")
22
+ pb.progress(tick.to_f / 10) if pb
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cli_logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cli_logger'
8
+ spec.version = CliLogger::VERSION
9
+ spec.authors = ['Tomasz Maczukin']
10
+ spec.email = ['tomasz@maczukin.pl']
11
+ spec.license = 'MIT'
12
+ spec.summary = 'Another loggerm gem for ruby based CLI applications'
13
+ spec.homepage = 'https://github.com/tmaczukin/cli_logger'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(/^spec/) }
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.8'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'rubocop', '~> 0.31.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.2.0'
24
+ spec.add_development_dependency 'pry', '~> 0.10.1'
25
+ spec.add_development_dependency 'simplecov', '~> 0.10.0'
26
+
27
+ spec.add_dependency 'colorize', '~> 0.7.5'
28
+ end
@@ -0,0 +1,14 @@
1
+ require 'colorize'
2
+
3
+ require 'cli_logger/exceptions'
4
+ require 'cli_logger/version'
5
+
6
+ # CliLogger module
7
+ #
8
+ module CliLogger
9
+ autoload :Logger, 'cli_logger/logger'
10
+ autoload :Spinner, 'cli_logger/spinner'
11
+ autoload :ProgressBar, 'cli_logger/progress_bar'
12
+
13
+ extend Logger
14
+ end
@@ -0,0 +1,5 @@
1
+ module CliLogger
2
+ class CliLoggerError < RuntimeError; end
3
+
4
+ class UnknownLogLevelError < CliLoggerError; end
5
+ end
@@ -0,0 +1,130 @@
1
+ module CliLogger
2
+ # Logger module
3
+ #
4
+ module Logger
5
+ # rubocop:disable Style/AccessorMethodName
6
+ def set_options(*args)
7
+ @options = args
8
+ end
9
+ # rubocop:enable Style/AccessorMethodName
10
+
11
+ def method_missing(name, *args, &block)
12
+ log(name.to_sym, *args, &block) if levels.key?(name.to_sym)
13
+ end
14
+
15
+ def log(log_level, message)
16
+ validate_level(log_level)
17
+ return unless levels[log_level] <= current_level
18
+
19
+ message = log_message(log_level, message)
20
+ if block_given?
21
+ sublog do
22
+ yield progress_bar
23
+ end
24
+ message += log_message(log_level, 'DONE')
25
+ end
26
+
27
+ message
28
+ end
29
+
30
+ def current_level
31
+ @current_level ||= levels[:info]
32
+ end
33
+
34
+ def current_level=(value)
35
+ validate_level(value)
36
+ @current_level = levels[value]
37
+ end
38
+
39
+ def levels
40
+ {
41
+ error: 0,
42
+ warning: 1,
43
+ info: 2,
44
+ debug: 3
45
+ }
46
+ end
47
+
48
+ private
49
+
50
+ def validate_level(log_level)
51
+ raise UnknownLogLevelError, log_level unless levels.key?(log_level)
52
+ end
53
+
54
+ def log_message(log_level, message)
55
+ progress_bar.clear_line if progress_bar
56
+
57
+ message = colorize(log_level, "#{prefix(log_level)}#{message}")
58
+ puts message
59
+
60
+ message
61
+ end
62
+
63
+ def prefix(log_level)
64
+ "#{nesting_prefix}[#{prefixes[log_level]}] "
65
+ end
66
+
67
+ def prefixes
68
+ {
69
+ error: '!',
70
+ warning: '-',
71
+ info: '*',
72
+ debug: '?'
73
+ }
74
+ end
75
+
76
+ def nesting_prefix
77
+ level = @nesting_level || 0
78
+ ' ' * level * 2
79
+ end
80
+
81
+ def colorize(log_level, message)
82
+ return message unless colors[log_level]
83
+ message.colorize(colors[log_level])
84
+ end
85
+
86
+ def colors
87
+ {
88
+ error: { color: :white, background: :red, mode: :bold },
89
+ warning: { color: :yellow, mode: :bold },
90
+ info: {},
91
+ debug: { color: :light_cyan }
92
+ }
93
+ end
94
+
95
+ def sublog
96
+ add_nesting
97
+ spinner.start if spinner
98
+
99
+ yield
100
+
101
+ spinner.stop if spinner
102
+ progress_bar.hide if progress_bar
103
+ sub_nesting
104
+ end
105
+
106
+ def add_nesting
107
+ @nesting_level ||= 0
108
+ @nesting_level += 1
109
+ @nesting_level
110
+ end
111
+
112
+ def sub_nesting
113
+ @nesting_level -= 1
114
+ @nesting_level = 0 if @nesting_level < 0
115
+ @nesting_level
116
+ end
117
+
118
+ def progress_bar
119
+ @progress_bar ||= ProgressBar.new(spinner) unless options.include?(:disable_progress_bar)
120
+ end
121
+
122
+ def spinner
123
+ @spinner ||= Spinner.new unless options.include?(:disable_spinner)
124
+ end
125
+
126
+ def options
127
+ @options ||= []
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,53 @@
1
+ module CliLogger
2
+ # ProgressBar class
3
+ #
4
+ class ProgressBar
5
+ def initialize(spinner)
6
+ @spinner = spinner
7
+ @visible = false
8
+ @width = 40
9
+ end
10
+
11
+ def progress(percent)
12
+ @spinner.stop if @spinner
13
+ @visible = true
14
+ $stdout.sync = true
15
+
16
+ raise InvalidProgressValueError, percent if percent < 0 || percent > 1
17
+
18
+ @percent = percent
19
+
20
+ print prepare_bar
21
+ end
22
+
23
+ def hide
24
+ $stdout.sync = true
25
+ @visible = false
26
+
27
+ clear_line
28
+ sleep 0.1
29
+ end
30
+
31
+ def clear_line
32
+ width = @width + 10
33
+
34
+ print "\r"
35
+ print ' ' * width
36
+ print "\r"
37
+ end
38
+
39
+ private
40
+
41
+ def prepare_bar
42
+ clear_line
43
+
44
+ progress = (@percent * @width).floor
45
+ progress_trace = '=' * progress
46
+ progress_bg = '-' * (@width - progress)
47
+
48
+ percent = @percent * 100
49
+
50
+ "[#{progress_trace}#{progress_bg}] ".green + ('%5.2f%%'.green.bold % percent)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,45 @@
1
+ module CliLogger
2
+ # Spinner class
3
+ #
4
+ class Spinner
5
+ def initialize
6
+ @chars = %w( + - - - )
7
+ @step = 0.095
8
+ end
9
+
10
+ def start
11
+ stop
12
+ sleep @step
13
+
14
+ @thread = Thread.new { spin }
15
+ end
16
+
17
+ def stop
18
+ return unless @thread
19
+ return if @thread[:done]
20
+
21
+ @thread[:done] = true
22
+ sleep @step / (@chars.count - 1)
23
+ clear_line
24
+ end
25
+
26
+ def spin
27
+ $stdout.sync = true
28
+ Thread.current[:done] = false
29
+ until Thread.current[:done]
30
+ clear_line
31
+ print @chars.join.green
32
+ @chars.unshift @chars.pop
33
+ sleep @step
34
+ end
35
+ end
36
+
37
+ def clear_line
38
+ width = @chars.count
39
+
40
+ print "\r"
41
+ print ' ' * width
42
+ print "\r"
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ # CliLogger module
2
+ #
3
+ module CliLogger
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cli_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Maczukin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.31.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.31.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.10.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.10.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.10.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.10.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: colorize
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.7.5
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.7.5
111
+ description:
112
+ email:
113
+ - tomasz@maczukin.pl
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/example
128
+ - bin/setup
129
+ - cli_logger.gemspec
130
+ - lib/cli_logger.rb
131
+ - lib/cli_logger/exceptions.rb
132
+ - lib/cli_logger/logger.rb
133
+ - lib/cli_logger/progress_bar.rb
134
+ - lib/cli_logger/spinner.rb
135
+ - lib/cli_logger/version.rb
136
+ homepage: https://github.com/tmaczukin/cli_logger
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.2.2
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Another loggerm gem for ruby based CLI applications
160
+ test_files: []