Module: Opn

Defined in:
lib/opn/colours.rb,
lib/opn/module.rb,
lib/opn/version/version.rb

Overview

#

Constant Summary collapse

VERSION =
#

Opn::VERSION

#
'1.0.53'
LAST_UPDATE =
#

Opn::LAST_UPDATE

#
'20.01.2020'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.be_verbose?Boolean

#

Opn.be_verbose?

#

Returns:

  • (Boolean)

20
21
22
# File 'lib/opn/module.rb', line 20

def self.be_verbose?
  @be_verbose
end

.disable_coloursObject

#

Opn.disable_colours

We can disable colours too.

#

26
27
28
# File 'lib/opn/colours.rb', line 26

def self.disable_colours
  @use_colours = false
end

.namespace(input, optional_extra_arguments = nil) ⇒ Object

#

Opn.namespace

This is a simplified variant, if you just need the namespace.

If you need more flexibility, then use Opn.opn() directly - it will support more arguments.

#

32
33
34
35
36
# File 'lib/opn/module.rb', line 32

def self.namespace(
    input, optional_extra_arguments = nil
  )
  Opn.opn(namespace: input) { optional_extra_arguments }
end

.opn(use_colours = Opn.use_colours?, be_verbose = be_verbose?, , use_this_as_namespace = '') ⇒ Object

#

Opn.opn

This is the main method of this project.

If we wish to be silent or set other options, we can do this:

Opn.opn :be_silent
Opn.opn :no_newline
Opn.opn :no_colours
Opn.opn(namespace: 'YoThere')
Opn.opn(namespace: '')
Opn.opn(namespace: 'YoThere', be_verbose: false)

Since as of September 2017, we can also pad the output if a key called :padding is passed. The argument should be like this:

padding: 18

This means to use up to 18 characters, padding via ' '.

#

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
100
101
102
103
104
105
106
107
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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/opn/module.rb', line 59

