Class: TermuxRubyApi::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/termux_ruby_api/base.rb

Instance Method Summary collapse

Instance Method Details

#api_command(command, stdin = nil, *args) ⇒ String

General executor of Termux API commands

Parameters:

  • command (String)

    the termux command to execute (except the ‘termux-` prefix.

  • stdin (String, nil) (defaults to: nil)

    If not nil, the contents of ‘stdin` are passed as STDIN to the command

  • args

    The rest of the arguments are passed verbatim to the executed command.

Returns:

  • (String)

    the STDOUT of the executed command

Raises:



70
71
72
73
74
75
# 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_logTermuxRubyApi::SubSystems::CallLog

Returns the ‘call_log` subsystem



37
38
39
# File 'lib/termux_ruby_api/base.rb', line 37

def call_log
  @call_log ||= TermuxRubyApi::SubSystems::CallLog.new(self)
end

#clipboardTermuxRubyApi::SubSystems::Clipboard

Returns the ‘clipboard` subsystem



25
26
27
# File 'lib/termux_ruby_api/base.rb', line 25

def clipboard
  @clipboard ||= TermuxRubyApi::SubSystems::Clipboard.new(self)
end

#dialogTermuxRubyApi::SubSystems::Dialog

Returns the ‘dialog` subsystem



61
62
63
# File 'lib/termux_ruby_api/base.rb', line 61

def dialog
  @dialog ||= TermuxRubyApi::SubSystems::Dialog.new(self)
end

#generate_args(args) ⇒ Array?

Utility method to generate the arguments for a single command option If the last argument is blank, the whole list is ignored and returns nil

Examples:

Single element

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

Two non-nil elements

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

Two elements, with the last being nil

generate_args(['-t', nil]) #=> nil

Parameters:

  • args (Array)

Returns:

  • (Array, nil)


146
147
148
# 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) ⇒ Array <String>

Utility method to generate args lists to be passed to ‘api_command` and the like 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.

Examples:

generate_args_list([['-l', 3], ['-i'], ['-t', nil], ['My text']]) #=> ['-l', 3, '-i', 'My text']

Parameters:

  • (Array <Array(String, Object)>)

Returns:

  • (Array <String>)


132
133
134
# 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) ⇒ Object

Note:

JSON objects are converted to ActiveSupport::HashWithIndifferentAccess

Executes a Termux API command returning the results parsed as JSON

Parameters:

  • command (String)

    the termux command to execute (except the ‘termux-` prefix.

  • stdin (String, nil) (defaults to: nil)

    If not nil, the contents of ‘stdin` are passed as STDIN to the command

  • args

    The rest of the arguments are passed verbatim to the executed command.

Returns:

  • native Ruby classes parsed from JSON



81
82
83
84
85
# 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, &block) ⇒ nil

Executes the Termux API command and streams its results as parsed JSON, as soon as JSON structures are complete. Only works if a block is given.

Parameters:

  • command (String)

    the termux command to execute (except the ‘termux-` prefix.

  • stdin (String, nil) (defaults to: nil)

    If not nil, the contents of ‘stdin` are passed as STDIN to the command

  • args

    The rest of the arguments are passed verbatim to the executed command.

Returns:

  • (nil)


112
113
114
115
116
117
118
119
120
121
122
123
# 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

#locationTermuxRubyApi::SubSystems::Location

Returns the ‘location` subsystem



43
44
45
# File 'lib/termux_ruby_api/base.rb', line 43

def location
  @location ||= TermuxRubyApi::SubSystems::Location.new(self)
end

#sensorTermuxRubyApi::SubSystems::Sensor

Returns the ‘sensor` subsystem



55
56
57
# File 'lib/termux_ruby_api/base.rb', line 55

def sensor
  @sensor ||= TermuxRubyApi::SubSystems::Sensor.new(self)
end

#smsTermuxRubyApi::SubSystems::Sms

Returns the ‘sms` subsystem



49
50
51
# 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, &block) ⇒ nil, [Io,Thread]

Executes a Termux API command and streams its results, line by line to the provided block 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.

Parameters:

  • command (String)

    the termux command to execute (except the ‘termux-` prefix.

  • stdin (String, nil) (defaults to: nil)

    If not nil, the contents of ‘stdin` are passed as STDIN to the command

  • args

    The rest of the arguments are passed verbatim to the executed command.

Returns:

  • (nil, [Io,Thread])


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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

#ttsTermuxRubyApi::SubSystems::Tts

Returns the ‘tts` subsystem



31
32
33
# File 'lib/termux_ruby_api/base.rb', line 31

def tts
  @tts ||= TermuxRubyApi::SubSystems::Tts.new(self)
end