Class: UniversalPipeHandler::Shell

Inherits:
Base
  • Object
show all
Defined in:
lib/universal_pipe_handler/shell/shell.rb

Overview

UniversalPipeHandler::Shell

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#allowed_actions?, #chdir, #commandline_arguments?, #copy, #first_argument?, #mkdir, #register_sigint, #remove_quotes, #return_pwd, #set_commandline_arguments

Constructor Details

#initialize(commandline_arguments = ARGV, run_already = true) ⇒ Shell

#

initialize

#


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

def initialize(
    commandline_arguments = ARGV,
    run_already           = true
  )
  register_sigint
  consider_enabling_readline_support
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  run if run_already
end

Class Method Details

.[](i = '') ⇒ Object

#

UniversalPipeHandler::Shell[]

#


248
249
250
# File 'lib/universal_pipe_handler/shell/shell.rb', line 248

def self.[](i = '')
  new(i)
end

Instance Method Details

#consider_enabling_readline_supportObject

#

consider_enabling_readline_support

We will enable readline support, if it is available.

The registered actions are part of the sanitize_pipe project.

#


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/universal_pipe_handler/shell/shell.rb', line 76

def consider_enabling_readline_support
  if readline_is_available?
    Readline.completion_append_character = ' '
    completion_proc = proc { |input|
      input = input.to_s.strip
      aliases = all_aliases
      # input = Regexp.escape(input)
      aliases.grep(/^#{input}/)
    }
    Readline.completion_proc = completion_proc
  else
    e 'No support for Readline is available.'
  end
end

#enter_the_main_loopObject

#

enter_the_main_loop (loop tag)

#


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/universal_pipe_handler/shell/shell.rb', line 108

def enter_the_main_loop
  loop {
    _ = fetch_user_input
    case _ # (case tag)
    # ===================================================================== #
    # === aliases?
    # ===================================================================== #
    when 'aliases?'
      show_all_aliases
    # ===================================================================== #
    # === value?
    # ===================================================================== #
    when 'value?',
         'result?'
      show_last_return_value
    # ===================================================================== #
    # === test1
    #
    # This entry point can be used to quickly test the pipe.
    # ===================================================================== #
    when 'test1'
      _ = 'all images | resize 25%'
      set_user_input(_)
    # ===================================================================== #
    # === test2
    #
    # Similar to test1.
    # ===================================================================== #
    when 'test2'
      _ = 'ls | camelCase'
      set_user_input(_)
    # ===================================================================== #
    # === quit
    # ===================================================================== #
    when 'quit',
         'q',
         'exit',
         'd',
         'x'
      verbose_exit
    end
    if _.include? '|' # This is also an implicit "if not empty" check.
      # ================================================================= #
      # All input that contains '|' will be evaluated as a
      # pipe-instruction.
      # ================================================================= #
      evaluate_this_pipe_instruction(_)
    else # Else, by default, evaluate it. (else tag)
      if _.empty?
        e
      else
        e rev+'The instruction '+
          ::Colours.steelblue(_)+
          ' is not registered or understood.'
      end
    end
  }
end

#evaluate_this_pipe_instruction(i) ⇒ Object Also known as: test

#

evaluate_this_pipe_instruction

Usage example:

ls | camelCase
#


229
230
231
232
233
234
235
# File 'lib/universal_pipe_handler/shell/shell.rb', line 229

def evaluate_this_pipe_instruction(i)
  i = i.to_s.dup
e tomato("Debug: `#{i}` in evaluate_this_pipe_instruction")
  # opn; e 'Next testing `'+sfancy(i)+'`.' unless i.empty?
  result = UniversalPipeHandler::CmdletsHandler.new(i)
  @last_result = result.result?
end

#fetch_user_inputObject

#

fetch_user_input

#


170
171
172
173
174
175
176
177
# File 'lib/universal_pipe_handler/shell/shell.rb', line 170

def fetch_user_input
  if readline_is_available?
    set_user_input(Readline.readline('', true))
  else
    set_user_input($stdin.gets.chomp)
  end
  return @user_input
end

#is_readline_available?Boolean Also known as: readline_is_available?

#

is_readline_available?

#

Returns:

  • (Boolean)


65
66
67
# File 'lib/universal_pipe_handler/shell/shell.rb', line 65

def is_readline_available?
  return Object.const_defined? :Readline
end

#resetObject

#

reset (reset tag)

#


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/universal_pipe_handler/shell/shell.rb', line 50

def reset
  super()
  # ======================================================================= #
  # === @namespace
  # ======================================================================= #
  @namespace = NAMESPACE
  # ======================================================================= #
  # === @last_result
  # ======================================================================= #
  @last_result = nil
end

#runObject

#

run (run tag)

#


240
241
242
243
# File 'lib/universal_pipe_handler/shell/shell.rb', line 240

def run
  show_startup_message
  enter_the_main_loop
end

#set_user_input(i) ⇒ Object

#

set_user_input

#


182
183
184
# File 'lib/universal_pipe_handler/shell/shell.rb', line 182

def set_user_input(i)
  @user_input = i
end

#show_all_aliasesObject

#

show_all_aliases

#


94
95
96
# File 'lib/universal_pipe_handler/shell/shell.rb', line 94

def show_all_aliases
  pp UniversalPipeHandler.pipe_aliases?.keys.sort
end

#show_last_return_valueObject

#

show_last_return_value

#


101
102
103
# File 'lib/universal_pipe_handler/shell/shell.rb', line 101

def show_last_return_value
  e @last_result
end

#show_startup_messageObject

#

show_startup_message

#


211
212
213
214
215
216
217
218
219
# File 'lib/universal_pipe_handler/shell/shell.rb', line 211

def show_startup_message
  e ::Colours.rev+
    'Welcome to the interactive Shell for the '+
    ::Colours.steelblue('UniversalPipeHandler')+
    ' project.'
  e
  e silver('Input your pipe-instruction next to see it be evaluated:')
  e
end

#silver(i) ⇒ Object

#

silver

#


204
205
206
# File 'lib/universal_pipe_handler/shell/shell.rb', line 204

def silver(i)
  ::Colours.silver(i)
end

#user_input?Boolean

#

user_input?

#

Returns:

  • (Boolean)


189
190
191
# File 'lib/universal_pipe_handler/shell/shell.rb', line 189

def user_input?
  @user_input.to_s.strip
end

#verbose_exitObject

#

verbose_exit

#


196
197
198
199
# File 'lib/universal_pipe_handler/shell/shell.rb', line 196

def verbose_exit
  e 'Bye.'
  exit
end