multish 0.2.0 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d76d493821db956c0ff89f1b06f65b9017130620400fbb58468c9c90f3417019
4
- data.tar.gz: 89234f44a40082755499b5a7b0279cff4e147ed43bde517f457a651653d2b56b
3
+ metadata.gz: d26fbeace3df038252b8a4b39319c07593ddede65c892542aa3bdbee2f3a8c8e
4
+ data.tar.gz: 9457c3ef496f10d3f23fad25f85c5d46a297bf5ed554676431b14ba8fee3a771
5
5
  SHA512:
6
- metadata.gz: 215dc7043862bc9c4185730f74855cb74448b4d5627f674595ff2978c944c56f4bfc5f284f058123aec0a5f14325672479b42df494d10079dca3fcf3ebebc37a
7
- data.tar.gz: '04969adf2a71b600d3cdf0d61d0d260c851fd6a49a12111d261cb2da2bb2b99dc793efea3966a2c2eecd23dbffb91ce53041bf82633d655d665171c96e1951d6'
6
+ metadata.gz: dda0a8d05902b289522d4e5a563554529018870c6120ab5706e1094e7d4a6fdfdabdd709f7de61b67fc58a5c04d96d8848607456426fdd6fa9f4a6e4e99737f3
7
+ data.tar.gz: cd86199b25f754b2762e7ae7be758128e2cd8929abdb553036b825ef15d35235cef012c47f5d0f6ca41db8127b2028b97a16403d53bfa3b7a813bc3afd9181c5
data/lib/multish.rb CHANGED
@@ -4,6 +4,8 @@ require 'curses'
4
4
  require 'open3'
5
5
  require 'colorize'
6
6
 
7
+ BRIGHT_WHITE = 15
8
+
7
9
  class MultishItem
8
10
  attr_reader :command
9
11
 
@@ -80,10 +82,94 @@ class MultishItem
80
82
 
81
83
  def print(text)
82
84
  @output << text
83
- @window.addstr(text)
85
+ color_print(@window, text)
84
86
  @window.refresh
85
87
  end
86
88
 
89
+ def color_print(window, input)
90
+ parse_commands(input) do |op, arg|
91
+ case op
92
+ when :string
93
+ window.addstr(arg)
94
+ when :reset
95
+ window.attroff(Curses.color_pair(10) | Curses::A_BOLD)
96
+ when :bold
97
+ window.attron(Curses::A_BOLD)
98
+ when :color
99
+ Curses.init_pair(10, arg, BRIGHT_WHITE)
100
+ window.attron(Curses.color_pair(10))
101
+ when :error
102
+ raise "ERROR: #{arg}"
103
+ end
104
+ end
105
+ end
106
+
107
+ def parse_commands(string)
108
+ parse_string(string) do |op, arg|
109
+ case op
110
+ when :string
111
+ yield [:string, arg]
112
+ when :escape
113
+ if arg == '[m'
114
+ yield [:reset]
115
+ elsif arg[/^\[(\d+;)+(\d+)m$/]
116
+ args = ($1 + $2).split(';')
117
+ args.each do |subarg|
118
+ subarg = subarg.to_i
119
+ case subarg
120
+ when 1
121
+ yield [:bold]
122
+ when 30..37
123
+ color = Curses::COLOR_BLACK + subarg - 30
124
+ yield [:color, color]
125
+ end
126
+ end
127
+ end
128
+ when :error
129
+ yield [:error, arg]
130
+ end
131
+ end
132
+ end
133
+
134
+ def parse_string(string)
135
+ len = string.length
136
+ i = 0
137
+ chars = ''
138
+ while i < len
139
+ char = string[i]
140
+ if char == "\e"
141
+ yield [:string, chars] if !chars.empty? && block_given?
142
+ chars = ''
143
+ escape = ''
144
+ i += 1
145
+ if string[i] == '['
146
+ escape << string[i]
147
+ i += 1
148
+ else
149
+ return yield [:error, string]
150
+ end
151
+ while string[i] =~ /[\x30-\x3f]/
152
+ escape << string[i]
153
+ i += 1
154
+ end
155
+ while string[i] =~ /[\x20–\x2f]/
156
+ escape << string[i]
157
+ i += 1
158
+ end
159
+ if string[i] =~ /[\x40-\x7e]/
160
+ escape << string[i]
161
+ else
162
+ return yield [:error, string]
163
+ end
164
+ yield [:escape, escape] if block_given?
165
+ else
166
+ chars << char
167
+ end
168
+ i += 1
169
+ end
170
+ yield [:string, chars] if !chars.empty? && block_given?
171
+ end
172
+
87
173
  def finished?
88
174
  return false unless @wait_thr
89
175
 
@@ -117,6 +203,7 @@ class Multish
117
203
  Curses.init_pair(2, Curses::COLOR_WHITE, Curses::COLOR_GREEN)
118
204
  Curses.init_pair(3, Curses::COLOR_WHITE, Curses::COLOR_RED)
119
205
  Curses.init_pair(4, Curses::COLOR_WHITE, Curses::COLOR_BLACK)
206
+ Curses.init_color(BRIGHT_WHITE, 1000, 1000, 1000)
120
207
  Curses.use_default_colors
121
208
  Curses.cbreak
122
209
  @commands.each(&:create_window!)
@@ -134,14 +221,17 @@ class Multish
134
221
  @commands.each(&:update_title!)
135
222
  break if @commands.all?(&:finished?)
136
223
  end
224
+ rescue StandardError => e
225
+ Curses.close_screen
226
+ warn 'INTERNAL ERROR'.red
227
+ warn e.message
228
+ warn e.backtrace
137
229
  ensure
138
230
  Curses.curs_set(1)
139
231
  Curses.close_screen
140
232
  if errored?
141
233
  warn 'At least one of the commands exited with error.'
142
- @commands.select(&:errored?).each do |command|
143
- command.print_output!
144
- end
234
+ @commands.select(&:errored?).each(&:print_output!)
145
235
  exit 1
146
236
  else
147
237
  exit 0
@@ -1,3 +1,3 @@
1
1
  class Multish
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multish
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Dąbrowski