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.



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

def initialize(input: STDIN, output: STDOUT)
  @in = input
  @out = output
  @yes_or_no_persist = false
  @override_persist = false
end

Instance Attribute Details

#inObject

Input stream, default: STDIN



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

def in
  @in
end

#outObject

Output stream, default: STDOUT



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

def out
  @out
end

Instance Method Details

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

Ask a question

Parameters:

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

    Filter for autocomplete



22
23
24
25
26
27
28
29
# File 'lib/clin/shell.rb', line 22

def ask(statement, default: nil, autocomplete: nil)
  answer = scan(statement, autocomplete: autocomplete)
  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)



41
42
43
44
# File 'lib/clin/shell.rb', line 41

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.



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

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

#keep?(filename, &block) ⇒ Boolean

File conflict question defaulted to no

Returns:

  • (Boolean)


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

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:



66
67
68
69
# File 'lib/clin/shell.rb', line 66

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)


86
87
88
# File 'lib/clin/shell.rb', line 86

def overwrite?(filename, &block)
  file_conflict(filename, default: :yes, &block)
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:



58
59
60
61
# File 'lib/clin/shell.rb', line 58

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



51
52
53
# File 'lib/clin/shell.rb', line 51

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