Class: Clin::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/clin/shell.rb

Overview

Class the offer helper method to interact with the user using the command line

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input: STDIN, output: STDOUT) ⇒ Shell

Returns a new instance of Shell.



15
16
17
18
19
20
21
# File 'lib/clin/shell.rb', line 15

def initialize(input: STDIN, output: STDOUT)
  @in = input
  @out = output
  @yes_or_no_persist = false
  @override_persist = false
  @text = Clin::Text.new
end

Instance Attribute Details

#inObject

Input stream, default: STDIN



7
8
9
# File 'lib/clin/shell.rb', line 7

def in
  @in
end

#outObject

Output stream, default: STDOUT



10
11
12
# File 'lib/clin/shell.rb', line 10

def out
  @out
end

#textObject

Text builder instance that is used to stream



13
14
15
# File 'lib/clin/shell.rb', line 13

def text
  @text
end

Instance Method Details

#ask(statement, default: nil, autocomplete: nil, echo: true, add_to_history: true) ⇒ Object

Ask a question

Parameters:

  • statement (String)
  • default (String) (defaults to: nil)
  • autocomplete (Array|Proc) (defaults to: nil)

    Filter for autocomplete (Need Readline)

  • echo (Boolean) (defaults to: true)

    If false no character will be displayed during input

  • add_to_history (Boolean) (defaults to: true)

    If the answer should be added to history. (Need Readline)



38
39
40
41
42
43
44
45
# File 'lib/clin/shell.rb', line 38

def ask(statement, default: nil, autocomplete: nil, echo: true, add_to_history: true)
  answer = scan(statement, autocomplete: autocomplete, echo: echo, add_to_history: add_to_history)
  if answer.blank?
    default
  else
    answer.strip
  end
end

#choose(statement, choices, default: nil, allow_initials: false) ⇒ Object

Ask a question and expect the result to be in the list of choices Will continue asking until the input is correct or if a default value is supplied then empty will return. If multiple choices start with the same initial ONLY the first one will be able to be selected using its initial

Parameters:

  • statement (String)

    Question to ask

  • choices (Array)

    List of choices

  • default (String) (defaults to: nil)

    Default value if the user put blank value.

  • allow_initials (Boolean) (defaults to: false)

    Allow the user to reply with only the initial of the choice. (e.g. yes/no => y/n)



61
62
63
64
# File 'lib/clin/shell.rb', line 61

def choose(statement, choices, default: nil, allow_initials: false)
  Clin::ShellInteraction::Choose.new(self).run(statement, choices,
                                               default: default, allow_initials: allow_initials)
end

#file_conflict(filename, default: nil, &block) ⇒ Boolean

File conflict helper method. Give the following options to the user

  • yes, Yes for this one

  • no, No for this one

  • all, Yes for all one

  • quit, Quit the program

  • diff, Diff the 2 files

Parameters:

  • filename (String)

    Filename with the conflict

  • block (Block)

    optional block that give the new content in case of diff

Returns:

  • (Boolean)

    If the file should be overwritten.



112
113
114
# File 'lib/clin/shell.rb', line 112

def file_conflict(filename, default: nil, &block)
  Clin::ShellInteraction::FileConflict.new(self).run(filename, default: default, &block)
end

#indent(indent, &block) ⇒ Object

Indent the current output



28
29
30
# File 'lib/clin/shell.rb', line 28

def indent(indent, &block)
  text.indent(indent, &block)
end

#keep?(filename, &block) ⇒ Boolean

File conflict question defaulted to no

Returns:

  • (Boolean)


122
123
124
# File 'lib/clin/shell.rb', line 122

def keep?(filename, &block)
  file_conflict(filename, default: :no, &block)
end

#no?(statement, options = {}) ⇒ Boolean

Yes or no question defaulted to no

Parameters:

  • options (Hash) (defaults to: {})

    Named parameters for yes_or_no

Returns:

  • (Boolean)

See Also:



97
98
99
100
# File 'lib/clin/shell.rb', line 97

def no?(statement, options = {})
  options[:default] = :no
  yes_or_no(statement, **options)
end

#overwrite?(filename, &block) ⇒ Boolean

File conflict question defaulted to yes

Returns:

  • (Boolean)


117
118
119
# File 'lib/clin/shell.rb', line 117

def overwrite?(filename, &block)
  file_conflict(filename, default: :yes, &block)
end

#password(statement, default: nil) ⇒ Object



47
48
49
# File 'lib/clin/shell.rb', line 47

def password(statement, default: nil)
  ask(statement, default: default, echo: false, add_to_history: false)
end

#say(line, indent: '') ⇒ Object



23
24
25
# File 'lib/clin/shell.rb', line 23

def say(line, indent: '')
  @out.puts text.line(line, indent: indent)
end

#select(statement, choices, default: nil) ⇒ Object

Ask a question with a list of possible answer. Answer can either be selected using their name or their index e.g. Select answer:

  1. Choice A

  2. Choice B

  3. Choice C



73
74
75
# File 'lib/clin/shell.rb', line 73

def select(statement, choices, default: nil)
  Clin::ShellInteraction::Select.new(self).run(statement, choices, default: default)
end

#yes?(statement, options = {}) ⇒ Boolean

Yes or no question defaulted to yes

Parameters:

  • options (Hash) (defaults to: {})

    Named parameters for yes_or_no

Returns:

  • (Boolean)

See Also:



89
90
91
92
# File 'lib/clin/shell.rb', line 89

def yes?(statement, options = {})
  options[:default] = :yes
  yes_or_no(statement, **options)
end

#yes_or_no(statement, default: nil, persist: false) ⇒ Object

Expect the user the return yes or no(y/n also works) call to yes_or_no with persist: true will return true instead of asking the user.

Parameters:

  • statement (String)

    Question to ask

  • default (String) (defaults to: nil)

    Default value(yes/no)

  • persist (Boolean) (defaults to: false)

    Add “always” to the choices. When all is selected all the following



82
83
84
# File 'lib/clin/shell.rb', line 82

def yes_or_no(statement, default: nil, persist: false)
  Clin::ShellInteraction::YesOrNo.new(self).run(statement, default: default, persist: persist)
end