Skip to content

Commit

Permalink
Use git to choose files when available
Browse files Browse the repository at this point in the history
  • Loading branch information
ianks committed May 4, 2018
1 parent 15c5f15 commit f58adb5
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 30 deletions.
6 changes: 1 addition & 5 deletions bin/octodown
Expand Up @@ -71,16 +71,12 @@ OptionParser.new do |opts|
end
end.parse!

require 'tty-prompt'

TUI = TTY::Prompt.new(enable_color: true)

options[:file] = if ARGF.file == STDIN && options[:stdin]
STDIN
elsif ARGF.file != STDIN
ARGF.file
else
FileChooser.new(prompt: TUI).choose_file
Octodown::FileChooser.new(logger: options[:logger]).call
end

Octodown.call options
100 changes: 75 additions & 25 deletions lib/octodown/support/file_chooser.rb
@@ -1,26 +1,11 @@
class FileChooser
attr_reader :prompt
require 'English'
require 'tty-prompt'

def initialize(prompt:)
@prompt = prompt
end

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

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
module Octodown
class FileChooser
TUI = ::TTY::Prompt.new(enable_color: true)

def all_markdown_files
extensions = %w[markdown
EXTENSIONS = %w[markdown
mdown
mkdn
md
Expand All @@ -29,12 +14,77 @@ def all_markdown_files
mdtxt
mdtext
text
Rmd]
Rmd].freeze

class MarkdownFileList
class Git
def call
ext_args = EXTENSIONS.map { |ext| "**/*.#{ext} *.#{ext}" }.join(' ')
`git ls-files --cached --others -z #{ext_args}`.split("\x0").uniq
end

def runnable?
`git rev-parse --is-inside-work-tree`
$CHILD_STATUS.success?
rescue StandardError
false
end
end

class Glob
def call
Dir.glob "*.{#{EXTENSIONS.join(',')}}"
end

def runnable?
true
end
end

attr_reader :logger

def initialize(logger)
@logger = logger
end

def call
logger.debug("File choose strategy: #{winning_strategy.class.name}")
winning_strategy.call
end

def winning_strategy
strats = [Git.new, Glob.new]
@winning_strategy ||= strats.find(&:runnable?)
end
end

attr_reader :prompt, :logger

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

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

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

choices = Dir.glob "**/*.{#{extensions.join(',')}}"
def all_markdown_files
choices = MarkdownFileList.new(logger).call

abort_no_files_found! if choices.empty?
abort_no_files_found! if choices.empty?

choices.sort_by! { |c| c.split(File::SEPARATOR).length }
choices.sort_by! { |c| c.split(File::SEPARATOR).length }
end
end
end

0 comments on commit f58adb5

Please sign in to comment.