Class: Octodown::FileChooser

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(logger:) ⇒ FileChooser

Returns a new instance of FileChooser.



65
66
67
68
# File 'lib/octodown/support/file_chooser.rb', line 65

def initialize(logger:)
  @logger = logger
  @prompt = TUI
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



63
64
65
# File 'lib/octodown/support/file_chooser.rb', line 63

def logger
  @logger
end

#promptObject (readonly)

Returns the value of attribute prompt.



63
64
65
# File 'lib/octodown/support/file_chooser.rb', line 63

def prompt
  @prompt
end

Instance Method Details

#abort_no_files_found!Object



76
77
78
79
80
81
82
# File 'lib/octodown/support/file_chooser.rb', line 76

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_filesObject



84
85
86
87
88
89
90
# File 'lib/octodown/support/file_chooser.rb', line 84

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

#callObject



70
71
72
73
74
# File 'lib/octodown/support/file_chooser.rb', line 70

def call
  choices = all_markdown_files
  choice = prompt.select('Which file would you like to edit?', choices)
  File.open(choice, 'r')
end