Skip to content

Commit

Permalink
Rely on class << self more
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Jan 9, 2024
1 parent 653e159 commit 3ad6bdc
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 221 deletions.
104 changes: 52 additions & 52 deletions lib/flor/colours.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,78 +53,78 @@ class NoColours
@colours = Colours.new
@no_colours = NoColours.new

def self.no_colours
class << self

@no_colours
end
def no_colours

def self.colours(opts={})
@no_colours
end

#opts =
# case opts
# when Hash then opts
# when Colours, NoColours then { color: opts }
# else { out: opts }
# end
def colours(opts={})

c = nil;
[ :color, :colour, :colors, :colours ].each do |k|
if opts.has_key?(k); c = opts[k]; break; end
end
#opts =
# case opts
# when Hash then opts
# when Colours, NoColours then { color: opts }
# else { out: opts }
# end

return @colours if c == true
return @no_colours if c == false
c = nil;
[ :color, :colour, :colors, :colours ].each do |k|
if opts.has_key?(k); c = opts[k]; break; end
end

o = opts[:out] || $stdout
return @colours if c == true
return @no_colours if c == false

return @colours if (
(o.respond_to?(:log_colours?) ? o.log_colours? : o.tty?) ||
($0[-6..-1] == '/rspec' &&
(ARGV.include?('--tty') || ARGV.include?('--color'))))
o = opts[:out] || $stdout

@no_colours
end
return @colours if (
(o.respond_to?(:log_colours?) ? o.log_colours? : o.tty?) ||
($0[-6..-1] == '/rspec' &&
(ARGV.include?('--tty') || ARGV.include?('--color'))))

def self.decolour(s)
@no_colours
end

