Class: Scide::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/scide/command.rb

Overview

A command to be used in a GNU Screen window. There are several command implementations (show command, run command, tail file, etc). See under Commands.

Direct Known Subclasses

Scide::Commands::Show

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents, options = {}) ⇒ Command

Returns a new command with the given options.

Arguments

  • contents - The contents of the command. Typically this is only a string, but more advanced commands might be initialized with arrays or hashes. By default, the contents can be retrieved as a string with #text_with_options.

  • options - Options that can be used in the string contents of the command. See #text_with_options.



57
58
59
60
61
62
63
64
65
# File 'lib/scide/command.rb', line 57

def initialize contents, options = {}

  # fill text only if it's not already there, in case a subclass does
  # some initialization work before calling super
  @text ||= contents.to_s

  # merge given options to the already initialized ones, if any
  @options = (@options || {}).merge options
end

Instance Attribute Details

#optionsObject (readonly)

The options given to this command. These are built by merging global options, project options and window options.



10
11
12
# File 'lib/scide/command.rb', line 10

def options
  @options
end

Class Method Details

.resolve(window, contents) ⇒ Object

Returns a new command for the given window.

Arguments

  • window - The window in which the command will be used. Command options are retrieved from Window#options. See #initialize.

  • contents - The command configuration (String or Hash).

String Initialization

The string must be in the format COMMAND [CONTENTS].

TYPE is the name of the command class under Scide::Commands, in uppercase camelcase. For example, TAIL corresponds to Scide::Commands::Tail, MY_COMMAND would correspond to Scide::Commands::MyCommand.

CONTENTS is the contents of the command.

Hash Initialization

The following options can be given:

  • :command => string is the same COMMAND as for string initialization above.

  • :contents => string or other is the same CONTENTS as for string initialization above. Typically this is only a string, but more advanced commands might be initialized with arrays or hashes.



38
39
40
41
42
43
44
45
46
# File 'lib/scide/command.rb', line 38

def self.resolve window, contents
  if contents.kind_of? Hash
    resolve_from_hash window, contents
  elsif contents.kind_of? String
    resolve_from_string window, contents
  else
    raise ArgumentError, 'command must be a string or a hash'
  end
end

Instance Method Details

#text_with_optionsObject

Returns the text of this command with filtered option placeholders.

Examples

com_text = 'tail %{tail} -f file.txt -c %{foo}'
com = Scide::Command.new com_text, :tail => '-n 1000', :foo => 400

com.text_with_options   #=> 'tail -n 1000 -f file.txt -c 400'


83
84
85
86
87
88
89
# File 'lib/scide/command.rb', line 83

def text_with_options
  @text.dup.tap do |s|
    @options.each_pair do |key, value|
      s.gsub! /\%\{#{Regexp.escape key.to_s}\}/, value.to_s
    end
  end
end

#to_screenObject

Returns a representation of this command as a GNU Screen configuration fragment.

This default implementation raises an error and must be overriden by subclasses.



72
73
74
# File 'lib/scide/command.rb', line 72

def to_screen
  raise 'Use a subclass'
end