module Kanrisuru::Core::Stream
Public Instance Methods
cat(files, opts = {})
click to toggle source
# File lib/kanrisuru/core/stream.rb, line 88 def cat(files, opts = {}) command = Kanrisuru::Command.new('cat') command.append_flag('-T', opts[:show_tabs]) command.append_flag('-n', opts[:number]) command.append_flag('-s', opts[:squeeze_blank]) command.append_flag('-v', opts[:show_nonprinting]) command.append_flag('-E', opts[:show_ends]) command.append_flag('-b', opts[:number_nonblank]) command.append_flag('-A', opts[:show_all]) files = files.instance_of?(String) ? [files] : files command << files.join(' ') append_file(command, opts) execute_shell(command) Kanrisuru::Result.new(command, &:to_a) end
echo(text, opts = {})
click to toggle source
# File lib/kanrisuru/core/stream.rb, line 71 def echo(text, opts = {}) command = Kanrisuru::Command.new('echo') command.append_flag('-e', opts[:backslash]) command << "'#{text}'" append_file(command, opts) execute_shell(command) Kanrisuru::Result.new(command) do |cmd| if Kanrisuru::Util.present?(opts[:new_file]) nil else cmd.to_s end end end
head(path, opts = {})
click to toggle source
# File lib/kanrisuru/core/stream.rb, line 15 def head(path, opts = {}) command = Kanrisuru::Command.new('head') command.append_arg('-c', opts[:bytes]) command.append_arg('-n', opts[:lines]) command << path execute_shell(command) Kanrisuru::Result.new(command, &:to_a) end
read_file_chunk(path, start_line, end_line)
click to toggle source
# File lib/kanrisuru/core/stream.rb, line 37 def read_file_chunk(path, start_line, end_line) if !start_line.instance_of?(Integer) || !end_line.instance_of?(Integer) || start_line > end_line || start_line.negative? || end_line.negative? raise ArgumentError, 'Invalid start or end' end end_line = end_line - start_line + 1 command = Kanrisuru::Command.new("tail -n +#{start_line} #{path} | head -n #{end_line}") execute_shell(command) Kanrisuru::Result.new(command, &:to_a) end
sed(path, expression, replacement, opts = {})
click to toggle source
# File lib/kanrisuru/core/stream.rb, line 51 def sed(path, expression, replacement, opts = {}) command = Kanrisuru::Command.new('sed') command.append_flag('-i', opts[:in_place]) command.append_flag('-r', opts[:regexp_extended]) command << "'s/#{expression}/#{replacement}/g'" command << "'#{path}'" append_file(command, opts) execute_shell(command) Kanrisuru::Result.new(command) do |cmd| if Kanrisuru::Util.present?(opts[:new_file]) nil else cmd.to_a.join("\n") end end end
tail(path, opts = {})
click to toggle source
# File lib/kanrisuru/core/stream.rb, line 26 def tail(path, opts = {}) command = Kanrisuru::Command.new('tail') command.append_arg('-c', opts[:bytes]) command.append_arg('-n', opts[:lines]) command << path execute_shell(command) Kanrisuru::Result.new(command, &:to_a) end
Private Instance Methods
append_file(command, opts)
click to toggle source
# File lib/kanrisuru/core/stream.rb, line 109 def append_file(command, opts) return unless Kanrisuru::Util.present?(opts[:new_file]) case opts[:mode] when 'append' command << ">> #{opts[:new_file]}" when 'write' command << "> #{opts[:new_file]}" end end