Class: Scide::Command
- Inherits:
-
Object
- Object
- Scide::Command
- 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
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
The options given to this command.
Class Method Summary collapse
-
.resolve(window, contents) ⇒ Object
Returns a new command for the given window.
Instance Method Summary collapse
-
#initialize(contents, options = {}) ⇒ Command
constructor
Returns a new command with the given options.
-
#text_with_options ⇒ Object
Returns the text of this command with filtered option placeholders.
-
#to_screen ⇒ Object
Returns a representation of this command as a GNU Screen configuration fragment.
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, = {} # 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 end |
Instance Attribute Details
#options ⇒ Object (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 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 sameCOMMAND
as for string initialization above. -
:contents => string or other
is the sameCONTENTS
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_options ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/scide/command.rb', line 83 def @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_screen ⇒ Object
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 |