Class: Octodown::FileChooser
- Inherits:
-
Object
- Object
- Octodown::FileChooser
- Defined in:
- lib/octodown/support/file_chooser.rb
Defined Under Namespace
Classes: MarkdownFileList
Constant Summary collapse
- TUI =
::TTY::Prompt.new(enable_color: true)
- EXTENSIONS =
%w[markdown mdown mkdn md mkd mdwn mdtxt mdtext text Rmd].freeze
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
Instance Method Summary collapse
- #abort_no_files_found! ⇒ Object
- #all_markdown_files ⇒ Object
- #call ⇒ Object
-
#initialize(logger:) ⇒ FileChooser
constructor
A new instance of FileChooser.
Constructor Details
#initialize(logger:) ⇒ FileChooser
Returns a new instance of FileChooser.
63 64 65 66 |
# File 'lib/octodown/support/file_chooser.rb', line 63 def initialize(logger:) @logger = logger @prompt = TUI end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
61 62 63 |
# File 'lib/octodown/support/file_chooser.rb', line 61 def logger @logger end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
61 62 63 |
# File 'lib/octodown/support/file_chooser.rb', line 61 def prompt @prompt end |
Instance Method Details
#abort_no_files_found! ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/octodown/support/file_chooser.rb', line 74 def abort_no_files_found! prompt.error 'We could not find any markdown files in this folder.' puts prompt.error 'Try passing the file explicitly such as, i.e:' prompt.error ' $ octodown README.md' exit 1 end |
#all_markdown_files ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/octodown/support/file_chooser.rb', line 82 def all_markdown_files choices = MarkdownFileList.new(logger).call abort_no_files_found! if choices.empty? choices.sort_by! { |c| c.split(File::SEPARATOR).length } end |
#call ⇒ Object
68 69 70 71 72 |
# File 'lib/octodown/support/file_chooser.rb', line 68 def call choices = all_markdown_files choice = prompt.select('Which file would you like to edit?', choices) File.open(choice, 'r') end |