s.gsub(/\x1b\[\d+(;\d+)?m/, '')
end
def decolour(s)

def self.no_colour_length(s)
s.gsub(/\x1b\[\d+(;\d+)?m/, '')
end

decolour(s).length
end
def no_colour_length(s)

def self.truncate_string(s, maxlen, post='...')
decolour(s).length
end

ncl = no_colour_length(s)
r = StringIO.new
l = 0
def truncate_string(s, maxlen, post='...')

s.scan(/(\x1b\[\d+(?:;\d+)?m|[^\x1b]+)/) do |ss, _|
if ss[0, 1] == ""
r << ss
else
ncl = no_colour_length(s)
r = StringIO.new
l = 0

s.scan(/(\x1b\[\d+(?:;\d+)?m|[^\x1b]+)/) do |ss, _|
if ss[0, 1] == ""
r << ss
else
#p({ r: r.string, l: l, ssl: ss.length, maxlen: maxlen, reml: maxlen - l })
ss = ss[0, maxlen - l]
r << ss
l += ss.length
break if l >= maxlen
ss = ss[0, maxlen - l]
r << ss
l += ss.length
break if l >= maxlen
end
end
end

return r.string if l < maxlen

if post.is_a?(String)
r << post
elsif post.is_a?(Proc)
r << post.call(ncl, maxlen, s)
end
return r.string if l < maxlen

r.string
end
if post.is_a?(String)
r << post
elsif post.is_a?(Proc)
r << post.call(ncl, maxlen, s)
end

class << self
r.string
end

alias decolor decolour

Expand Down
109 changes: 56 additions & 53 deletions lib/flor/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,83 @@

module Flor

def self.generate_exid(domain, unit)
class << self

@exid_counter ||= 0
@exid_mutex ||= Mutex.new
def generate_exid(domain, unit)

t = Time.now.utc
@exid_counter ||= 0
@exid_mutex ||= Mutex.new

sus =
@exid_mutex.synchronize do
t = Time.now.utc

sus = t.sec * 100000000 + t.usec * 100 + @exid_counter
sus =
@exid_mutex.synchronize do

@exid_counter = @exid_counter + 1
@exid_counter = 0 if @exid_counter > 99
sus = t.sec * 100000000 + t.usec * 100 + @exid_counter

Munemo.to_s(sus)
end
@exid_counter = @exid_counter + 1
@exid_counter = 0 if @exid_counter > 99

t = t.strftime('%Y%m%d.%H%M')
Munemo.to_s(sus)
end

"#{domain}-#{unit}-#{t}.#{sus}"
end
t = t.strftime('%Y%m%d.%H%M')

def self.make_launch_msg(exid, tree, opts)
"#{domain}-#{unit}-#{t}.#{sus}"
end

t =
tree.is_a?(String) ?
Flor.parse(tree, opts[:fname] || opts[:path], opts) :
tree
def make_launch_msg(exid, tree, opts)

unless t
t =
tree.is_a?(String) ?
Flor.parse(tree, opts[:fname] || opts[:path], opts) :
tree

#h = opts.merge(prune: false, rewrite: false, debug: 0)
#Raabro.pp(Flor.parse(tree, h[:fname], h))
# TODO re-parse and indicate what went wrong...
unless t

fail ArgumentError.new(
"flow parsing failed: " + tree.inspect[0, 35] + '...')
end
#h = opts.merge(prune: false, rewrite: false, debug: 0)
#Raabro.pp(Flor.parse(tree, h[:fname], h))
# TODO re-parse and indicate what went wrong...

pl = opts[:payload] || opts[:fields] || {}
vs = opts[:variables] || opts[:vars] || {}
fail ArgumentError.new(
"flow parsing failed: " + tree.inspect[0, 35] + '...')
end

fail ArgumentError.new(
"given launch payload should be a Hash, but it's a #{pl.class}"
) unless pl.is_a?(Hash)
fail ArgumentError.new(
"given launch variables should come in a Hash, but it's a #{vs.class}"
) unless vs.is_a?(Hash)
pl = opts[:payload] || opts[:fields] || {}
vs = opts[:variables] || opts[:vars] || {}

msg = {
'point' => 'execute',
'exid' => exid,
'nid' => '0',
'tree' => t,
'payload' => pl,
'vars' => vs }
fail ArgumentError.new(
"given launch payload should be a Hash, but it's a #{pl.class}"
) unless pl.is_a?(Hash)
fail ArgumentError.new(
"given launch variables should come in a Hash, but it's a #{vs.class}"
) unless vs.is_a?(Hash)

msg['vdomain'] = opts[:vdomain] \
if opts.has_key?(:vdomain)
msg = {
'point' => 'execute',
'exid' => exid,
'nid' => '0',
'tree' => t,
'payload' => pl,
'vars' => vs }

msg
end
msg['vdomain'] = opts[:vdomain] \
if opts.has_key?(:vdomain)

msg
end

def self.load_procedures(dir)
def load_procedures(dir)

dirpath =
if dir.match(/\A[.\/]/)
File.join(dir, '*.rb')
else
File.join(File.dirname(__FILE__), dir, '*.rb')
end
dirpath =
if dir.match(/\A[.\/]/)
File.join(dir, '*.rb')
else
File.join(File.dirname(__FILE__), dir, '*.rb')
end

Dir[dirpath].sort.each { |path| require(path) }
Dir[dirpath].sort.each { |path| require(path) }
end
end
end

51 changes: 27 additions & 24 deletions lib/flor/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -697,42 +697,45 @@ def rewrite_flor(t)
alias rewrite_panode rewrite_flor
end # module Parser

def self.unescape_u(cs)
class << self

s = ''; 4.times { s << cs.next }
def unescape_u(cs)

[ s.to_i(16) ].pack('U*')
end
s = ''; 4.times { s << cs.next }

[ s.to_i(16) ].pack('U*')
end

def self.unescape(s)
def unescape(s)

sio = StringIO.new
sio = StringIO.new

cs = s.each_char
cs = s.each_char

loop do
loop do

c = cs.next
c = cs.next

break unless c
break unless c

if c == '\\'
case cn = cs.next
when 'u' then sio.print(unescape_u(cs))
when '\\', '"', '\'' then sio.print(cn)
when 'b' then sio.print("\b")
when 'f' then sio.print("\f")
when 'n' then sio.print("\n")
when 'r' then sio.print("\r")
when 't' then sio.print("\t")
else sio.print("\\#{cn}")
if c == '\\'
case cn = cs.next
when 'u' then sio.print(unescape_u(cs))
when '\\', '"', '\'' then sio.print(cn)
when 'b' then sio.print("\b")
when 'f' then sio.print("\f")
when 'n' then sio.print("\n")
when 'r' then sio.print("\r")
when 't' then sio.print("\t")
else sio.print("\\#{cn}")
end
else
sio.print(c)
end
else
sio.print(c)
end
end

sio.string
sio.string
end
end
end

Loading

0 comments on commit 3ad6bdc

Please sign in to comment.