From 6252e0affe3d86a5bb2f9276e124f729510a5379 Mon Sep 17 00:00:00 2001 From: Gennady Bekasov Date: Wed, 2 Nov 2022 13:08:50 +0300 Subject: [PATCH 1/2] Add read_file and background_command commands --- lib/spielbash/interactor/record_interactor.rb | 6 ++++ .../model/action/background_command_action.rb | 14 ++++++++ .../model/action/read_file_action.rb | 21 ++++++++++++ lib/spielbash/model/session.rb | 34 ++++++++++--------- lib/spielbash/spielbash.rb | 2 ++ lib/spielbash/version.rb | 2 +- 6 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 lib/spielbash/model/action/background_command_action.rb create mode 100644 lib/spielbash/model/action/read_file_action.rb diff --git a/lib/spielbash/interactor/record_interactor.rb b/lib/spielbash/interactor/record_interactor.rb index a98f926..44c3fad 100644 --- a/lib/spielbash/interactor/record_interactor.rb +++ b/lib/spielbash/interactor/record_interactor.rb @@ -67,6 +67,12 @@ def create_actions(context, scenes) when scene.has_key?('tmux_command') then cmd = scene['tmux_command'] Spielbash::TmuxCommandAction.new(cmd, action_context) + when scene.has_key?('read_file') then + cmd = scene['read_file'] + Spielbash::ReadFileAction.new(cmd, action_context) + when scene.has_key?('background_command') then + cmd = scene['background_command'] + Spielbash::BackgroundCommandAction.new(cmd, action_context) else not_implemented end diff --git a/lib/spielbash/model/action/background_command_action.rb b/lib/spielbash/model/action/background_command_action.rb new file mode 100644 index 0000000..9f2fcda --- /dev/null +++ b/lib/spielbash/model/action/background_command_action.rb @@ -0,0 +1,14 @@ +module Spielbash + class BackgroundCommandAction < Spielbash::BaseAction + attr_accessor :command + + def initialize(command, action_context) + super(action_context) + @command = command + end + + def execute(session) + session.execute_with_exactly('tmux', action_context.wait, false, true, '-c', command) + end + end +end diff --git a/lib/spielbash/model/action/read_file_action.rb b/lib/spielbash/model/action/read_file_action.rb new file mode 100644 index 0000000..de4f423 --- /dev/null +++ b/lib/spielbash/model/action/read_file_action.rb @@ -0,0 +1,21 @@ +module Spielbash + class ReadFileAction < Spielbash::BaseAction + attr_accessor :filePath + + def initialize(file_path, action_context) + super(action_context) + @filePath = file_path + end + + def execute(session) + File.open(filePath, "r").each_line do |row| + row.each_char do |c| + session.send_key(c) + sleep(action_context.typing_delay_s) + end + session.send_key('Enter') + end + sleep(action_context.reading_delay_s) + end + end +end diff --git a/lib/spielbash/model/session.rb b/lib/spielbash/model/session.rb index c4c9875..f992f55 100644 --- a/lib/spielbash/model/session.rb +++ b/lib/spielbash/model/session.rb @@ -46,6 +46,8 @@ def send_key(key, count=1) key = case key when ' ' then 'Space' when ';' then '\\;' + when '\t' then 'Tab' + when ' ' then 'Tab' else key end execute_tmux_with("send-keys -t #{name} -N #{count} #{key}", true) @@ -62,22 +64,6 @@ def execute_tmux_with(arguments, wait = false) execute_with('tmux', arguments, wait) end - private - - def exec_wait_check_cmd(pid) - if is_real_environment - execute_with('pgrep', "-P #{pid}", true) - else - cmd = context.wait_check_cmd.split - execute_with_exactly(cmd.first, true, false, true, *cmd.drop(1)) - end - end - - def execute_with(cmd, arguments, wait = false, leader = true, io_inherit = false) - args = arguments.split - execute_with_exactly cmd, wait, io_inherit, leader, *args - end - def execute_with_exactly(cmd, wait, io_inherit, leader, *arguments) raise "Please install #{cmd}" if is_real_environment && which(cmd).nil? @@ -99,6 +85,22 @@ def execute_with_exactly(cmd, wait, io_inherit, leader, *arguments) process end + private + + def exec_wait_check_cmd(pid) + if is_real_environment + execute_with('pgrep', "-P #{pid}", true) + else + cmd = context.wait_check_cmd.split + execute_with_exactly(cmd.first, true, false, true, *cmd.drop(1)) + end + end + + def execute_with(cmd, arguments, wait = false, leader = true, io_inherit = false) + args = arguments.split + execute_with_exactly cmd, wait, io_inherit, leader, *args + end + def is_real_environment context.wait_check_cmd.nil? end diff --git a/lib/spielbash/spielbash.rb b/lib/spielbash/spielbash.rb index 0728250..42e8941 100644 --- a/lib/spielbash/spielbash.rb +++ b/lib/spielbash/spielbash.rb @@ -12,3 +12,5 @@ require 'spielbash/model/action/new_environment_action' require 'spielbash/model/action/delete_environment_action' require 'spielbash/model/action/tmux_command_action' +require 'spielbash/model/action/read_file_action' +require 'spielbash/model/action/background_command_action' diff --git a/lib/spielbash/version.rb b/lib/spielbash/version.rb index ac88e50..c9d10ff 100644 --- a/lib/spielbash/version.rb +++ b/lib/spielbash/version.rb @@ -1,3 +1,3 @@ module Spielbash - VERSION = "0.1.4" + VERSION = "0.1.5" end From 571e6b4ec1e1273f7fe8c6d4ef888bdf65220e4b Mon Sep 17 00:00:00 2001 From: Gennady Bekasov Date: Wed, 2 Nov 2022 14:18:50 +0300 Subject: [PATCH 2/2] Fix time to read for command, delay before enter key --- lib/spielbash/model/action/command_action.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/spielbash/model/action/command_action.rb b/lib/spielbash/model/action/command_action.rb index 3005607..27ec479 100644 --- a/lib/spielbash/model/action/command_action.rb +++ b/lib/spielbash/model/action/command_action.rb @@ -12,11 +12,10 @@ def execute(session) session.send_key(c) sleep(action_context.typing_delay_s) end + sleep(action_context.reading_delay_s) session.send_key('C-m') session.wait if action_context.wait - - sleep(action_context.reading_delay_s) end end end \ No newline at end of file