interactive-logger 0.1.1 → 0.1.2

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
2
  SHA1:
3
- metadata.gz: 0c5636a0534a2b4c303e9b6b3f83cdb0e1791dc6
4
- data.tar.gz: f11583fe28d1b0757f8a73edeb82d52d0d8b8bf6
3
+ metadata.gz: 1a9968debc063988b1bbc485e31145a487b5c526
4
+ data.tar.gz: 26611433c1293dcce8cc04d99d693a9873e09cdc
5
5
  SHA512:
6
- metadata.gz: 3156898ce2975d594b9799bcfe0abef48d68db309e7cc2b07345e61306c97994314e890ecb3ad72dcc447ef0e1df499525cf9ca21efc3b5e0e6730389ac5cc33
7
- data.tar.gz: 4136d190ac320d7e5f9d60100d1ad84a9783fba4c7c053fcc5ba6c43499533e538c6e7ef8c54db4114b6de45079195e4de7451f45dc19c156943e6ed0b9c4a57
6
+ metadata.gz: 6ff09b4c51163c387f018fa64a22927d918393bcb91b1b64dd0f4cf014e03e8aed804fd40074807722e1101a255081dd48e3edf019bb05319daad4f2dc5d0c45
7
+ data.tar.gz: bc8cc4c43d4f0201a67bd28dff417534c3655852ab80e86c4d3c2f773b2c57f60da3e3e7bad9e2bb06dee41548eda679076e3e8f2697b2b1c394a2d08c4ef82e
@@ -11,6 +11,7 @@ class InteractiveLogger
11
11
  def initialize(debug: false)
12
12
  @debug = debug
13
13
  @current_step = nil
14
+ @draw_mutex = Mutex.new
14
15
  end
15
16
 
16
17
  def debug?; @debug == true end
@@ -41,11 +42,16 @@ class InteractiveLogger
41
42
 
42
43
  loop do
43
44
  if queue.empty?
44
- @current_step.continue # Keep the UI updating regardless of actual process.
45
+ @draw_mutex.synchronize do
46
+ @current_step.continue # Keep the UI updating regardless of actual process.
47
+ end
45
48
  else
46
49
  until queue.empty?
47
50
  msg = queue.pop
48
- @current_step.send(msg.shift, *msg)
51
+
52
+ @draw_mutex.synchronize do
53
+ @current_step.send(msg.shift, *msg)
54
+ end
49
55
  end
50
56
  end
51
57
 
@@ -69,25 +75,32 @@ class InteractiveLogger
69
75
  def debug(str)
70
76
  return unless debug?
71
77
 
72
- @current_step.blank if @current_step
73
- print '--> '.yellow
74
- puts str
75
- @current_step.repaint if @current_step
78
+ @draw_mutex.synchronize do
79
+ @current_step.blank if @current_step
80
+ print '--> '.yellow
81
+ puts str
82
+ @current_step.repaint if @current_step
83
+ end
76
84
  end
85
+
77
86
  # Post an informative message above the current step output.
78
87
  def info(str)
79
- @current_step.blank if @current_step
80
- print '--> '.green
81
- puts str
82
- @current_step.repaint if @current_step
88
+ @draw_mutex.synchronize do
89
+ @current_step.blank if @current_step
90
+ print '--> '.green
91
+ puts str
92
+ @current_step.repaint if @current_step
93
+ end
83
94
  end
84
95
 
85
96
  # Post an error message above the current step output.
86
97
  def error(str)
87
- @current_step.blank if @current_step
88
- print '--> '.red
89
- puts str
90
- @current_step.repaint if @current_step
98
+ @draw_mutex.synchronize do
99
+ @current_step.blank if @current_step
100
+ print '--> '.red
101
+ puts str
102
+ @current_step.repaint if @current_step
103
+ end
91
104
  end
92
105
 
93
106
  # Post a single message, without any progress tracking.
@@ -1,4 +1,6 @@
1
1
  # coding: utf-8
2
+ require 'io/console'
3
+
2
4
  class InteractiveLogger
3
5
  class Step
