Class: Inputreceiver::Inputreceiver

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

Instance Method Summary collapse

Constructor Details

#initialize(t = 'Enter text') ⇒ Inputreceiver

Returns a new instance of Inputreceiver.



21
22
23
# File 'lib/inputreceiver.rb', line 21

def initialize(t = 'Enter text')
  @title = @prompt = t
end

Instance Method Details

#browser_js?Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
# File 'lib/inputreceiver.rb', line 107

def browser_js?
  `document`
  true
rescue Exception
  false
end

#get_input_from_browserObject



101
102
103
104
105
# File 'lib/inputreceiver.rb', line 101

def get_input_from_browser
  true; require "native"
  puts 'Democracy barr'
  $$.get_democracy_bar_input
end

#modeObject



29
30
31
# File 'lib/inputreceiver.rb', line 29

def mode
  'zenity'
end

#node?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
# File 'lib/inputreceiver.rb', line 114

def node?
  `process`
  true
rescue Exception
  false
end

#receive_inputObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/inputreceiver.rb', line 52

def receive_input

  if ARGV[0].in? %w[mode modes]
    n = 0
    puts "Input modes\n".bold
    puts %w[browser-input stdin zenity dialog kdialog readline].sort.map {
           "  #{n += 1}. #{_1}"
         }.join("\n")
    puts
    exit
  end
  speak_input

  if browser_js? || (ARGV[0] == 'browser-js')
    get_input_from_browser
  elsif ARGV[0] != 'stdin' && ARGV[0] == 'kdialog' && which('kdialog')
    if RUBY_ENGINE != 'opal'
      `kdialog --inputbox #{@prompt.shellescape} --title #{@title.shellescape}`.strip
    end
  elsif ARGV[0] != 'stdin' && ARGV[0] == 'zenity' && which('zenity')
    if RUBY_ENGINE != 'opal'
      `zenity --entry --text #{@prompt.shellescape} --title #{@title.shellescape}`.strip
    end
  elsif ARGV[0] != 'stdin' && ARGV[0] == 'dialog' && which('dialog')
    eval 'require "tempfile"'
    text = ''
    Tempfile.open do |file|
    #puts "dialog --inputbox #{@title.shellescape} 0 0 2> #{file.to_path.shellescape}"
    system "dialog --inputbox #{@title.shellescape} 0 0 2> #{file.to_path.shellescape}"
      text = File.read(file)
    end
    text
  elsif (ARGV[0] == 'stdin') || (RUBY_ENGINE == 'opal')
    true; require "opal/platform"
            puts @title
#        eval ("require opal/native")
    q = $stdin.readline.split("\n")

    q.first.to_s
  elsif ARGV[0].in? %w[foo bar]
    'foo'.puts

  else
    puts @title
    eval 'require "readline"'
    Readline.readline.strip
  end
end

#ruby?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/inputreceiver.rb', line 121

def ruby?
  true
end

#speak_inputObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/inputreceiver.rb', line 33

def speak_input
  if RUBY_PLATFORM == 'opal'
    return
  else
  end

  a = "echo #{@prompt} | #{voice = %w[flite espeak festival cat].which}  "
  voice = File.basename voice
  b = { 'festival' => '--tts', 'flite' => '-voice slt', 'espeak' => '' }

  a += b[voice]

  spawn a
end

#to_sObject



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

def to_s
  @text
end

#which(a) ⇒ Object



48
49
50
# File 'lib/inputreceiver.rb', line 48

def which(a)
  [a].which
end