Skip to content
Snippets Groups Projects
Commit 20f8d9c1 authored by cleemy desu wayo's avatar cleemy desu wayo
Browse files

Modify: add new feature "pre_procs" and "post_procs"

"pre_procs" is for the process before the execution of a Proc
registered by hook.

When overwriting e.body with pre_procs, it should be noted that
it will be executed as many times as the number of hooks.

"post_procs" treats the value returned by Proc registered by hook.

This new feature "post_procs" is related to 04be13b4 and 00b2fb7c.
parent 00b2fb7c
Branches main
No related tags found
No related merge requests found
#!/usr/bin/env ruby
#
# ==========================================================================
# Outvoke -- version 0.0.99.20241111
# Outvoke -- version 0.0.99.20241111.1
#
# written by cleemy desu wayo / Licensed under CC0 1.0
#
......@@ -23,7 +23,7 @@ class Outvoke
@version = Struct.new(:branch, :body).new(
'0.1',
'0.0.99.20241111'
'0.0.99.20241111.1'
)
@sources = Hash.new
......@@ -182,6 +182,8 @@ class Outvoke
hook_cond_array = hook['cond']
end
event.is_quiet_mode = @is_quiet_mode # TODO
# check condition
hook_cond_array.detect do |cond|
event.m = nil
......@@ -213,6 +215,11 @@ class Outvoke
hook_result = nil
hook_result_for_post_process = nil
# pre process
ds.pre_procs.each do |proc|
proc.call(event)
end
# execute
if hook['proc']
tmp_context = OutvokeProcExecContext.new(self, event)
......@@ -227,12 +234,9 @@ class Outvoke
hook_result_for_post_process = hook_result
end
if hook_result_for_post_process
if not @is_quiet_mode
puts "[#{event.time}] #{hook_result_for_post_process}".gsub("\n", " ")
else
puts hook_result_for_post_process.to_s
end
# post process
ds.post_procs.each do |proc|
proc.call(event, hook_result_for_post_process)
end
end
end
......@@ -350,7 +354,8 @@ module OutvokeDSL
end
class OutvokeDataSource
attr_accessor :label, :status, :log_lines, :lo_data, :mutex_lo, :is_first_time_log_check
attr_accessor :label, :status, :log_lines, :lo_data, :mutex_lo, :is_first_time_log_check,
:pre_procs, :post_procs
def initialize(label)
@label = label
......@@ -359,6 +364,27 @@ class OutvokeDataSource
@lo_data = []
@mutex_lo = Mutex.new
@is_first_time_log_check = false
#
# Proc objects for pre process
# (Procs to process before the execution of a Proc registered by hook)
#
@pre_procs = []
#
# Proc objects for post process
# (Procs to process the value returned by Proc registered by hook)
#
@post_procs = []
@post_procs << ->(e, hook_result) {
return unless hook_result
if e.is_quiet_mode
puts hook_result.to_s
else
puts "[#{e.time}] #{hook_result}".gsub("\n", " ")
end
}
end
def enabled?
......@@ -392,7 +418,7 @@ end
# (specifications are subject to sudden change without notice)
#
StructDSStdinStatus = Struct.new(:now, :last_time)
StructDSStdinEvent = Struct.new(:status, :time, :body, :m)
StructDSStdinEvent = Struct.new(:status, :time, :body, :m, :is_quiet_mode)
class OutvokeDataSourceStdin < OutvokeDataSource
def initialize(label)
......@@ -480,7 +506,7 @@ end
# (specifications are subject to sudden change without notice)
#
StructDSLoopbackStatus = Struct.new(:now, :last_time)
StructDSLoopbackEvent = Struct.new(:status, :time, :body, :m)
StructDSLoopbackEvent = Struct.new(:status, :time, :body, :m, :is_quiet_mode)
class OutvokeDataSourceLoopback < OutvokeDataSource
......@@ -525,7 +551,7 @@ end
StructDSVrchatStatus = Struct.new(:now, :logfile, :file_size,
:last_joined_time, :elapsed, :count, :lineno)
StructDSVrchatEvent = Struct.new(:status, :raw, :time, :type, :body, :m)
StructDSVrchatEvent = Struct.new(:status, :raw, :time, :type, :body, :m, :is_quiet_mode)
class OutvokeDataSourceVrchat < OutvokeDataSource
attr_accessor :log_dir
......@@ -642,7 +668,7 @@ class OutvokeDataSourceVrchat < OutvokeDataSource
end
StructDSEverySecStatus = Struct.new(:now, :last_time)
StructDSEverySecEvent = Struct.new(:status, :time, :body, :m)
StructDSEverySecEvent = Struct.new(:status, :time, :body, :m, :is_quiet_mode)
class OutvokeDataSourceEverySec < OutvokeDataSource
......
# for Outvoke 0.1
# written by cleemy desu wayo / Licensed under CC0 1.0
# initial release of this sample: 2024-11-11
# --------
#
# this sample code is for checking the new features introduced in
# version 0.0.99.20241111.1
#
# usage:
#
# $ seq 1 7 | ./outvoke.rb samples/2024/modify_20241111.1_post_procs.rb
#
# (try also with -q option)
#
# $ seq 1 7 | ./outvoke.rb -q samples/2024/modify_20241111.1_post_procs.rb
#
using OutvokeDSL
proc_buf = $outvoke.sources["stdin"].post_procs[0]
hookstd /2/ do
$outvoke.sources["stdin"].pre_procs << ->(e) {
puts "pre-process A ---- #{e.body}"
}
$outvoke.sources["stdin"].post_procs[0] = ->(e, x) {
return unless x
puts "post-process A ---- #{x}"
}
nil
end
hookstd /4/ do |e|
$outvoke.sources["stdin"].pre_procs << ->(e) {
puts "pre-process B ---- #{e.body}"
e.body = "#{e.body}-#{e.body}"
}
puts "e.body: #{e.body}"
$outvoke.sources["stdin"].post_procs << ->(e, x) {
return unless x
puts "post-process B ---- #{x}"
}
nil
end
hookstd /6/ do
$outvoke.quiet_mode = !($outvoke.quiet_mode?)
$outvoke.sources["stdin"].post_procs[0] = proc_buf
nil
end
hookstd do
"hookstd #{_1.body}"
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment