octodown 1.8.1 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 873f8e9bccb9cea287afa902146a4932ca71e83a
4
- data.tar.gz: 5d88a4c9b7a26f981d10671a76a7a5bfc5271959
3
+ metadata.gz: 89c51bf66ccf645f30d89b02527b0d7d4f976d69
4
+ data.tar.gz: 6bf5907da675618e307c7fbb6613ea93f8f66803
5
5
  SHA512:
6
- metadata.gz: 358cc97c065769a35c69c608ab41268d7eb2d4fa23fd84b92702cd3f727e51a8e486249609bc7d6da1f1fb91bd59ff4c63ce4a00c7555269b19b3ebc914781ae
7
- data.tar.gz: 720403b3d42ce1b2c1b83f908aebab01dca2e3d8a8c6783b165951a8acfd3495c8cf76823dfd1c55f2de5baa521b3136d85a27df7f2773ca41ac1854fd5f1520
6
+ metadata.gz: 661e7500c5ab971eed08b57c2e7d4550944a850769722eaf0b3aedd8fedde103ba1c003c7c633c813c501182a2aa041744d61231350d88d4bfe974a6ad766b04
7
+ data.tar.gz: 01a6a0e86ea8626a9b2676d4f06947db7e5a31b0991d36e2fe562c7b9d9773bd028e51c0efa9b7a995322cc1d777cae98e95defac2fba0097975b4e0b98459c4
data/bin/octodown CHANGED
@@ -71,16 +71,12 @@ OptionParser.new do |opts|
71
71
  end
72
72
  end.parse!
73
73
 
74
- require 'tty-prompt'
75
-
76
- TUI = TTY::Prompt.new(enable_color: true)
77
-
78
74
  options[:file] = if ARGF.file == STDIN && options[:stdin]
79
75
  STDIN
80
76
  elsif ARGF.file != STDIN
81
77
  ARGF.file
82
78
  else
83
- FileChooser.new(prompt: TUI).choose_file
79
+ Octodown::FileChooser.new(logger: options[:logger]).call
84
80
  end
85
81
 
86
82
  Octodown.call options
@@ -1,26 +1,11 @@
1
- class FileChooser
2
- attr_reader :prompt
1
+ require 'English'
2
+ require 'tty-prompt'
3
3
 
4
- def initialize(prompt:)
5
- @prompt = prompt
6
- end
7
-
8
- def choose_file
9
- choices = all_markdown_files
10
- choice = prompt.select('Which markdown file would you like to edit?', choices)
11
- File.open(choice, 'r')
12
- end
13
-
14
- def abort_no_files_found!
15
- prompt.error 'We could not find any markdown files in this folder.'
16
- puts
17
- prompt.error 'Try passing the file explicitly such as, i.e:'
18
- prompt.error ' $ octodown README.md'
19
- exit 1
20
- end
4
+ module Octodown
5
+ class FileChooser
6
+ TUI = ::TTY::Prompt.new(enable_color: true)
21
7
 
22
- def all_markdown_files
23
- extensions = %w[markdown
8
+ EXTENSIONS = %w[markdown
24
9
  mdown
25
10
  mkdn
26
11
  md
@@ -29,12 +14,77 @@ class FileChooser
29
14
  mdtxt
30
15
  mdtext
31
16
  text
32
- Rmd]
17
+ Rmd].freeze
18
+
19
+ class MarkdownFileList
20
+ class Git
21
+ def call
22
+ ext_args = EXTENSIONS.map { |ext| "**/*.#{ext} *.#{ext}" }.join(' ')
23
+ `git ls-files --cached --others -z #{ext_args}`.split("\x0").uniq
24
+ end
25
+
26
+ def runnable?
27
+ `git rev-parse --is-inside-work-tree`
28
+ $CHILD_STATUS.success?
29
+ rescue StandardError
30
+ false
31
+ end
32
+ end
33
+
34
+ class Glob
35
+ def call
36
+ Dir.glob "*.{#{EXTENSIONS.join(',')}}"
37
+ end
38
+
39
+ def runnable?
40
+ true
41
+ end
42
+ end
43
+
44
+ attr_reader :logger
45
+
46
+ def initialize(logger)
47
+ @logger = logger
48
+ end
49
+
50
+ def call
51
+ logger.debug("File choose strategy: #{winning_strategy.class.name}")
52
+ winning_strategy.call
53
+ end
54
+
55
+ def winning_strategy
56
+ strats = [Git.new, Glob.new]
57
+ @winning_strategy ||= strats.find(&:runnable?)
58
+ end
59
+ end
60
+
61
+ attr_reader :prompt, :logger
62
+
63
+ def initialize(logger:)
64
+ @logger = logger
65
+ @prompt = TUI
66
+ end
67
+
68
+ def call
69
+ choices = all_markdown_files
70
+ choice = prompt.select('Which file would you like to edit?', choices)
71
+ File.open(choice, 'r')
72
+ end
73
+
74
+ def abort_no_files_found!
75
+ prompt.error 'We could not find any markdown files in this folder.'
76
+ puts
77
+ prompt.error 'Try passing the file explicitly such as, i.e:'
78
+ prompt.error ' $ octodown README.md'
79
+ exit 1
80
+ end
33
81
 
34
- choices = Dir.glob "**/*.{#{extensions.join(',')}}"
82
+ def all_markdown_files
83
+ choices = MarkdownFileList.new(logger).call
35
84
 
36
- abort_no_files_found! if choices.empty?
85
+ abort_no_files_found! if choices.empty?
37
86
 
38
- choices.sort_by! { |c| c.split(File::SEPARATOR).length }
87
+ choices.sort_by! { |c| c.split(File::SEPARATOR).length }
88
+ end
39
89
  end
40
90
  end
@@ -1,3 +1,3 @@
1
1
  module Octodown
2
- VERSION = '1.8.1'.freeze
2
+ VERSION = '1.8.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octodown
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.1
4
+ version: 1.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer