Module: DTK::Client::Console

Extended by:
CommandBase, CommandHelperMixin, PushCloneChangesMixin, ReparseMixin
Included in:
CloneMixin, CommandBaseThor
Defined in:
lib/util/console.rb

Constant Summary

Constants included from CommandHelperMixin

DTK::Client::CommandHelperMixin::Loaded

Constants included from ReparseMixin

ReparseMixin::YamlDTKMetaFiles

Class Method Summary collapse

Methods included from CommandHelperMixin

Helper

Methods included from CommandBase

get, get_connection, handle_argument_error, post, post_file, rest_url, rotate_args

Methods included from ReparseMixin

reparse_aux

Methods included from PushCloneChangesMixin

push_clone_changes_aux

Class Method Details

.confirmation_prompt(message, add_options = true) ⇒ Object

Display confirmation prompt and repeat message until expected answer is given



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/util/console.rb', line 37

def confirmation_prompt(message, add_options=true)
  # used to disable skip with ctrl+c
  trap("INT", "SIG_IGN")
  message += " (yes|no)" if add_options

  while line = Readline.readline("#{message}: ", true)
    if (line.eql?("yes") || line.eql?("y"))
      trap("INT",false)
      return true
    elsif (line.eql?("no") || line.eql?("n"))
      trap("INT",false)
      return false
    end
  end
end

.confirmation_prompt_additional_options(message, options = []) ⇒ Object

Display confirmation prompt and repeat message until expected answer is given options should be sent as array [‘all’, ‘none’]



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/util/console.rb', line 75

def confirmation_prompt_additional_options(message, options = [])
  raise DTK::Client::DtkValidationError, "Options should be sent as array: ['all', 'none']" unless options.is_a?(Array)

  # used to disable skip with ctrl+c
  trap("INT", "SIG_IGN")
  message += " (yes/no#{options.empty? ? '' : ('/' + options.join('/'))})"

  while line = Readline.readline("#{message}: ", true)
    if line.eql?("yes") || line.eql?("y")
      trap("INT",false)
      return true
    elsif line.eql?("no") || line.eql?("n")
      trap("INT",false)
      return false
    elsif options.include?(line)
      trap("INT",false)
      return line
    end
  end
end

.confirmation_prompt_multiple_choice(message, opts_array) ⇒ Object

multiple choice e.g. Select version to delete:

  1. base

  2. 0.0.1

  3. 0.0.2

  4. all

> 2



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/util/console.rb', line 104

def confirmation_prompt_multiple_choice(message, opts_array)
  indexes = opts_array.map{ |opt| "#{opts_array.index(opt)+1}" }

  trap("INT", "SIG_IGN")

  message << "\n"
  opts_array.each {|opt| message << "#{opts_array.index(opt)+1}. #{opt}\n" }
  message << "> "

  while line = Readline.readline("#{message}", true)
    if indexes.include?(line)
      trap("INT",false)
      return opts_array[line.to_i-1]
    elsif line.eql?('exit')
      return nil
    end
  end
end

.confirmation_prompt_simple(message, add_options = true) ⇒ Object

Display confirmation prompt and repeat message until expected answer is given



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/util/console.rb', line 56

def confirmation_prompt_simple(message, add_options=true)
  # used to disable skip with ctrl+c
  trap("INT", "SIG_IGN")
  message += " (Y/n)" if add_options

  while line = Readline.readline("#{message}: ", true)
    if (line.downcase.eql?("yes") || line.downcase.eql?("y") || line.empty?)
      trap("INT",false)
      return true
    elsif (line.downcase.eql?("no") || line.downcase.eql?("n"))
      trap("INT",false)
      return false
    end
  end
end

.unix_shell(path, module_id, module_type, version = nil) ⇒ Object

Method that will execute until interupted as unix like shell. As input takes path to desire directory from where unix shell can execute normaly.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/util/console.rb', line 158

def unix_shell(path,module_id,module_type,version=nil)

  dtk_shell_ac_proc = Readline.completion_proc
  dtk_shell_ac_append_char = Readline.completion_append_character

  if OsUtil.is_windows?
    puts "[NOTICE] Shell interaction is currenly not supported on Windows."
    return
  end

  begin
    # we need to change path like this since system call 'cd' is not supported
    initial_dir = Dir.pwd
    Dir.chdir(path)
    puts "[NOTICE] You are switching to unix-shell, to path #{path}"

    # prompt = DTK::Client::OsUtil.colorize("$dtk:unix-shell ", :yellow)
    prompt = DTK::Client::OsUtil.colorize("$:", :yellow)

    Readline.completion_append_character = ""
    Readline.completion_proc = Proc.new do |str|
      Dir[str+'*'].grep(/^#{Regexp.escape(str)}/)
    end
    while line = Readline.readline("#{prompt}#{OsUtil.current_dir}>", true)
      begin
        line = line.chomp()
        break if line.eql?('exit')
        # since we are not able to support cd command due to ruby specific restrictions
        # we will be using chdir to this.
        if (line.match(/^cd /))
           # remove cd part of command
          line = line.gsub(/^cd /,'')
          # Get path
          path = line.match(/^\//) ? line : "#{Dir.getwd()}/#{line}"
          # If filepat* with '*' at the end, match first directory and go in it, else try to change original input
          if path.match(/\*$/)
            dirs = Dir[path].select{|file| File.directory?(file)}
            unless dirs.empty?
              Dir.chdir(dirs.first)
              next
            end
          end
          # Change directory
          Dir.chdir(path)
        elsif line.match(/^dtk-push-changes/)
          args       = Shellwords.split(line)
          commit_msg = nil

          unless args.size==1
            raise DTK::Client::DtkValidationError, "To push changes to server use 'dtk-push-changes [-m COMMIT-MSG]'" unless (args[1]=="-m" && args.size==3)
            commit_msg = args.last
          end

          reparse_aux(path)
          push_clone_changes_aux(module_type, module_id, version, commit_msg)
        else
          # we are sending stderr to stdout
          system("#{line} 2>&1")
        end
      rescue Exception => e
        DtkLogger.instance.error_pp(e.message, e.backtrace)
      end
    end
   rescue Interrupt
    puts ""
    # do nothing else
   ensure
    Dir.chdir(initial_dir)
  end

  Readline.completion_append_character = dtk_shell_ac_append_char unless dtk_shell_ac_append_char.nil?
  Readline.completion_proc = dtk_shell_ac_proc unless dtk_shell_ac_proc.nil?
  puts "[NOTICE] You are leaving unix-shell."
end

.wait_animation(message, time_seconds) ⇒ Object

Loading output used to display waiting status



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/util/console.rb', line 124

def wait_animation(message, time_seconds)
  print message
  print " [     ]"
  STDOUT.flush
  time_seconds.downto(1) do
    1.upto(4) do |i|
      next_output = "\b\b\b\b\b\b\b"
      case
       when i % 4 == 0
        next_output  += "[ =   ]"
       when i % 3 == 0
         next_output += "[  =  ]"
       when i % 2 == 0
        next_output  += "[   = ]"
       else
        next_output  += "[  =  ]"
      end

      print next_output
      STDOUT.flush
      sleep(0.25)
    end
  end
  # remove loading animation
  print "\b\b\b\b\b\b\bRefreshing..."
  STDOUT.flush
  puts
end