Module: UniversalPipeHandler

Includes:
Colours
Defined in:
lib/universal_pipe_handler/base/base.rb,
lib/universal_pipe_handler/cmdlets/add.rb,
lib/universal_pipe_handler/cmdlets/all.rb,
lib/universal_pipe_handler/cmdlets/any.rb,
lib/universal_pipe_handler/cmdlets/cut.rb,
lib/universal_pipe_handler/shell/shell.rb,
lib/universal_pipe_handler/cmdlets/crop.rb,
lib/universal_pipe_handler/cmdlets/days.rb,
lib/universal_pipe_handler/cmdlets/help.rb,
lib/universal_pipe_handler/cmdlets/size.rb,
lib/universal_pipe_handler/cmdlet/cmdlet.rb,
lib/universal_pipe_handler/cmdlets/years.rb,
lib/universal_pipe_handler/cmdlets/assign.rb,
lib/universal_pipe_handler/cmdlets/random.rb,
lib/universal_pipe_handler/cmdlets/resize.rb,
lib/universal_pipe_handler/cmdlets/select.rb,
lib/universal_pipe_handler/cmdlets/to_dna.rb,
lib/universal_pipe_handler/cmdlets/to_pdf.rb,
lib/universal_pipe_handler/cmdlets/extract.rb,
lib/universal_pipe_handler/cmdlets/install.rb,
lib/universal_pipe_handler/cmdlets/n_files.rb,
lib/universal_pipe_handler/cmdlets/n_words.rb,
lib/universal_pipe_handler/cmdlets/reverse.rb,
lib/universal_pipe_handler/cmdlets/seconds.rb,
lib/universal_pipe_handler/cmdlets/shuffle.rb,
lib/universal_pipe_handler/colours/colours.rb,
lib/universal_pipe_handler/dataset/dataset.rb,
lib/universal_pipe_handler/project/project.rb,
lib/universal_pipe_handler/version/version.rb,
lib/universal_pipe_handler/cmdlets/download.rb,
lib/universal_pipe_handler/cmdlets/duration.rb,
lib/universal_pipe_handler/cmdlets/find_all.rb,
lib/universal_pipe_handler/cmdlets/identify.rb,
lib/universal_pipe_handler/cmdlets/pad_left.rb,
lib/universal_pipe_handler/cmdlets/position.rb,
lib/universal_pipe_handler/cmdlets/to_ascii.rb,
lib/universal_pipe_handler/cmdlets/to_movie.rb,
lib/universal_pipe_handler/cmdlets/write_to.rb,
lib/universal_pipe_handler/cmdlets/add_audio.rb,
lib/universal_pipe_handler/cmdlets/colourize.rb,
lib/universal_pipe_handler/cmdlets/pad_right.rb,
lib/universal_pipe_handler/cmdlets/processes.rb,
lib/universal_pipe_handler/cmdlets/read_file.rb,
lib/universal_pipe_handler/cmdlets/read_line.rb,
lib/universal_pipe_handler/cmdlets/stat_file.rb,
lib/universal_pipe_handler/cmdlets/translate.rb,
lib/universal_pipe_handler/cmdlets/upload_to.rb,
lib/universal_pipe_handler/cmdlets/word_wrap.rb,
lib/universal_pipe_handler/colours/colourize.rb,
lib/universal_pipe_handler/cmdlets/convert_to.rb,
lib/universal_pipe_handler/cmdlets/screenshot.rb,
lib/universal_pipe_handler/cmdlets/show_lines.rb,
lib/universal_pipe_handler/cmdlets/word_count.rb,
lib/universal_pipe_handler/toplevel_methods/e.rb,
lib/universal_pipe_handler/cmdlets/ascii_video.rb,
lib/universal_pipe_handler/cmdlets/decolourize.rb,
lib/universal_pipe_handler/cmdlets/extract_all.rb,
lib/universal_pipe_handler/cmdlets/match_regex.rb,
lib/universal_pipe_handler/cmdlets/remove_html.rb,
lib/universal_pipe_handler/cmdlets/shuffle_csv.rb,
lib/universal_pipe_handler/cmdlets/starts_with.rb,
lib/universal_pipe_handler/constants/constants.rb,
lib/universal_pipe_handler/cmdlets/number_lines.rb,
lib/universal_pipe_handler/cmdlets/random_video.rb,
lib/universal_pipe_handler/cmdlets/read_n_lines.rb,
lib/universal_pipe_handler/cmdlets/remove_audio.rb,
lib/universal_pipe_handler/cmdlets/repackage_to.rb,
lib/universal_pipe_handler/cmdlets/resize_image.rb,
lib/universal_pipe_handler/cmdlets/sort_by_date.rb,
lib/universal_pipe_handler/toplevel_methods/rds.rb,
lib/universal_pipe_handler/cmdlets/extract_audio.rb,
lib/universal_pipe_handler/cmdlets/extract_video.rb,
lib/universal_pipe_handler/cmdlets/get_all_files.rb,
lib/universal_pipe_handler/cmdlets/n_directories.rb,
lib/universal_pipe_handler/cmdlets/to_camel_case.rb,
lib/universal_pipe_handler/toplevel_methods/misc.rb,
lib/universal_pipe_handler/cmdlets/capture_screen.rb,
lib/universal_pipe_handler/cmdlets/convert_to_mp3.rb,
lib/universal_pipe_handler/cmdlets/convert_to_wav.rb,
lib/universal_pipe_handler/cmdlets/get_all_images.rb,
lib/universal_pipe_handler/cmdlets/increase_audio.rb,
lib/universal_pipe_handler/cmdlets/remove_numbers.rb,
lib/universal_pipe_handler/cmdlets/search_torrent.rb,
lib/universal_pipe_handler/toplevel_methods/token.rb,
lib/universal_pipe_handler/cmdlets/count_character.rb,
lib/universal_pipe_handler/cmdlets/generate_string.rb,
lib/universal_pipe_handler/cmdlets/open_in_browser.rb,
lib/universal_pipe_handler/cmdlets/remove_comments.rb,
lib/universal_pipe_handler/cmdlets/remove_newlines.rb,
lib/universal_pipe_handler/cmdlets/copy_directories.rb,
lib/universal_pipe_handler/cmdlets/download_torrent.rb,
lib/universal_pipe_handler/cmdlets/convert_to_images.rb,
lib/universal_pipe_handler/cmdlets/count_longest_row.rb,
lib/universal_pipe_handler/cmdlets/sort_alphabetical.rb,
lib/universal_pipe_handler/cmdlets/remove_directories.rb,
lib/universal_pipe_handler/cmdlets/get_all_audio_files.rb,
lib/universal_pipe_handler/cmdlets/get_all_video_files.rb,
lib/universal_pipe_handler/cmdlets/get_last_characters.rb,
lib/universal_pipe_handler/cmdlets/replace_underscores.rb,
lib/universal_pipe_handler/configuration/configuration.rb,
lib/universal_pipe_handler/toplevel_methods/all_actions.rb,
lib/universal_pipe_handler/cmdlets/generate_random_video.rb,
lib/universal_pipe_handler/cmdlets/read_n_lines_inverted.rb,
lib/universal_pipe_handler/cmdlets_handler/cmdlets_handler.rb,
lib/universal_pipe_handler/toplevel_methods/cmdlet_directory.rb,
lib/universal_pipe_handler/cmdlets/get_all_images_including_subdirs.rb,
lib/universal_pipe_handler/requires/do_require_the_individual_cmdlet_files.rb

Overview

#

require ‘universal_pipe_handler/requires/do_require_the_individual_cmdlet_files.rb’

#

Defined Under Namespace

Classes: Base, Cmdlet, CmdletsHandler, Colourize, Shell

Constant Summary collapse

ALSO_OPEN_IN_PDF_VIEWER =
#

ALSO_OPEN_IN_PDF_VIEWER

If true we also open it at once.

#
true
USE_THIS_PDF_VIEWER =
#

USE_THIS_PDF_VIEWER

#
'evince'
PROJECT_BASE_DIRECTORY =
#

PROJECT_BASE_DIRECTORY

#
File.absolute_path("#{__dir__}/..")+'/'
VERSION =
#

VERSION

#
'0.0.10'
LAST_UPDATE =
#

LAST_UPDATE

#
'03.07.2022'
N =
#

N

#
"\n"
PIPE_TOKEN =
#

UniversalPipeHandler::PIPE_TOKEN

The pipe-token to use. Defaults to ‘|’.

#
'|'
FILE_ALLOWED_CMDLETS =
#

UniversalPipeHandler::FILE_ALLOWED_ACTIONS

#
yaml_directory?+'allowed_cmdlets.yml'
ALLOWED_CMDLETS =
YAML.load_file(_)
FILE_PREDEFINED_METHODS =
#

UniversalPipeHandler::FILE_PREDEFINED_METHODS

#
yaml_directory?+'predefined_methods.yml'
FILE_PIPE_ALIASES =
#

UniversalPipeHandler::FILE_PIPE_ALIASES

This constant will refer to a path such as:

/home/x/programming/ruby/src/universal_pipe_handler/lib/universal_pipe_handler/yaml/aliases_to_cmdlets.yml
#
yaml_directory?+'aliases_to_cmdlets.yml'
DEFAULT_PIPE_COMMAND =
#

UniversalPipeHandler::DEFAULT_PIPE_COMMAND

#
'ls | nl'
DEFAULT_PIPE =
#

DEFAULT_PIPE

#
"test 'cat :this_file | wrap_at 76"
PREDEFINED_METHODS =

Empty Hash.

{}
ARRAY_VIDEO_FILES =
#

ARRAY_VIDEO_FILES

All registered video files should be defined here.