def self.opn(
    use_colours           = Opn.use_colours?,
    be_verbose            = be_verbose?,
    use_this_as_namespace = ''
  )
  # ======================================================================= #
  # === Default values
  #
  # First, some default variables for intrinsic behaviour of this method.
  # ======================================================================= #
  shall_we_append_a_colon_character = true # By default, we append ':'.
  append_newline = false
  use_padding = false
  may_we_display_anything = true # If true then we will show output.

  # ======================================================================= #
  # Hardcoded colours follow next.
  # ======================================================================= #
  grey  = "\e[1;30m"
  green = "\e[0;32m"
  # ======================================================================= #
  # Next keep a link towards the colour that we will use.
  # ======================================================================= #
  use_this_colour = grey
  reset_terminal = "\e[0;37m"

  # ======================================================================= #
  # Work on the first argument given to this method next:
  # ======================================================================= #
  case use_colours
  when :default
    use_colours = Opn.use_colours?
  when :use_colours
    use_colours = true
  when :no_colours
    use_colours = false
    be_verbose  = be_verbose?
  when :no_newline # This is the default anyway.
  when :newline
    append_newline = true
  when :be_silent, :be_quiet
    use_colours = @use_colours # This is the default. We restore it here.
    be_verbose  = false
  when :be_verbose
    use_colours = Opn.use_colours?
    be_verbose  = be_verbose?
  end
  case be_verbose
  when nil
    be_verbose = be_verbose?
  when :be_quiet
    be_verbose = false
  end
  # ======================================================================= #
  # use_this_as_namespace = File.basename(__FILE__) # Could use this here.
  # use_this_as_namespace = caller[0]
  # ======================================================================= #
  the_caller = caller
  if use_this_as_namespace.empty? # This is the default case then.
    if the_caller.size > 1
      use_this_as_namespace = the_caller[1]
    else
      use_this_as_namespace = the_caller.first
    end
  end
  # ======================================================================= #
  # === use_colours
  #
  # Next, we will handle the case when use_colours is a Hash. This may
  # quite often be the case, as a Hash is more flexible than most other
  # "types" in Ruby.
  #
  # This can be used for e. g. namespace: 'RBT::Compile'
  # ======================================================================= #
  if use_colours.is_a? Hash
    copy = use_colours.dup # Keep a reference copy here.
    # =================================================================== #
    # === :append_colon
    # =================================================================== #
    if use_colours.has_key? :append_colon
      shall_we_append_a_colon_character = use_colours.delete(:append_colon)
    end
    if use_colours.has_key? :use_this_as_namespace
      use_this_as_namespace = use_colours.fetch(:use_this_as_namespace)
    elsif use_colours.has_key? :use_this_namespace
      use_this_as_namespace = use_colours.fetch(:use_this_namespace)
    elsif use_colours.has_key? :namespace
      use_this_as_namespace = use_colours.fetch(:namespace)
    end
    if use_colours.has_key? :use_opn
      may_we_display_anything = use_colours.fetch(:use_opn)
    end
    if use_colours.has_key? :be_verbose
      be_verbose = use_colours.delete(:be_verbose)
    end
    use_colours = true # Restore the default again here, since it was a Hash before.
    # ===================================================================== #
    # Check for more keys though:
    # ===================================================================== #
    if copy.has_key? :use_colours
      use_colours = copy.delete(:use_colours)
    end
    # ===================================================================== #
    # === :trailing_colon
    # ===================================================================== #
    if copy.has_key? :trailing_colon
      shall_we_append_a_colon_character = copy.delete(:trailing_colon)
    # ===================================================================== #
    # === :no_trailing
    # ===================================================================== #
    elsif copy.has_key? :no_trailing
      shall_we_append_a_colon_character = copy.delete(:no_trailing)
    end
    # ===================================================================== #
    # === Check for :padding value
    # ===================================================================== #
    if copy.has_key? :padding
      use_padding = true
      use_this_padding = copy.delete(:padding).to_i # Must be an Integer.
    end
  end
  # ======================================================================= #
  # Check for Hash as namespace.
  # ======================================================================= #
  if use_this_as_namespace.is_a?(Hash) and use_this_as_namespace.has_key?(:namespace)
    use_this_as_namespace = use_this_as_namespace.delete(:namespace)
  end
  # ======================================================================= #
  # Next we split on the namespace, but this may be problematic when
  # our input contains more than one ':' such as 'RBT::Compile'.
  # In this case, we assume that the user is smarter than we are,
  # and we will NOT split on such input provided.
  # ======================================================================= #
  if use_this_as_namespace.is_a? String
    unless use_this_as_namespace.include? '::' # ^^^ See above for the explanation.
      use_this_as_namespace = File.basename(use_this_as_namespace.split(':').first)
    end
    if use_this_as_namespace.include? '_' # Assume Camelcase provided here.
      use_this_as_namespace = use_this_as_namespace.split('_').map(&:capitalize).join
    else # Else upcase the first argument always.
      if use_this_as_namespace.frozen?
        use_this_as_namespace = use_this_as_namespace.dup
      end
      use_this_as_namespace[0,1] = use_this_as_namespace[0,1].upcase
    end
    # ======================================================================= #
    # Do some sanitizing next, by getting rid of .rb entries.
    # ======================================================================= #
    use_this_as_namespace = use_this_as_namespace.sub(/\.rb/,'')
  end
  # ======================================================================= #
  # Handle blocks next.
  # ======================================================================= #
  if block_given?
    yielded = yield
    case yielded
    when :no_trailing_colon,
         :no_trailing_character,
         :no_trailing_char,
         :no_trailing,
         :no_colon,
         :no_semicolon,
         :short
      shall_we_append_a_colon_character = false
    when :show_only_the_name_of_the_class, :display_only_last_name
      # =================================================================== #
      # In this case, show only the name of the main class. But only
      # if the namespace includes a '::'.
      # =================================================================== #
      if use_this_as_namespace.include? '::'
        use_this_as_namespace = use_this_as_namespace.split('::').last
      end
    # ===================================================================== #
    # === :no_colours
    # ===================================================================== #
    when :no_colours, :disable_colours
      Opn.disable_colours
    # ===================================================================== #
    # Next, we ought to handle Hashes passed to this method via a block.
    # ===================================================================== #
    when Hash
      # =================================================================== #
      # === :append_colon
      # =================================================================== #
      if yielded.has_key? :append_colon
        shall_we_append_a_colon_character = yielded.delete(:append_colon)
      end
      if    yielded.has_key?(:use_this_as_namespace)
        use_this_as_namespace = yielded.delete :use_this_as_namespace
      elsif yielded.has_key?(:use_this_namespace)
        use_this_as_namespace = yielded.delete :use_this_namespace
      elsif yielded.has_key?(:namespace)
        use_this_as_namespace = yielded.delete :namespace
      elsif yielded.has_key?(:trailing_colon)
        shall_we_append_a_colon_character = yielded.delete(:trailing_colon)
      elsif yielded.has_key?(:use_colours)
        use_colours = yielded.delete(:use_colours)
      end
      if yielded.has_key? :use_this_colour
        use_this_colour = return_escape_sequence_for_this_colour(
          yielded[:use_this_colour]
        )
      end
    end
  end
  _ = ''.dup
  if use_colours
    _ << use_this_colour
  end
  # ======================================================================= #
  # Determine whether we shall append a colon character next:
  # ======================================================================= #
  if shall_we_append_a_colon_character
    unless use_this_as_namespace.end_with? '' # The → is a general exception.
      use_this_as_namespace << ':'
    end
  end
  use_this_as_namespace << ' ' if use_this_as_namespace.end_with? ':'
  # ======================================================================= #
  # Honour padding-operations onto the main Namespace next.
  # ======================================================================= #
  if use_padding
    use_this_as_namespace = use_this_as_namespace.ljust(use_this_padding)
  end
  # ======================================================================= #
  # Add the separator here.
  # ======================================================================= #
  _ << use_this_as_namespace
  _ << green+reset_terminal if use_colours
  _ << "\n" if append_newline
  if may_we_display_anything
    print _ if be_verbose
  end
  return _
end

.opne(i = '') ⇒ Object

#

Opn.opne

To test this, do:

Opn.opne 'Hello world!'
#

301
302
303
304
# File 'lib/opn/module.rb', line 301

def self.opne(i = '')
  Opn.opn
  puts i
end

.return_escape_sequence_for_this_colour(i = :green) ⇒ Object

#

Opn.return_escape_sequence_for_this_colour

This method can return the proper colour code.

#

318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/opn/module.rb', line 318

def self.return_escape_sequence_for_this_colour(
    i = :green
  )
  i = i.to_sym
  case i
  when :grey
    i = "\e[0;30m"
  when :green
    i = "\e[0;32m"
  end
  i
end

.use_colours?Boolean

#

Opn.use_colours?

#

Returns:

  • (Boolean)

17
18
19
# File 'lib/opn/colours.rb', line 17

def self.use_colours?
  @use_colours
end

Instance Method Details

#opne(i = '') ⇒ Object

#

opne

#

309
310
311
# File 'lib/opn/module.rb', line 309

def opne(i = '')
  Opn.opne(i)
end