4
6
  PROGRESS_SYMBOLS = %w(- \\ | /)
@@ -12,21 +14,24 @@ class InteractiveLogger
12
14
  @start = Time.now
13
15
  @show_time = show_time
14
16
  @pos = 0
15
- print_msg(in_progress_prefix << str)
17
+ print_trimmed(in_progress_prefix << str)
16
18
  end
17
19
 
18
20
  def continue(str = nil)
19
21
  @pos += 1
22
+ blank
20
23
  @last_str = str if str
21
- print_msg(in_progress_prefix << @last_str)
24
+ print_trimmed(in_progress_prefix << @last_str)
22
25
  end
23
26
 
24
27
  def failure(str = nil)
28
+ blank
25
29
  @last_str = str if str
26
30
  print_msg(prefix(FAILURE_SYMBOL) << @last_str)
27
31
  end
28
32
 
29
33
  def success(str = nil)
34
+ blank
30
35
  @last_str = str if str
31
36
  print_msg(prefix(SUCCESS_SYMBOL) << @last_str)
32
37
  end
@@ -35,7 +40,7 @@ class InteractiveLogger
35
40
  def blank
36
41
  print "\r"
37
42
  if @last_print_msg
38
- print ' ' * @last_print_msg.uncolorize.size
43
+ print ' ' * IO.console.winsize[1]
39
44
  end
40
45
  print "\r"
41
46
  end
@@ -64,5 +69,58 @@ class InteractiveLogger
64
69
  @last_print_msg = str
65
70
  print str
66
71
  end
72
+
73
+ # Trim string to current terminal width and break at newline so it can be
74
+ # replaced rather than causing the same line to print multiple times.
75
+ #
76
+ # The bulk of this logic is dedicated to counting only printable
77
+ # characters, and ensure we reset the colors afterward.
78
+ def print_trimmed(str)
79
+ terminal_width = IO.console.winsize[1]
80
+
81
+ # Take only the first line.
82
+ str = str.lines.first.chomp
83
+ @last_print_msg = ""
84
+
85
+ len = 0 # Printable length of characters sent to screen so far.
86
+ pos = 0 # Position in input string buffer.
87
+ c = true # Are we counting characters?
88
+
89
+ # Copy characters, including nonprintables, until we have copied
90
+ # terminal_width - 4 printables, then add an elipsis and call it
91
+ # a day.
92
+ loop do
93
+ if str[pos, 2] == "\e["
94
+ @last_print_msg << "\e["
95
+ pos += 2
96
+ c = false
97
+ next
98
+ end
99
+
100
+ if c == false && str[pos] == "m"
101
+ @last_print_msg << str[pos]
102
+ pos += 1
103
+ c = true
104
+ next
105
+ end
106
+
107
+ # Everything fits, nothing to do.
108
+ if pos == str.size
109
+ break
110
+ end
111
+
112
+ # We are going to run out of space, reset color to normal and draw an elipsis.
113
+ if len == terminal_width - 5 && str.size > pos + 3
114
+ @last_print_msg << "\e[0m..."
115
+ break
116
+ end
117
+
118
+ @last_print_msg << str[pos]
119
+ len += 1 if c
120
+ pos += 1
121
+ end
122
+
123
+ print @last_print_msg
124
+ end
67
125
  end
68
126
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interactive-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Smith <askreet@gmail.com>
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-08 00:00:00.000000000 Z
11
+ date: 2016-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '3.2'
34
34
  - - ">="
35
35
  - !ruby/object:Gem::Version
36
- version: 3.2.1
36
+ version: 3.2.3
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '3.2'
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: 3.2.1
46
+ version: 3.2.3
47
47
  description: A colorful, interactive logger for tracking progress of an operation.
48
48
  email: askreet@gmail.com
49
49
  executables: []
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  version: '0'
74
74
  requirements: []
75
75
  rubyforge_project:
76
- rubygems_version: 2.4.5.1
76
+ rubygems_version: 2.4.6
77
77
  signing_key:
78
78
  specification_version: 4
79
79
  summary: A colorful, interactive logger.