#
%w(
  mp4
  avi
  flv
  ogm
  mpg
)
ARRAY_VIDEOFILE_EXTENSION =
ARRAY_VIDEO_FILES
ARRAY_AUDIO_FILES =
#

ARRAY_AUDIO_FILES

All registered audio files should be defined here.

#
%w(
  mp3
  mp4
  ogg
  wav
)
ARRAY_IMAGE_FILE_TYPES =
#

ARRAY_IMAGE_FILE_TYPES

All registered images. 3 so far - .jpg, .png and .gif

#
%w(
  jpg
  png
  gif
  tiff
)
ARRAY_MULTIMEDIA_FILES =
#

ARRAY_MULTIMEDIA_FILES

Conjoint result between the video files and audio files.

#
(
  ARRAY_VIDEO_FILES+
  ARRAY_AUDIO_FILES
).flatten.uniq
ONE_DAY_HAS_N_SECONDS =
#

ONE_DAY_HAS_N_SECONDS

A day has 24 hours, 60 minutes each, and every minute has 60 seconds.

#
24 * 60 * 60
SRC_DIR =
'/tmp/'
SAVE_DIR =
'/home/Temp/universal_pipe_handler/'
CMDLETS_DIRECTORY =
#

CMDLETS_DIRECTORY

Where we store all actions of the pipe paradise projects.

This will be batch-required.

#
PROJECT_BASE_DIRECTORY+'cmdlets/'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_actions?Boolean

#

UniversalPipeHandler.all_actions?

This method will return which actions are registered, and thus, allowed. For this to work all .rb files that are relevant will be loaded.

#

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/universal_pipe_handler/toplevel_methods/all_actions.rb', line 26

def self.all_actions?
  array_all_actions.map {|entry|
    File.basename(
      entry.delete_suffix('.rb')
    )
  }
end

.array_all_actionsObject

#

UniversalPipeHandler.array_all_actions

#


15
16
17
# File 'lib/universal_pipe_handler/toplevel_methods/all_actions.rb', line 15

def self.array_all_actions
  Dir[action_directory?+'*.rb']
end

