module Opn
#¶ ↑
#¶ ↑
Constants
- LAST_UPDATE
#¶ ↑
LAST_UPDATE
¶ ↑#¶ ↑
- VERSION
#¶ ↑
VERSION
¶ ↑#¶ ↑
Public Class Methods
be_verbose?()
click to toggle source
cadetblue(i)
click to toggle source
disable_colours()
click to toggle source
namespace( input, optional_extra_arguments = nil )
click to toggle source
#¶ ↑
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.
#¶ ↑
# 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 = '' ) { || ... }
click to toggle source
#¶ ↑
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 ‘ ’.
#¶ ↑
# 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 = '')
click to toggle source
return_escape_sequence_for_this_colour( i = :green )
click to toggle source