class TermuxRubyApi::Base

Public Instance Methods

api_command(command, stdin = nil, *args) click to toggle source

General executor of Termux API commands @param command [String] the termux command to execute (except the `termux-` prefix. @param stdin [String, nil] If not nil, the contents of `stdin` are passed as STDIN to the command @param args The rest of the arguments are passed verbatim to the executed command. @return [String] the STDOUT of the executed command

# File lib/termux_ruby_api/base.rb, line 70
def api_command(command, stdin = nil, *args)
  command, args = prepare_command_args(command, args)
  stdout, stderr, status = Open3.capture3(command, *args, stdin_data: stdin.to_s)
  raise CommandError.new(status: status, stderr: stderr) unless status.success?
  stdout
end
call_log() click to toggle source

Returns the `call_log` subsystem @return [TermuxRubyApi::SubSystems::CallLog]

# File lib/termux_ruby_api/base.rb, line 37
def call_log
  @call_log ||= TermuxRubyApi::SubSystems::CallLog.new(self)
end
clipboard() click to toggle source

Returns the `clipboard` subsystem @return [TermuxRubyApi::SubSystems::Clipboard]

# File lib/termux_ruby_api/base.rb, line 25
def clipboard
  @clipboard ||= TermuxRubyApi::SubSystems::Clipboard.new(self)
end
dialog() click to toggle source

Returns the `dialog` subsystem @return [TermuxRubyApi::SubSystems::Dialog]

# File lib/termux_ruby_api/base.rb, line 61
def dialog
  @dialog ||= TermuxRubyApi::SubSystems::Dialog.new(self)
end
generate_args(args) click to toggle source

Utility method to generate the arguments for a single command option @param args [Array] @return [Array, nil] If the last argument is blank, the whole list is ignored and returns nil @example Single element

generate_args(['-h']) #=> ['-h']

@example Two non-nil elements

generate_args(['-t', "My title"]) #=> ['-t', "My title"]

@example Two elements, with the last being nil

generate_args(['-t', nil]) #=> nil
# File lib/termux_ruby_api/base.rb, line 146
def generate_args(args)
  args.empty? || args.last.blank? ? nil : args
end
generate_args_list(args_list) click to toggle source

Utility method to generate args lists to be passed to `api_command` and the like @param [Array <Array(String, Object)>] @return [Array <String>] The array is expected to contain Arrays as elements. Each of them is passed to generate_args. Returns a flattened array with the resulting arguments. @example

generate_args_list([['-l', 3], ['-i'], ['-t', nil], ['My text']]) #=> ['-l', 3, '-i', 'My text']
# File lib/termux_ruby_api/base.rb, line 132
def generate_args_list(args_list)
  args = args_list.map { |args| generate_args(args) }.flatten.compact
end
json_api_command(command, stdin = nil, *args) click to toggle source

Executes a Termux API command returning the results parsed as JSON @param (see api_command) @return native Ruby classes parsed from JSON @note JSON objects are converted to ActiveSupport::HashWithIndifferentAccess

# File lib/termux_ruby_api/base.rb, line 81
def json_api_command(command, stdin = nil, *args)
  res = api_command(command, stdin, *args)
  return res if res.nil? || res == ''
  JSON.parse(res, symbolize_names: true, object_class: ActiveSupport::HashWithIndifferentAccess)
end
json_streamed_api_command(command, stdin = nil, *args) { |parsed_json| ... } click to toggle source

Executes the Termux API command and streams its results as parsed JSON, as soon as JSON structures are complete. @param (see streamed_api_command) Only works if a block is given. @return [nil]

# File lib/termux_ruby_api/base.rb, line 112
def json_streamed_api_command(command, stdin = nil, *args, &block)
  partial_out = ''
  streamed_api_command(command, stdin, *args) do |line|
    partial_out << line
    begin
      parsed_json = JSON.parse(partial_out, symbolize_names: true, object_class: ActiveSupport::HashWithIndifferentAccess)
      partial_out = ''
      yield parsed_json
    rescue
    end
  end
end
location() click to toggle source

Returns the `location` subsystem @return [TermuxRubyApi::SubSystems::Location]

# File lib/termux_ruby_api/base.rb, line 43
def location
  @location ||= TermuxRubyApi::SubSystems::Location.new(self)
end
sensor() click to toggle source

Returns the `sensor` subsystem @return [TermuxRubyApi::SubSystems::Sensor]

# File lib/termux_ruby_api/base.rb, line 55
def sensor
  @sensor ||= TermuxRubyApi::SubSystems::Sensor.new(self)
end
sms() click to toggle source

Returns the `sms` subsystem @return [TermuxRubyApi::SubSystems::Sms]

# File lib/termux_ruby_api/base.rb, line 49
def sms
  @sms ||= TermuxRubyApi::SubSystems::Sms.new(self)
end
streamed_api_command(command, stdin = nil, *args) { |line| ... } click to toggle source

Executes a Termux API command and streams its results, line by line to the provided block @param (see api_command) If a block is given, each new line output by the executed command is yielded to the block If a block is not given, it returns the output stream and the wait thread. @return [nil, [Io,Thread]]

# File lib/termux_ruby_api/base.rb, line 92
def streamed_api_command(command, stdin = nil, *args, &block)
  command, args = prepare_command_args(command, args)
  i, o, t = Open3.popen2(command, *args)
  i.puts(stdin) unless stdin.blank? # If we have any input, send it to the child
  i.close                           # Afterwards we can close child's stdin
  if block_given?
    o.each_line do |line|
      yield line
    end
    o.close
    raise "#{command} failed" unless t.value.success?
  else
    return o, t # The caller has to close o and wait for t
  end
end
tts() click to toggle source

Returns the `tts` subsystem @return [TermuxRubyApi::SubSystems::Tts]

# File lib/termux_ruby_api/base.rb, line 31
def tts
  @tts ||= TermuxRubyApi::SubSystems::Tts.new(self)
end

Protected Instance Methods

prepare_command_args(command, args) click to toggle source
# File lib/termux_ruby_api/base.rb, line 152
def prepare_command_args(command, args)
  command = "termux-" + Shellwords.escape(command.to_s)
  args = args.map { |arg| arg.to_s }
  return command, args
end