.cmdlet_add(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_add

#


12
13
# File 'lib/universal_pipe_handler/cmdlets/add.rb', line 12

def self.cmdlet_add(i = result?)
end

.cmdlet_add_audio(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_add_audio

#


12
13
14
15
16
17
18
19
20
# File 'lib/universal_pipe_handler/cmdlets/add_audio.rb', line 12

def self.cmdlet_add_audio(i = result?)
  audio_to_add_audio = i
  video_file  = result?
  output_file = 'new_'+video_file 
  _ = 'ffmpeg -vcodec copy -acodec copy -i '+audio_to_add_audio+' -i '+video_file+' '+output_file
  run_sys_cmd _
  set_result(output_file)
  result?
end

.cmdlet_all(i) ⇒ Object

#

UniversalPipeHandler.cmdlet_all

This action attempts to obtain “all”.

The very first argument (or the first member of the Array, when i is an Array) is the pattern which we will use to match against.

#


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/universal_pipe_handler/cmdlets/all.rb', line 17

def self.cmdlet_all(i)
  _ = action_get_all_files
  if i.is_a? Array
    i.pop if i.last == 'files' # Discard "files" entry if it is the last entry.
  end
  if i.is_a? Array
    i = i.first
  end

  case i
  when 'files'
  else
    match_to_these_file_endings = i 
    _.reject! {|entry| entry !~ /#{match_to_these_file_endings}$/ }
  end

  set_result(_)
  return result?
end

.cmdlet_any(i = 'avi') ⇒ Object

#

UniversalPipeHandler.cmdlet_any (any tag)

Fetch any single file.

#


14
15
16
17
18
19
20
21
22
23
# File 'lib/universal_pipe_handler/cmdlets/any.rb', line 14

def self.cmdlet_any(i = 'avi')
  to_match_against = i
  _ = action_get_all_files
  _.reject! {|file| file !~ /#{to_match_against}$/ }
  if ! _.empty?
    _ = _.sample # Return the first element.
  end
  set_result( _)
  return result?
end

.cmdlet_ascii_video(i = result?, , play_how = :libcaca) ⇒ Object

#

UniversalPipeHandler.cmdlet_ascii_video

Play a video through the aalib. We may however had also default to libcaca, which I am trying since March 2015.

#


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/universal_pipe_handler/cmdlets/ascii_video.rb', line 15

def self.cmdlet_ascii_video(
    i        = result?,
    play_how = :libcaca
  )
  i = i.first if i.is_a? Array
  case play_how
  when :libcaca
    _ = "mplayer -vo caca #{i}"
  else # Default to allib then.
    _ = "mplayer -vo aa #{i}"
  end
  esystem(_)
  set_result
end

.cmdlet_assign(i) ⇒ Object

#

UniversalPipeHandler.cmdlet_assign

Simply assign something here.

#


14
15
16
17
18
19
20
21
22
# File 'lib/universal_pipe_handler/cmdlets/assign.rb', line 14

def self.cmdlet_assign(i)
  if i =~ /^\d+$/ # User did input a number only.
    i = Dir['*'][i.to_i - 1]
  end unless File.exist? i
  i = sanitize_url(i) if i.start_with? ':' # assume it is a special keyword.
  set_file(i)   # We also keep track of the file here.
  set_result(i) # This is our assignment-method for the @result variable.
  return result?
end

.cmdlet_capture_screen(optional_how_many_seconds = nil) ⇒ Object

#

UniversalPipeHandler.cmdlet_capture_screen

Make a screencast with this method. The argument it accepts will break after n seconds, if given. Otherwise endless.

A typical example for this would look like:

ffmpeg -t 00:00:02 -f x11grab -y -r 12 -s 800x600 -i :0.0+480,200 -vcodec ffv1 -sameq out.avi
#


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/universal_pipe_handler/cmdlets/capture_screen.rb', line 20

def self.cmdlet_capture_screen(
    optional_how_many_seconds = nil
  )
  # output_file = '$MY_TEMP/GRABBED_X11_screencast.mov'
  output_file = 'captured_screencast-'+return_date+'_'+get_time(':')+'.avi'
  set_file(output_file) # Now we can use @file.
  capture_screen = CaptureScreen.new :dont_run_yet
  capture_screen.overwrite_if_the_file_already_exists
  capture_screen.framerate 25
  capture_screen.size '1280x720'
  # capture_screen.size '1024x800'
  # capture_screen.size '800x600'
  capture_screen.vcodec 'ffv1'
  capture_screen.sameq
  capture_screen.no_threads
  capture_screen.store_here = file?
  if optional_how_many_seconds
    twenty_four = TwentyfourHoursNotation.new
    # Must convert the seconds into the long-variant. This is done
    # By the TwentyfourHoursNotation class.
    capture_screen.set_duration( twenty_four.simple_format(optional_how_many_seconds) )
  end
  capture_screen.run
  set_result(file?)
end

.cmdlet_colourize(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_colourize

This instruction handles colourization; the method can be used specifically to colourize the given input.

Before September 2012 we used an internal class for this, however since as of September 2012 we will prefer to use CodeRay instead, and optionally fallback to the Colourize class in the event that CodeRay is unavailable.

#


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/universal_pipe_handler/cmdlets/colourize.rb', line 20

def self.cmdlet_colourize(i = result?)
  # If Coderay is available, use that.
  if Object.const_defined? :CodeRay
    if i.is_a? Array
      i = i.join # Won't need to join with newlines here.
    end
    _ = CodeRay.scan(i, :ruby).term # This will become a String.
  else # Else use the project-internal Colourize class, which resides under addons/colours.rb.  
    _ = Colourize.new(i) # bl $PIPE_HANDLER/addons/colourize.rb
    _ = _.string
  end
  set_result(_) # Better keep it in array form.
  return result?
end

.cmdlet_convert_to(which_format) ⇒ Object

#

UniversalPipeHandler.cmdlet_convert_to

This could in principle convert between different formats.

#


14
15
16
17
18
19
# File 'lib/universal_pipe_handler/cmdlets/convert_to.rb', line 14

def self.cmdlet_convert_to(which_format)
  case which_format
  when 'wav'
    set_result(MultimediaParadise.mp3_to_wav(@target_file))
  end
end

.cmdlet_convert_to_images(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_convert_to_images

This action converts a video file to images.

#


14
15
# File 'lib/universal_pipe_handler/cmdlets/convert_to_images.rb', line 14

def self.cmdlet_convert_to_images(i = result?)
end

.cmdlet_convert_to_mp3Object

#

UniversalPipeHandler.cmdlet_convert_to_mp3

This will convert into .mp3. If the input file already is a .mp3 file then we won’t have to do a conversion.

#


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/universal_pipe_handler/cmdlets/convert_to_mp3.rb', line 15

def self.cmdlet_convert_to_mp3
  e 'Now converting all found audio files to .wav format.'
  set_result result?.map { |entry|
    if entry.include? '.ogg'
      MultimediaParadise::MultimediaConversions.ogg_to_mp3(entry)
    elsif entry.include? '.wav'
      MultimediaParadise::MultimediaConversions.wav_to_mp3(entry)
    else
      entry
    end
  }
end

.cmdlet_convert_to_wavObject

#

UniversalPipeHandler.cmdlet_convert_to_wav

#


12
13
14
15
# File 'lib/universal_pipe_handler/cmdlets/convert_to_wav.rb', line 12

def self.cmdlet_convert_to_wav
  e 'Now converting all found audio files to .wav format.' if be_verbose?
  result?.each { |entry| MultimediaParadise.mp3_to_wav(entry) }
end

.cmdlet_copy_directories(i = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_copy_directories

This method will simply copy directories. We tap into the method return_directories() which is defined in shared.rb

#


15
16
17
18
19
20
21
# File 'lib/universal_pipe_handler/cmdlets/copy_directories.rb', line 15

def self.cmdlet_copy_directories(
    i = result?
  )
  @we_want_to_copy_something = true
  set_result(return_directories)
  return result?
end

.cmdlet_count_character(this_char = :all) ⇒ Object

#

UniversalPipeHandler.cmdlet_count_character

Count how many occurences we can find of a specific character.

That character should be passed to this method.

#


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/universal_pipe_handler/cmdlets/count_character.rb', line 16

def self.cmdlet_count_character(
    this_char = :all
  )
  _ = result? # Work on a copy of @result here.
  case this_char
  when :all
    _ = _.size
  else
    _ = _.join if _.is_a? Array
    _ = _.scan(/#{this_char}/).size
  end
  set_result(_)
  return result?
end

.cmdlet_count_longest_rowObject

#

UniversalPipeHandler.cmdlet_count_longest_row

#


12
13
14
15
16
17
18
19
# File 'lib/universal_pipe_handler/cmdlets/count_longest_row.rb', line 12

def self.cmdlet_count_longest_row
  n_number = 0
  result?.each {|_|
    n_number = _.size unless n_number > _.size
  }
  set_result(n_number)
  return result?
end

.cmdlet_crop(crop_command_to_use = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_crop

This method can be used to crop an image.

#


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/universal_pipe_handler/cmdlets/crop.rb', line 14

def self.cmdlet_crop(
    crop_command_to_use = result?
  )
  filename = result?
  crop = ImageParadise::Crop.new(
    filename, crop_command_to_use, :dont_run_yet
  )
  crop.run
  set_result(crop.result?)
  return result?
end

.cmdlet_cut(start_position = result?, , duration_in_n_seconds = nil) ⇒ Object

#

UniversalPipeHandler.cmdlet_cut (cut tag)

This action allows you to cut a video or audio file.

The input can be as complex as “20%-50%”.

#


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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/universal_pipe_handler/cmdlets/cut.rb', line 16

def self.cmdlet_cut(
    start_position        = result?,
    duration_in_n_seconds = nil
  )
  # To prepare for the duration. bl $RUBY_TIME/start_length_duration.rb
  start_length_duration = MultimediaParadise::StartLengthDuration.new
  start_position = start_position.first if start_position.is_a? Array # Trying this since Dec 2014.
  # original_input = start_position.dup
  _ = Multimedia::FileDuration.new(result?) # This will contain the filename.
  # ======================================================================= #
  # === Handle percent-input next
  #
  # This can be either -30 or 30. If we count more than one %, we treat the
  # whole instruction as a range.
  # ======================================================================= #
  if start_position.include?('%')
    # ===================================================================== #
    # Enter a %-Range next:
    # ===================================================================== #
    if start_position.count('%') > 1 # This here is valid if we have something like "30%-80%"
      splitted = start_position.split('%')
      end_pos_in_percent = splitted[-1].to_f.abs
      duration_in_n_seconds = _.one_percent * end_pos_in_percent
    else # else we have just one %.
      start_position = start_position.to_i.abs
    end
  end
  length = _.file_duration # Get the absolute length of the file here.
  # ========================================================================= #
  # We set the total length of the file here. This is useful because
  # we can use this information to calculate extra things.
  # ========================================================================= #
  start_length_duration.length = length
  this_file = result?.first
  e 'The total length of the file '+sfile(this_file)+' is '+
    simportant(length.to_s)+' seconds.'
  _ = 'ffmpeg -y -i '+this_file # Build our sys string here. -y so we overwrite our string.
  # ========================================================================= #
  # The variable save_to will keep track of where we will store the newly
  # generated file (in other words, the output file).
  # ========================================================================= #
  save_to = start_dir?+
            File.basename(this_file.gsub(/\.mp3/,''))+'_ouput'
  # ========================================================================= #
  # Hack, hack hackety hack.
  # ========================================================================= #
  if File.extname(this_file) == '.mp4'
    save_to << '.mp4'
  else
    save_to << '.mp3'
  end
  unless duration_in_n_seconds
    duration_in_n_seconds = start_position.to_i 
  end
  start_position = 0 # Default.
  # ========================================================================= #
  # If @position has been set, then we will add the position both to the
  # start position, and to the end position.
  # ========================================================================= #
  if @position
    start_position = start_position.to_i + @position
  end
  # ========================================================================= #
  # Use StartLengthDuration now to get the required info.
  # ========================================================================= #
  start_length_duration.start        = start_position
  start_length_duration.set_duration = duration_in_n_seconds
  _ << ' -ss '+start_length_duration.start.to_s
  _ << ' -t '+(start_length_duration.duration).to_s
  # ========================================================================= #
  # The problem with -acodec copy is that an AAC file can not be easily 
  # copied into a .mp3 file. Hence we must use -acodec libmp3lame
  # instead here.
  # ========================================================================= #
  if save_to.include? 'mp3'
    _ << ' -acodec libmp3lame '
  else
    _ << ' -acodec copy '
  end
  _ << save_to
  esystem "  #{_}"
  set_result(save_to)
  result?
end

.cmdlet_days?(i = result?) ) ⇒ Boolean

#

UniversalPipeHandler.cmdlet_days?

The input should be amount of seconds.

However had, this is context-dependent and thus a bit difficult to handle correctly.

For instance, take these two different inputs:

'5 years | days?'
'45 days | n_seconds?'

The first variant obviously gives us n seconds to days?, and then days? can output the amount of days.

The second variant should however had simply give us the amount of seconds that 45 days have.

The convention thus will be that we will always convert into seconds, and when it is fed into n_seconds? then we will not do any further modification.

#

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/universal_pipe_handler/cmdlets/days.rb', line 32

def self.cmdlet_days?(i = result?)
  i = i.join.strip if i.is_a? Array
  # result = i.to_f / ONE_DAY_HAS_N_SECONDS.to_f
  # i = result.floor
  set_result(i)
  return result?
end

.cmdlet_decolourize(this_file = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_decolourize

This method will get rid of the colours from a video file.

#


14
15
16
17
18
19
20
21
22
23
# File 'lib/universal_pipe_handler/cmdlets/decolourize.rb', line 14

def self.cmdlet_decolourize(
    this_file = result?
  )
  this_file = this_file.first if this_file.is_a? Array
  output_file = 'decolourized_file_'+File.basename(this_file)
  _ = 'ffmpeg -i '+this_file+' -vf format=gray '+output_file
  esystem _
  set_result(output_file)
  return result? 
end

.cmdlet_download(i) ⇒ Object

#

UniversalPipeHandler.cmdlet_download

Download a file via this cmdlet.

#


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/universal_pipe_handler/cmdlets/download.rb', line 18

def self.cmdlet_download(i)
  if Object.const_defined? :Wget # Then use WgetWrapper if it is available.
    Wget.new(i)
  else
    cmd = 'wget '+i
    run_sys_cmd(cmd)
  end
  @file = File.basename(i)
  set_result @file
  result?
end

.cmdlet_duration(i = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_duration

#


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/universal_pipe_handler/cmdlets/duration.rb', line 16

def self.cmdlet_duration(
    i = result?
  )
  n_seconds = 0
  if i.is_a? Array
    i.each {|entry|
      _ = Multimedia::FileDuration.new(entry)
      n_seconds += _.duration?
    }
  else
    _ = Multimedia::FileDuration.new(i)
    n_seconds += _.duration?
  end
  result = 'Duration of `'+sfile(i)+'`: '+sfancy(n_seconds.to_s)+' seconds.'
  set_result(result)
  return result?
end

.cmdlet_extract(i = nil) ⇒ Object

#

UniversalPipeHandler.cmdlet_extract

Here we will extract the audio and the video.

#


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/universal_pipe_handler/cmdlets/extract.rb', line 14

def self.cmdlet_extract(i = nil)
  _ = result?
  if i.is_a? Array
    case i.first
    when 'audio'
      action_extract_audio _, Dir.pwd
    end
  else # Delegate towards the two other methods here.
    action_extract_audio _, Dir.pwd
    action_extract _
  end
end

.cmdlet_extract_all(dir = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_extract_all

#


12
13
14
15
16
17
18
19
20
21
# File 'lib/universal_pipe_handler/cmdlets/extract_all.rb', line 12

def self.cmdlet_extract_all(dir = result?)
  dir = dir.first if dir.is_a? Array
  all_files = get_all_files_from(dir)
  all_files.each { |file|
    _ = extract_aller.new(file, :dont_run_yet) # If it is an archive, extract_all it.
    _.set_extract_all_to_this_location(dir) # We will extract_all into the same directory.
    _.run
  }
  set_result(all_files)
end

.cmdlet_extract_audio(i = result?, , optional_store_where = nil) ⇒ Object

#

UniversalPipeHandler.cmdlet_extract_audio

This method will extract audio from a video container.

We will make use of the project ExtractAudio for this, so we absolutely depend on that project.

The functionality is implemented as part of the multimedia_paradise gem.

#


20
21
22
23
24
25
26
27
28
# File 'lib/universal_pipe_handler/cmdlets/extract_audio.rb', line 20

def self.cmdlet_extract_audio(
    i                    = result?,
    optional_store_where = nil
  )
  e "Extracting audio from #{sfancy(i)} next." if be_verbose?
  _ = MultimediaParadise.extract_audio(i, optional_store_where)
  set_result(_)
  return result?
end

.cmdlet_extract_video(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_extract_video

Simply delegate towards MultimediaParadise.

#


14
15
16
# File 'lib/universal_pipe_handler/cmdlets/extract_video.rb', line 14

def self.cmdlet_extract_video(i = result?)
  MultimediaConversions.extract_video(i)
end

.cmdlet_find_all(search_term = commandline_arguments? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_find_all (find tag)

This cmdlet can be used in similar ways as “grep”.

#


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/universal_pipe_handler/cmdlets/find_all.rb', line 14

def self.cmdlet_find_all(
    search_term = commandline_arguments?
  )
  keyphrase_to_search_for = search_term
  if keyphrase_to_search_for.is_a? Array
    keyphrase_to_search_for = keyphrase_to_search_for.first
  end
  e 'Now searching for the term `'+
    sfancy(keyphrase_to_search_for)+'`:'+N+N
  dataset = result?
  dataset = dataset.reject {|line|
    line !~ /#{keyphrase_to_search_for}/ # Here we apply the "grep" action.
  }
  colourize = true
  # ======================================================================= #
  # If we enable colourizing the result, then we will add swarn(search_term)
  # ======================================================================= #
  if colourize
    dataset.map! {|entry|
      entry.gsub!(/(#{keyphrase_to_search_for})/, swarn("\\1"))
    }
  end
  set_result(dataset)
  return result?
end

.cmdlet_generate_random_video(i = result?, , hash_with_options = {}) ⇒ Object

#

UniversalPipeHandler.cmdlet_generate_random_video

We generate a random video here.

The second argument is a hash that can keep extra options.

#


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/universal_pipe_handler/cmdlets/generate_random_video.rb', line 16

def self.cmdlet_generate_random_video(i = result?, hash_with_options = {})
  output_file = 'output.mpg'
  if hash_with_options.has_key? :duration
    duration = 'duration='+hash_with_options.delete(:duration)
  else
    duration = 'duration=15' # The default.
  end
  _ = 'ffmpeg -f lavfi -i testsrc='+duration+':size=1280x720:rate=30 '+output_file
  esystem _
  set_result(output_file)
  return result?
end

.cmdlet_generate_string(max = 50, assign_to_result = true) ⇒ Object

#

UniversalPipeHandler.cmdlet_generate_string

This method will simply generate a random string.

action_generate_string 25, false

#


17
18
19
20
21
22
23
24
25
# File 'lib/universal_pipe_handler/cmdlets/generate_string.rb', line 17

def self.cmdlet_generate_string(
    max = 50,
    assign_to_result = true
  )
  alphabet = ('a'..'z').to_a << ' '
  _ = (0...max).map { alphabet.sample }.join
  set_result(_) if assign_to_result
  return result?
end

.cmdlet_get_all_audio_files(from = Dir.pwd) ⇒ Object

#

UniversalPipeHandler.cmdlet_get_all_audio_files

We depend on the action “get all files” for this method here.

We will obtain all audio files from the current working directory, by first obtaining all files, and then applying a filter.

#


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/universal_pipe_handler/cmdlets/get_all_audio_files.rb', line 17

def self.cmdlet_get_all_audio_files(
    from = Dir.pwd
  )
  from = '/Depot/Audio' if from.nil?
  _ = action_get_all_audio_files(from)
  # ======================================================================= #
  # Next, we apply a filter to get only the audio files:
  # ======================================================================= #
  _.reject! {|entry|
    entry = File.extname(entry).delete('.')
    ! ARRAY_AUDIO_FILES.include?(entry)
  }
  set_result(_) # We use this result.
  return _      
end

.cmdlet_get_all_files(from_this_directory = :try_to_use_the_working_directory_variable, include_the_subdirectories = false) ⇒ Object

#

UniversalPipeHandler.cmdlet_get_all_files (files tag)

Invoked via list_content(). We use expand_path here to get the real absolute path. It will return all files that matched a certain criteria.

The first argument is the target directory. This MUST include a trailing ‘/’ - the method here will NOT append a trailing ‘/’.

The second argument, called ‘include_the_subdirectories`, instructs this method to traverse into subdirectories.

#


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/universal_pipe_handler/cmdlets/get_all_files.rb', line 24

def self.cmdlet_get_all_files(
    from_this_directory = :try_to_use_the_working_directory_variable,
    include_the_subdirectories     = false
  )
  if from_this_directory.is_a? Array
    from_this_directory = from_this_directory.first
  end
  case from_this_directory
  # ======================================================================= #
  # === :default
  # ======================================================================= #
  when :default, :def, nil
    from_this_directory = return_pwd
  # ======================================================================= #
  # === :try_to_use_the_working_directory_variable
  # ======================================================================= #
  when :try_to_use_the_working_directory_variable
    from_this_directory = UniversalPipeHandler.working_directory?
    if from_this_directory.nil?
      from_this_directory = return_pwd
    end
  end

  @target_directory = from_this_directory # this may have to be changed eventually.
  if @target_directory.start_with? ':' # assume it is a special keyword.
    @target_directory = sanitize_url(@target_directory)
  end
  target = @target_directory+'**'
  target = @target_directory+'**/**' if include_the_subdirectories
  _ = Dir[target].map {|entry| File.expand_path(entry) }
  set_result(_)
  return result? # We will return all found files.      
end

.cmdlet_get_all_images(dir = start_directory?, , include_subdirs = false) ⇒ Object

#

UniversalPipeHandler.cmdlet_get_all_images

The first argument specifies from where we get the image files.

In order for this to work, we will tap into the method called action_get_all_files().

#


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/universal_pipe_handler/cmdlets/get_all_images.rb', line 17

def self.cmdlet_get_all_images(
    dir             = start_directory?,
    include_subdirs = false
  )
  if dir.is_a? Hash
    if dir.has_key? :from
      dir = dir.delete :from
    end
  end
  if dir == :include_subdirs
    dir = start_directory?
    include_subdirs = true
  end
  dir = dir.first if dir.is_a? Array
  dir << '/' unless dir.end_with? '/'
  dir = sanitize_url(dir)
  dir = dir.first if dir.is_a? Array # We must check again here due to sanitize_url().
  case dir.delete('/')
  when 'njoy_dir','njoy'
    dir = '/home/x/data/images/NJOY/'
  end
  @target_directory = dir # this may have to be changed eventually.
  _ = action_get_all_files(target_directory?, include_subdirs)
  _.select! {|file|
    ARRAY_IMAGE_FILE_TYPES.include?(File.extname(file).delete('.'))
  }
  set_result(_)
  return result?
end

.cmdlet_get_all_images_including_subdirs(optional_target_dir = nil) ⇒ Object

#

UniversalPipeHandler.cmdlet_get_all_images_including_subdirs

The first argument should be the path to a directory.

#


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/universal_pipe_handler/cmdlets/get_all_images_including_subdirs.rb', line 14

def self.cmdlet_get_all_images_including_subdirs(
    optional_target_dir = nil
  )
  set_target_directory(optional_target_dir) if optional_target_dir
  all_the_images = action_get_all_images_including_subdirs(
    { :from => target_directory? },
    :include_subdirs
  ) # true for including all subdirs
  set_result(all_the_images)
  return result?
end

.cmdlet_get_all_video_files(from = Dir.pwd) ⇒ Object

#

UniversalPipeHandler.cmdlet_get_all_video_files

We depend on the action “get all files” for this method here.

By default we will fetch all audios from the directory /Depot/Audio. This behaviour may change in the future, and default to Dir.pwd instead.

#


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/universal_pipe_handler/cmdlets/get_all_video_files.rb', line 18

def self.cmdlet_get_all_video_files(
    from = Dir.pwd
  )
  from << '/' unless from.end_with? '/'
  _ = action_get_all_files(from)
  # ======================================================================= #
  # Next, we apply a filter to get only the audio files:
  # ======================================================================= #
  _.reject! {|entry|
    entry = File.extname(entry).delete('.')
    ! ARRAY_VIDEO_FILES.include?(entry)
  }
  set_result(_) # We use this result.
  return _
end

.cmdlet_get_last_characters(i = 50) ⇒ Object

#

UniversalPipeHandler.cmdlet_get_last_characters

Gets the specified n last characters from a string.

#


14
15
16
17
18
19
20
21
22
# File 'lib/universal_pipe_handler/cmdlets/get_last_characters.rb', line 14

def self.cmdlet_get_last_characters(
    i = 50
  )
  i = i.to_i
  joined = result?.join
  joined = joined.delete(N) # remove all newlines for now. Or not. Hmmm.
  set_result joined[-i, i.abs]
  return result?
end

.cmdlet_helpObject

#

UniversalPipeHandler.cmdlet_help

List all available action types. In order for this to work, the constant REGISTERED_ACTIONS must have been set.

To test this, try this in the pipeshell:

help?
#


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/universal_pipe_handler/cmdlets/help.rb', line 20

def self.cmdlet_help
  set_result(registered_actions?)
  if be_verbose?
    e 'The available actions are:'
    registered_actions?.each_with_index { |action, index| index += 1
      left_part = (index.to_s).rjust(3)+') '
      if Object.const_defined? :Konsole # Add some colours then.
        left_part = Konsole.darkseagreen(left_part)
      end
      e left_part+action
    }
  end
  return result?
end

.cmdlet_identify(this_file = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_identify

This method will attempt to identify a file, in particular multimedia data (thus, audio and video).

For this we will tap into the project MultimediaParadise.

#


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/universal_pipe_handler/cmdlets/identify.rb', line 17

def self.cmdlet_identify(
    this_file = result?
  )
  this_file = this_file.first if this_file.is_a? Array
  if is_a_multimedia_file?(this_file)
    _ = MultimediaParadise::VideoInformation.new(this_file)
    _.report
    set_result _.data # This will be an Array.
  end
  return result?
end

.cmdlet_increase_audio(by_how_much_percent = '5') ⇒ Object

#

UniversalPipeHandler.cmdlet_increase_audio

This method can be used to increase the audio volume.

#


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/universal_pipe_handler/cmdlets/increase_audio.rb', line 14

def self.cmdlet_increase_audio(
    by_how_much_percent = '5'
  )
  this_file = result? # We assume here that a 
  by_how_much_percent = by_how_much_percent.to_i
  _ = IncreaseVolume.new(this_file, :do_not_run_yet)
  _.set_gain by_how_much_percent
  _.run
  set_result(_.file?)
  return result?
end

.cmdlet_install(i = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_install

This will try to install something via RBT::Installer.new

#


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/universal_pipe_handler/cmdlets/install.rb', line 14

def self.cmdlet_install(
    i = result?
  )
  i = i.first if i.is_a? Array
  if Object.const_defined? :Easycompile
    unless File.exist?(i) and ! i.include?('/') # File does not exist here.
      target = SRC_DIR+i.upcase+'/'+i+'*'
      entries = Dir[target]
      i = entries.first unless entries.empty?
    end
    _ = Easycompile.compile(i)
  elsif Object.const_defined? :RBT
    _ = RBT::Compile.new(i)
  end
  set_result(i)
  return result?
end

.cmdlet_match_regex(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_match_regex

This method matches to a regex. Or rather, to the line that includes a particular match.

#


15
16
17
18
19
20
21
22
# File 'lib/universal_pipe_handler/cmdlets/match_regex.rb', line 15

def self.cmdlet_match_regex(i = result?)
  regex = /#{i}/
  match = []
  if result?
    result?.each { |t| match << t if t =~ regex }
    set_result(match)
  end
end

.cmdlet_n_directoriesObject

#

UniversalPipeHandler.cmdlet_n_directories

Tell us how many directories we have.

We need to keep in mind that a directory may also have 0 subdirectories.

#


17
18
19
20
21
22
# File 'lib/universal_pipe_handler/cmdlets/n_directories.rb', line 17

def self.cmdlet_n_directories
  result = get_all_directories
  size = result.size
  set_result(size)
  return result?
end

.cmdlet_n_filesObject

#

UniversalPipeHandler.cmdlet_n_files

#


12
13
14
15
16
17
# File 'lib/universal_pipe_handler/cmdlets/n_files.rb', line 12

def self.cmdlet_n_files
  result = Dir.entries(Dir.pwd).reject! {|_| File.directory? _ }
  result = result.size
  set_result(result)
  return result?
end

.cmdlet_n_words(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_n_words

This commandlet will simply count how many words are in a given string.

#


14
15
16
17
18
19
20
21
22
23
# File 'lib/universal_pipe_handler/cmdlets/n_words.rb', line 14

def self.cmdlet_n_words(i = result?)
  if i.is_a? String
    _ = i.join
  else
    _ = i
  end
  joined = _.join(N)
  set_result joined.split(/\s+/).size
  return result?
end

.cmdlet_number_linesObject

#

UniversalPipeHandler.cmdlet_number_lines (nl tag)

We don’t need to use an argument here, as we can directly make use of the @result variable.

#


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/universal_pipe_handler/cmdlets/number_lines.rb', line 17

def self.cmdlet_number_lines
  counter = 1
  _ = []
  @result = result?.split(N) if result?.is_a? String
  if result?
    result?.each {|member|
      unless counter == (@result.size + 1)
        _ << ('%5s' % counter.to_s )+'  '+member
      end
      counter += 1
    }
    set_result(_) #.join
  else
    e 'Sorry, we could not find any result.'
  end
end

.cmdlet_open_in_browser(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_open_in_browser

#


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/universal_pipe_handler/cmdlets/open_in_browser.rb', line 12

def self.cmdlet_open_in_browser(i = result?)
  if i.is_a? Array
    i.compact! # Get rid of nil values.
  end
  if i.empty? && @img_file
    i = @img_file.to_a
  elsif i.empty?
    i = result?
  end
  i << @file if i.empty? && ! @file.nil?
  i = [i] if i.is_a? String
  # ======================================================================= #
  # Next, iterate over our Array. (That also means that we expect an
  # Array here.)
  # ======================================================================= #
  if i.is_a? Array # if the input is an array
    if i.empty?
      e 'Can not open anything as the input was empty.'
    else
      i.each { |entry|
        @display_the_result = false # Don't show this on the commandline.
        sys_cmd = 'firefox -new-tab "'+entry+'"'
        system(sys_cmd)
      }
    end
  end
end

.cmdlet_pad_left(default_n_pad_to_use = 2) ⇒ Object

#

UniversalPipeHandler.cmdlet_pad_left

#


12
13
14
15
16
17
18
# File 'lib/universal_pipe_handler/cmdlets/pad_left.rb', line 12

def self.cmdlet_pad_left(
    default_n_pad_to_use = 2
  )
  pad = ' ' * default_n_pad_to_use.to_i
  set_result(result?.map { |_| pad+_ }) # Apply the padding on each element.
  return result?
end

.cmdlet_pad_right(default_n_pad_to_use = 2) ⇒ Object

#

UniversalPipeHandler.cmdlet_pad_right

#


12
13
14
15
16
17
18
# File 'lib/universal_pipe_handler/cmdlets/pad_right.rb', line 12

def self.cmdlet_pad_right(
    default_n_pad_to_use = 2
  )
  pad = ' ' * default_n_pad_to_use.to_i
  set_result(result?.map { |_| _+pad })
  return result?
end

.cmdlet_position(i) ⇒ Object

#

UniversalPipeHandler.cmdlet_position

We smply assign to position in a stream (video or audio), then return the original result unaltered.

#


15
16
17
18
19
# File 'lib/universal_pipe_handler/cmdlets/position.rb', line 15

def self.cmdlet_position(i)
  set_position(i)
  set_result(i)
  result?
end

.cmdlet_processes?Boolean

#

UniversalPipeHandler.cmdlet_processes

Show the processes on Linux.

#

Returns:

  • (Boolean)


14
15
16
# File 'lib/universal_pipe_handler/cmdlets/processes.rb', line 14

def self.cmdlet_processes?
  set_result `ps ax`
end

.cmdlet_random(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_random

Obtain a (one) random entry from @result.

This is also equivalent to random_line.

#


16
17
18
19
# File 'lib/universal_pipe_handler/cmdlets/random.rb', line 16

def self.cmdlet_random(i = result?)
  set_result(i.sample)
  return result?
end

.cmdlet_random_video(from_this_dir = ("#{Dir.pwd}/").squeeze('/')) ⇒ Object

#

UniversalPipeHandler.cmdlet_random_video

This method will return a random from the current directory.

#


14
15
16
17
18
19
20
21
# File 'lib/universal_pipe_handler/cmdlets/random_video.rb', line 14

def self.cmdlet_random_video(
    from_this_dir = ("#{Dir.pwd}/").squeeze('/')
  )
  files = Dir[from_this_dir+'*']
  files.select! {|entry| is_a_video_file?(entry) }
  set_result(files.sample)
  return result?
end

.cmdlet_read_file(this_file = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_read_file

If we input a number as argument this_file, then we assume that we want to fetch a file at this position, i.e. if “3” then this translates to the third file in the directory.

#


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/universal_pipe_handler/cmdlets/read_file.rb', line 16

def self.cmdlet_read_file(
    this_file = result?
  )
  this_file = this_file.first if this_file.is_a? Array
  if this_file =~ /\d+/
    this_file = Dir['*'].sort[ this_file.to_i - 1 ] # Sort the entries first, then access one of them.
  end
  this_file = this_file.to_s
  if File.exist? this_file
    if File.directory? this_file # If it is a dir, we read in the words combined  from all files in that dir.
      _ = this_file
      _ << '/' unless _.end_with? '/'
      _ << '*'
      array_of_files = Dir[_].reject {|x| File.directory?(x)}
      _ = [] # reassign the throwaway variable.
      array_of_files.each { |file| _ << File.readlines(file).flatten }
      set_result _.flatten
    else
      set_result File.readlines(this_file)
    end
  else # Else the file does not exist.
    e 'File `'+sfile(this_file)+'` does not exist, '+
      'thus we can not read its content.'
    set_result nil
  end
  return result?
end

.cmdlet_read_line(this_line_number = 1) ⇒ Object

#

UniversalPipeHandler.cmdlet_read_line

This is the cmdlet that will read in a file.

#


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/universal_pipe_handler/cmdlets/read_line.rb', line 14

def self.cmdlet_read_line(this_line_number = 1)
  _ = result?
  _ = _.join if _.is_a? Array # Work with a copy.
  if _.empty? # assume a problem here.
    _ = this_line_number
    this_line_number = 0
  elsif this_line_number == 'last' # special keyword. Pass through for now. We handle it later.
  else # Default.
    this_line_number = this_line_number.to_i.abs
  end
  _ = File.readlines(_)
  if this_line_number == 'last'
    this_line_number = _.size
  end
  set_result _[this_line_number - 1]
end

.cmdlet_read_n_lines(n_lines_to_read = 50, optional_file = nil) ⇒ Object

#

UniversalPipeHandler.cmdlet_read_n_lines

To manually test this method, do:

File.readlines('/Depot/Information/Confree_Images.html')[0..20]
#


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/universal_pipe_handler/cmdlets/read_n_lines.rb', line 17

def self.cmdlet_read_n_lines(
    n_lines_to_read = 50,
    optional_file   = nil
  )
  if n_lines_to_read.is_a? Array
    if n_lines_to_read.size > 1
      optional_file   = n_lines_to_read.pop # Eliminate the last entry.
      n_lines_to_read = n_lines_to_read.first
    else
      n_lines_to_read = n_lines_to_read.first
    end
  end
  n_lines_to_read = n_lines_to_read.to_i.abs
  if optional_file
    _ = action_read_file(optional_file)
  else
    _ = result? #.split(N)
  end
  set_result(_[0 .. (n_lines_to_read-1)])
end

.cmdlet_read_n_lines_inverted(n_lines_to_read = 50, optional_file = nil) ⇒ Object

#

UniversalPipeHandler.cmdlet_read_n_lines_inverted

#


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/universal_pipe_handler/cmdlets/read_n_lines_inverted.rb', line 12

def self.cmdlet_read_n_lines_inverted(
    n_lines_to_read = 50,
    optional_file   = nil
  )
  if n_lines_to_read.is_a? Array
    if n_lines_to_read.size > 1
      optional_file = n_lines_to_read.pop unless n_lines_to_read.nil?
    end
    n_lines_to_read = n_lines_to_read.first
  end
  n_lines_to_read = n_lines_to_read.to_i.abs
  if optional_file
    _ = File.readlines(optional_file)
  else
    _ = result?
  end
  if _
    set_result(_.reverse[0 .. (n_lines_to_read-1)].reverse)
  else
    e 'The input in the method `'+__method__.to_s+'` is '+
      'nil. Can not continue.'
  end
end

.cmdlet_remove_audio(i = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_remove_audio

This action will remove the audio from a clip, by calling the class RemoveAudio.

#


15
16
17
18
19
20
# File 'lib/universal_pipe_handler/cmdlets/remove_audio.rb', line 15

def self.cmdlet_remove_audio(
    i = result?
  )
  set_result(RemoveAudio[i])
  return result?
end

.cmdlet_remove_comments(i = result?, , use_this_split_character = '#') ⇒ Object

#

UniversalPipeHandler.cmdlet_remove_comments

Remove comments via the class StripComments.

#


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/universal_pipe_handler/cmdlets/remove_comments.rb', line 14

def self.cmdlet_remove_comments(
    i = result?,
    use_this_split_character = '#'
  )
  _ = RemoveComments::RemoveComments.new(i, :be_silent) # bl $RSRC/remove_comments/lib/remove_comments/remove_comments/remove_comments.rb
  _.split_character = use_this_split_character
  _.retain_newlines
  _.run
  set_result(_.result)
  return result?
end

.cmdlet_remove_directoriesObject

#

UniversalPipeHandler.cmdlet_remove_directories

We use this action to remove directories.

#


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/universal_pipe_handler/cmdlets/remove_directories.rb', line 14

def self.cmdlet_remove_directories
  result = []
  _ = result?.compact # Get rid of nil values.
  if _.empty? # Then get all directory from the current dir.
    _ = get_all_directories
  end
  _.map {|entry|
    result << entry if File.directory?(entry)
  }
  e 'Removing these directories next:'
  result.each { |entry| remove(entry) }
  set_result(result)
  return result?
end

.cmdlet_remove_html(partial_remove = false) ⇒ Object

#

UniversalPipeHandler.cmdlet_remove_html

Remove the HTML part from a string.

#


14
15
16
17
18
19
20
21
22
23
# File 'lib/universal_pipe_handler/cmdlets/remove_html.rb', line 14

def self.cmdlet_remove_html(partial_remove = false)
  _ = result? # Work on a copy of result here.
  _ = _.join if _.is_a? Array
  if partial_remove
    set_result _.gsub(/\</, '').gsub(/\>/,'')
  else
    set_result Cyberweb.remove_html(_)
  end
  return result? # This is ok again.
end

.cmdlet_remove_newlines(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_remove_newlines

Remove comments via a gsub.

This is similar to using tr by the way, like: tr “n” “ ”

#


16
17
18
19
20
21
# File 'lib/universal_pipe_handler/cmdlets/remove_newlines.rb', line 16

def self.cmdlet_remove_newlines(i = result?)
  i = i.join if i.is_a? Array
  _ = i.gsub(/\n/, ' ') # Here we will get rid of the "\"n.
  set_result(_)
  return result?
end

.cmdlet_remove_numbersObject

#

UniversalPipeHandler.cmdlet_remove_numbers

This method will remove numbers. We will apply the modification on The method remove_numbers is defined in shared/shared.rb

#


17
18
19
20
21
22
23
# File 'lib/universal_pipe_handler/cmdlets/remove_numbers.rb', line 17

def self.cmdlet_remove_numbers
  if result?.is_a? Array
    @result.map! {|entry| remove_numbers(entry) }
  else
    set_results(remove_numbers(result?))
  end
end

.cmdlet_repackage_to(this = :zip) ⇒ Object

#

UniversalPipeHandler.cmdlet_repackage_to

#


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/universal_pipe_handler/cmdlets/repackage_to.rb', line 12

def self.cmdlet_repackage_to(
    this = :zip
  )
  case this.to_s
  # ======================================================================= #
  # === zip
  # ======================================================================= #
  when 'zip'
    _ = 'zip a_zip_archive_'+
        action_generate_string(5, false).gsub(/ /,'')+
        '_'+get_date+'_'+get_time+'.zip '+result?.join(' ')
    run_sys_cmd _
  end
end

.cmdlet_replace_underscoresObject

#

UniversalPipeHandler.cmdlet_replace_underscores

We replace all ‘ ’ with ‘_’. In other words, blanks are replaced with underscores, in the filenames. This is done through the class ReplaceSpaceWithUnderscore.

To test this, try:

ls | replace underscores
#


23
24
25
26
27
# File 'lib/universal_pipe_handler/cmdlets/replace_underscores.rb', line 23

def self.cmdlet_replace_underscores
  result = Roebe::ReplaceSpaceWithUnderscore.new
  set_result(result.modified_these_files)
  return result?
end

.cmdlet_resize(which_files = nil, resize_how = nil) ⇒ Object

#

UniversalPipeHandler.cmdlet_resize

This method can be used to resize video, audio or image data.

#


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/universal_pipe_handler/cmdlets/resize.rb', line 14

def self.cmdlet_resize(
    which_files = nil,
    resize_how  = nil
  )
  if which_files.is_a? Array
    which_files.each {|entry| action_resize(resize_how, entry) }
  else
    if resize_how.include? '%'
      resize_how = (resize_how.to_i / 100.0)
      output_file = rds(Dir.pwd+'/output_'+File.basename(which_file))
      # ===================================================================== #
      # Next, resize the video here:
      # ===================================================================== #
      if is_video_file? 
        _ = 'ffmpeg -i '+_+' -vf scale=iw*'+resize_how.to_s+':-1 '+output_file
      else # Assume image here.
        _ = 'convert '+which_file
      end
      set_result(output_file)
      esystem _
      return result?
    end
  end
end

.cmdlet_resize_image(second_argument, third_argument = nil) ⇒ Object

#

UniversalPipeHandler.cmdlet_resize_image

This will resize an image, by making use of the program called “convert” from the ImageMagick suite.

#


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/universal_pipe_handler/cmdlets/resize_image.rb', line 17

def self.cmdlet_resize_image(
    second_argument,
    third_argument = nil
  )
  if second_argument.is_a? Array and second_argument.size > 1
    third_argument = second_argument.pop # Eliminate last Array member.
  end
  if third_argument
    second_argument = second_argument.to_a unless second_argument.is_a? Array
    @result = second_argument
    second_argument = third_argument
  end
  array = []
  result?.each { |img|
    store_here = ''
    if img.include? '/'
      store_here << rds(File.dirname(img))
    else
      store_here << rds(Dir.pwd)
    end
    array << (converted_name = store_here+'resized_image_'+File.basename(img) )
    _ = 'convert '+img+' -resize '+second_argument+' '+converted_name
    run_sys_cmd _
  }
  set_result(array)
  do_not_display
end

.cmdlet_reverseObject

#

UniversalPipeHandler.cmdlet_reverse

To test this method, try:

cat :main_file | reverse
#


17
18
19
20
# File 'lib/universal_pipe_handler/cmdlets/reverse.rb', line 17

def self.cmdlet_reverse
  set_result result?.reverse
  return result?
end

.cmdlet_screenshotObject

#

UniversalPipeHandler.cmdlet_screenshot (screenshot tag)

Make a screenshot through this method here.

To test this, do:

snapshot
#


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/universal_pipe_handler/cmdlets/screenshot.rb', line 21

def self.cmdlet_screenshot
  set_img_file = Dir.pwd+'/Screenshot_'+get_date+'_'+get_time+'.png'
  @result = @img_file
  if Object.const_defined?(:Roebe) and
     Roebe.const_defined?(:TakeScreenshot)
    _ = Roebe::TakeScreenshot.new :dont_run_yet
    _.store_into_this_file @img_file if @img_file # Otherwise use the default.
    _.delay = 0
    _.do_not_open_in_gimp # Don't want it gimp now.
    _.be_quiet
    _.set_output_file(set_img_file)
    _.run
    set_result _.output_file?
    return result?
  else
    _ = 'scrot '+@img_file
    esystem _
  end
end

.cmdlet_search_torrent(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_search_torrent

Simply delegate towards MultimediaConversions.

#


18
19
20
21
22
# File 'lib/universal_pipe_handler/cmdlets/search_torrent.rb', line 18

def self.cmdlet_search_torrent(i = result?)
  search_torrent = SearchTorrent.new(remove_quotes(i), false) # false for dont be verbose
  search_torrent.fetch_remote_url
  set_result search_torrent.url
end

.cmdlet_seconds?(i = result?) ) ⇒ Boolean

#

UniversalPipeHandler.cmdlet_seconds?

This method is a fake-method: it will simply return the result in form of an Integer.

#

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/universal_pipe_handler/cmdlets/seconds.rb', line 15

def self.cmdlet_seconds?(i = result?)
  i = i.first if i.is_a? Array
  i = i.to_i
  set_result(i)
  return i
end

.cmdlet_select(i = 0) ⇒ Object

#

UniversalPipeHandler.cmdlet_select

We use this method to select n entries, where n is the argument to that method.

#


15
16
17
18
19
20
21
# File 'lib/universal_pipe_handler/cmdlets/select.rb', line 15

def self.cmdlet_select(i = 0)
  i = i.to_i
  if result?
    set_result(result?[0, i])
  end
  return result?
end

.cmdlet_show_lines(range) ⇒ Object

#

UniversalPipeHandler.cmdlet_show_lines

#


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/universal_pipe_handler/cmdlets/show_lines.rb', line 14

def self.cmdlet_show_lines(range)
  range = range.to_s
  if range.include? '-'
    splitted = range.split '-'
    first = splitted.first.to_i - 1
    last = splitted.last.to_i - 1
    range = Range.new(first, last)
    range = range.to_a
  end
  new_result = []
  result?.each_with_index { |_, index|
    new_result << _ if range.include?(index.to_i)
  }
  set_result(new_result)
  return result? 
end

.cmdlet_shuffle(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_shuffle

#


12
13
14
# File 'lib/universal_pipe_handler/cmdlets/shuffle.rb', line 12

def self.cmdlet_shuffle(i = result?)
  set_result(result?.shuffle)
end

.cmdlet_shuffle_csv(arguments = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_shuffle_csv

Currently the assumption is that we will pass a .csv file to this method, which will then be reshuffled into different tab-entries.

#


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/universal_pipe_handler/cmdlets/shuffle_csv.rb', line 18

def self.cmdlet_shuffle_csv(
    arguments = result?
  )
  new_array = []
  possible_csv_file = result?.first
  # ======================================================================= #
  # We can not use CSV.read because this will chop off '"' quotes.
  # ======================================================================= #
  if File.exist? possible_csv_file
    possible_csv_file = CSV.read(possible_csv_file)
    # ===================================================================== #
    # When we have an ',' entry in a member then we know that this
    # derived from a '"' quoted word.
    # ===================================================================== #
    possible_csv_file.map! {|line|
      line.map! {|entry|
        entry = '"'+entry+'"' if entry.include? ','
        entry
      }
      line
    }
  end
  splitted = arguments.split(',')
  possible_csv_file.each {|line|
    index_array = [*splitted].map(&:to_i)
    # ===================================================================== #
    # Deduct one from the index-array next.
    # ===================================================================== #
    index_array.map! {|entry|
      entry -= 1
    }
    new_array << index_array.map {|index| line.at(index) }
  }
  set_result(new_array)
  return result?
end

.cmdlet_size(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_size

#


12
13
14
15
16
17
18
19
# File 'lib/universal_pipe_handler/cmdlets/size.rb', line 12

def self.cmdlet_size(i = result?)
  size = 0
  i.each {|entry|
    size += File.size(entry)
  }
  set_result(size)
  return result?
end

.cmdlet_sort_alphabetical(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_sort_alphabetical

#


12
13
14
15
16
17
# File 'lib/universal_pipe_handler/cmdlets/sort_alphabetical.rb', line 12

def self.cmdlet_sort_alphabetical(i = result?)
  new_data = i.sort_by {|entry|
    File.basename(entry).downcase # We sort only on the last part here.
  }
  set_result(new_data)
end

.cmdlet_sort_by_date(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_sort_by_date

The convention used here is:

new files will appear on top
#


17
18
19
20
21
# File 'lib/universal_pipe_handler/cmdlets/sort_by_date.rb', line 17

def self.cmdlet_sort_by_date(i = result?)
  sorted = i.sort_by {|entry| File.atime(entry) }.reverse
  set_result sorted
  return result?
end

.cmdlet_starts_with(search_term = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_starts_with

This method will iterate over our array and return a match.

#


14
15
16
17
18
19
20
21
22
23
# File 'lib/universal_pipe_handler/cmdlets/starts_with.rb', line 14

def self.cmdlet_starts_with(
    search_term = result?
  )
  result = []

  result?.each { |line|
    result << line.chomp if line.start_with?(search_term) #line[0, search_term.size] == search_term
  } if result?.is_a? Array
  set_result(result)
end

.cmdlet_stat_file(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_stat_file (stat tag)

Since as of August 2011 we get extended stat info, by tapping into class StatFile.

To try this, you can do:

assign :main_video | stat file
#


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/universal_pipe_handler/cmdlets/stat_file.rb', line 24

def self.cmdlet_stat_file(i = result?)
  if i.is_a? Array
    i.each {|entry|
      action_stat_file(entry)
    }
  else
    if i.nil?
      e 'Please provide a valid argument.'
      exit
    end
    i = i.to_s
    if File.exist? i.to_s
      stat_file = StatFile.new(i, :dont_be_verbose)
      stat_file.stat_file(i)
      @result = '' if @result.is_a? Array # Reset the result here.
      @result << stat_file.result
      file_size    = File.size(i).to_s
      file_end     = File.extname(i).gsub(/\./,'')
      n_lines      = 0    # safeguard.
      n_characters = 0 # safeguard.
      case file_end
      when *ARRAY_MULTIMEDIA_FILES # Pass through here - we can't read these file types..
      else
        readlines = File.readlines(i)
        n_lines = readlines.size.to_s
        n_characters = readlines.join.split(/\s+/).size.to_s
      end
      file_size = Colours.slateblue(file_size.to_s)
      @result << N+'  The file `'+sfile(i.to_s)+'`'+N+'  has '+n_lines.to_s+
                 ' lines, '+sfancy(n_characters.to_s)+' characters and a '+
                 'file size of `'+file_size+'`.'
    else
      e 'No file could be found at `'+sfancy(i.to_s)+'`.'
    end
  end
end

.cmdlet_to_asciiObject

#

UniversalPipeHandler.cmdlet_to_ascii

Convert image files to ascii format at once, using the program called jp2a. Obviously for this to work, jp2a must have been installed.

Since as of May 2015, we also store in an .ascii file automatically.

Standalone test, through the pipeshell:

random image from :njoy_dir | to ascii
#


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/universal_pipe_handler/cmdlets/to_ascii.rb', line 22

def self.cmdlet_to_ascii
  cmd = 'jp2a'
  if result?
    result?.each { |entry|
      _ = cmd+' '+entry
      e _
      return_value = `#{_}`
      what = return_value
      this_file = save_dir?+'ascii_'+
        File.basename(entry.downcase).tr('.','_')+'.ascii' 
      write_what_into(what, this_file)
      set_result(this_file)
    }
  end
end

.cmdlet_to_camel_case(i = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_to_camel_case

#


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/universal_pipe_handler/cmdlets/to_camel_case.rb', line 15

def self.cmdlet_to_camel_case(
    i = result?
  )
  array = []
  if i.is_a? Array
    i.each {|inner_entry|
      base_dir = File.dirname(inner_entry)+'/'
      if File.exist? inner_entry
        new_name = File.basename(inner_entry).
                   split('_').map { |most_inner_entry| most_inner_entry.capitalize }.join
        new_name = "#{base_dir}#{new_name}"
        unless File.exist? new_name
          FileUtils.mv(inner_entry, new_name)
        end
        array << new_name
      end
    }
  end
  set_result(array)
  return result?
end

.cmdlet_to_dna(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_to_dna

This method will convert the given input to a valid DNA string. We make use of the project called Bioroebe here.

To test this, start the pipeshell and do:

assign ACCACACCAUUUCCCAUGGGUGUGUGG | to dna
#


20
21
22
23
24
25
26
27
28
29
# File 'lib/universal_pipe_handler/cmdlets/to_dna.rb', line 20

def self.cmdlet_to_dna(i = result?)
  unless Object.const_defined? :Bioroebe
    require 'bioroebe'
  end
  if Object.const_defined? :Bioroebe
    _ = Bioroebe.to_dna(i)
    set_result(_)
  end
  return result?
end

.cmdlet_to_movieObject

#

UniversalPipeHandler.cmdlet_to_movie

This currently is somewhat dysfunct. More accurately, it was never written.

#


13
14
# File 'lib/universal_pipe_handler/cmdlets/to_movie.rb', line 13

def self.cmdlet_to_movie
end

.cmdlet_to_pdf(text_to_write = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_to_pdf

This currently is somewhat dysfunctional.

To test this, we could try:

cat :main_file | to pdf
#


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/universal_pipe_handler/cmdlets/to_pdf.rb', line 31

def self.cmdlet_to_pdf(
    text_to_write = result?
  )
  font_size  = 18
  alignment = :left
  store_here = 'autogenerated_pdf.pdf'
  text_to_write = text_to_write.join if text_to_write.is_a? Array
  if text_to_write.count('"') == 2
    text_to_write.delete!('"')
  end
  if text_to_write.include? '\\n'
    text_to_write.gsub!(/\\n/, "\n")
  end
  Prawn::Document.generate(store_here) {
    font 'Times-Roman'
    text text_to_write,
         align: alignment,
         size:  font_size
  }
  set_result(store_here)
  if ALSO_OPEN_IN_PDF_VIEWER # Open the .pdf next.
    esystem USE_THIS_PDF_VIEWER+' '+store_here+' &'
  end
  return result?
end

.cmdlet_torrentObject

#

UniversalPipeHandler.cmdlet_torrent

#


12
13
14
15
# File 'lib/universal_pipe_handler/cmdlets/download_torrent.rb', line 12

def self.cmdlet_torrent
  cmdlet_search_torrent
  cmdlet_download(result?)
end

.cmdlet_translate(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_translate

This method will try to translate english words.

To try it, one can do:

assign purview | translate
#


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/universal_pipe_handler/cmdlets/translate.rb', line 19

def self.cmdlet_translate(i = result?)
  if i.is_a? Array
    i.each {|entry| action_translate(entry) }
  else
    i = i.to_s.chomp
    if Object.const_defined? :AskEnglishWord
      do_not_show_the_result
      @result = AskEnglishWord.new(i, :delay => 0).result
    else
      location = ENV['SCIENCE'].to_s+'/YAML/DICTIONARIES/english.yml'
      english_data = YAML.load_file(location)
      @result = english_data[i]
    end
    result?
  end
end

.cmdlet_upload_to(this_target = 'shevegen.square7.ch') ⇒ Object

#

UniversalPipeHandler.cmdlet_upload_to

The first argument shall be where to upload to. We currently use the FtpParadise to upload to some remote site.

To try this, do:

assign :main_file | ftp_upload
#


20
21
22
23
24
25
26
27
# File 'lib/universal_pipe_handler/cmdlets/upload_to.rb', line 20

def self.cmdlet_upload_to(
    this_target = 'shevegen.square7.ch' # The target should not include leading http:
  )
  this = result?
  where_to = this_target # Where to we shall upload something.
  FtpParadise.upload(this, where_to)
  return result?
end

.cmdlet_word_countObject

#

UniversalPipeHandler.cmdlet_word_count

Count the words here.

Note that this method will instantly output the results - see the part of the code that is run in the .each_pair section.

#


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/universal_pipe_handler/cmdlets/word_count.rb', line 17

def self.cmdlet_word_count
  pad_n = 80
  hash = Hash.new(0)
  result?.join.scan(/\w+\??/) { |word| hash[word] += 1 }
  @result = hash
  be_really_verbose_here = true # Hack. For now we will always instantly display the result.
  if be_really_verbose_here
    _ = result?.sort_by {|key, value| value }.reverse # Sort by size.
    _.each { |key, value| # This is now an Array.
      if Object.const_defined? :Konsole
        e ('The key `'+mediumaquamarine(key)+'` has been found that '+
          'many times: ').ljust(pad_n)+orange(value.to_s)
      else # else use normal module Colours support.
        e ('The key `'+sfancy(key)+'` has been found that '+
          'many times: ').ljust(pad_n)+simp(value.to_s)
      end
    }
    set_result nil # Not sure why we set to nil in this case.
  end
  do_not_show_the_result
end

.cmdlet_word_wrap(n_chars_limit = 80) ⇒ Object

#

UniversalPipeHandler.cmdlet_word_wrap

By default, this method will limit the words up towards 80 characters per given line.

The first argument to it specifies that 80 characters limit.

#


17
18
19
20
21
22
23
24
# File 'lib/universal_pipe_handler/cmdlets/word_wrap.rb', line 17

def self.cmdlet_word_wrap(
    n_chars_limit = 80
  )
  set_result @result.map {|_|
    _.gsub(/(.{1,#{n_chars_limit}})(\s+|$)/, "\\1\n")
  }.join if result?
  return result?
end

.cmdlet_write_to(where_to = result? ) ⇒ Object

#

UniversalPipeHandler.cmdlet_write_to (write tag)

This method writes into a text file on the local filesystem.

It will also respect MACROS such as #APPEND# passed to it.

#


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/universal_pipe_handler/cmdlets/write_to.rb', line 16

def self.cmdlet_write_to(
    where_to = result?
  )
  require 'save_file'
  what = result?
  if where_to.include? '#' # Assume a macro like #APPEND# was given. For this to work, file? must return a non-nil value.
    where_to = where_to.gsub(/#APPEND#/, file?)
  end
  if where_to.end_with? '.csv' # Then we assume this is a .csv file.
    if what.is_a? Array
      what.map! {|line|
        line.join(',')+N
      }
    end
  end
  if what
    if what.is_a? Array
      what = what.join.strip
    end
    if File.directory? where_to
      where_to << '/'+File.basename(what)
      where_to = rds(where_to)
    end
    if File.exist? what
      e 'Copying '+what+' to `'+sfile(where_to)+'`.' if be_verbose?
      copy(what, where_to)
    else
      e 'Writing into `'+sfile(where_to)+'`.' if be_verbose?
      SaveFile.write_what_into(what, where_to) # Delegate towards the SaveFile module here.
    end
    set_result where_to
    return result?
  end
end

.cmdlet_years(i = result?) ) ⇒ Object

#

UniversalPipeHandler.cmdlet_years

We will convert the amount of years into seconds.

Usage example for the pipeshell:

20 years | n_days?
#


19
20
21
22
23
24
25
26
27
# File 'lib/universal_pipe_handler/cmdlets/years.rb', line 19

def self.cmdlet_years(i = result?)
  # ======================================================================= #
  # Calculate how many seconds there are.
  # ======================================================================= #
  n_days = (i.to_f * 365.25).floor # The 0.25 is included because of the leap years.
  i = ONE_DAY_HAS_N_SECONDS * n_days
  set_result(i)
  return result?
end

.cmdlets_directory?Boolean

#

UniversalPipeHandler.cmdlets_directory?

#

Returns:

  • (Boolean)


13
14
15
# File 'lib/universal_pipe_handler/toplevel_methods/cmdlet_directory.rb', line 13

def self.cmdlets_directory?
  UniversalPipeHandler::CMDLETS_DIRECTORY
end

.do_require_the_individual_cmdlet_filesObject

#

UniversalPipeHandler.do_require_the_individual_cmdlet_files

#


14
15
16
17
18
19
# File 'lib/universal_pipe_handler/requires/do_require_the_individual_cmdlet_files.rb', line 14

def self.do_require_the_individual_cmdlet_files
  Dir["#{PROJECT_BASE_DIRECTORY}/cmdlets/*.rb"].each {|this_cmdlet|
    require "#{PROJECT_BASE_DIRECTORY}/cmdlets/"+
            File.basename(this_cmdlet)
  }
end

.e(i = '') ⇒ Object

#

UniversalPipeHandler.e

#


12
13
14
# File 'lib/universal_pipe_handler/toplevel_methods/e.rb', line 12

def self.e(i = '')
  puts i
end

.file_pipe_aliasesObject

#

UniversalPipeHandler.file_pipe_aliases

#


63
64
65
# File 'lib/universal_pipe_handler/constants/constants.rb', line 63

def self.file_pipe_aliases
  FILE_PIPE_ALIASES
end

.predefined_methods?Boolean

#

UniversalPipeHandler.predefined_methods?

#

Returns:

  • (Boolean)


96
97
98
# File 'lib/universal_pipe_handler/constants/constants.rb', line 96

def self.predefined_methods?
  PREDEFINED_METHODS
end

.project_base_directory?Boolean

#

UniversalPipeHandler.project_base_directory?

#

Returns:

  • (Boolean)


19
20
21
# File 'lib/universal_pipe_handler/project/project.rb', line 19

def self.project_base_directory?
  PROJECT_BASE_DIRECTORY
end

.project_yaml_directory?Boolean

#

UniversalPipeHandler.project_yaml_directory?

#

Returns:

  • (Boolean)


27
28
29
# File 'lib/universal_pipe_handler/project/project.rb', line 27

def self.project_yaml_directory?
  PROJECT_BASE_DIRECTORY+'yaml/'
end

.rds(i = Dir.pwd) ⇒ Object

#

UniversalPipeHandler.rds

#


12
13
14
15
16
# File 'lib/universal_pipe_handler/toplevel_methods/rds.rb', line 12

def self.rds(
    i = Dir.pwd
  )
  return "#{i}/".squeeze('/')
end

.result=(i) ⇒ Object

#

UniversalPipeHandler.result=

#


24
25
26
# File 'lib/universal_pipe_handler/toplevel_methods/misc.rb', line 24

def self.result=(i)
  @result = i
end

.result?Boolean

#

UniversalPipeHandler.result?

#

Returns:

  • (Boolean)


17
18
19
# File 'lib/universal_pipe_handler/toplevel_methods/misc.rb', line 17

def self.result?
  @result
end

.return_pwdObject

#

UniversalPipeHandler.return_pwd

#


31
32
33
# File 'lib/universal_pipe_handler/toplevel_methods/misc.rb', line 31

def self.return_pwd
  "#{Dir.pwd}/".squeezu('/')
end

.set_working_directory(i) ⇒ Object

#

UniversalPipeHandler.set_working_directory

#


43
44
45
# File 'lib/universal_pipe_handler/toplevel_methods/misc.rb', line 43

def self.set_working_directory(i)
  @working_directory = i
end

.token?Boolean

#

UniversalPipeHandler.token?

#

Returns:

  • (Boolean)


14
15
16
# File 'lib/universal_pipe_handler/toplevel_methods/token.rb', line 14

def self.token?
  return UniversalPipeHandler::PIPE_TOKEN
end

.use_colours?Boolean

#

UniversalPipeHandler.use_colours?

Convenience method to determine whether we will use colours or whether we will not.

#

Returns:

  • (Boolean)


30
31
32
# File 'lib/universal_pipe_handler/colours/colours.rb', line 30

def self.use_colours?
  @dataset[:configuration][:use_colours]
end

.working_directory?Boolean

#

UniversalPipeHandler.working_directory?

#

Returns:

  • (Boolean)


50
51
52
# File 'lib/universal_pipe_handler/toplevel_methods/misc.rb', line 50

def self.working_directory?
  @working_directory
end

Instance Method Details

#sfancy(i) ⇒ Object

#

sfancy

#


45
46
47
48
# File 'lib/universal_pipe_handler/colours/colours.rb', line 45

def sfancy(i)
  return ::Colours.sfancy(i) if UniversalPipeHandler.use_colours?.use_colours?
  return i
end

#simp(i) ⇒ Object

#

simp

#


37
38
39
40
# File 'lib/universal_pipe_handler/colours/colours.rb', line 37

def simp(i)
  return ::Colours.simp(i) if UniversalPipeHandler.use_colours?.use_colours?
  return i
end