Skip to content

Commit

Permalink
Merge pull request #2 from Set27/1.0.0-release
Browse files Browse the repository at this point in the history
1.0.0 release
  • Loading branch information
Set27 authored Sep 1, 2024
2 parents 20e7fe9 + c63e2df commit 66b58a9
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 27 deletions.
10 changes: 0 additions & 10 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,3 @@ source "https://rubygems.org"

# Specify your gem's dependencies in tty-prompt-files.gemspec
gemspec

gem "rake", "~> 13.0"

group :test do
gem "rspec", "~> 3.0"
end

group :development do
gem "pry", "~> 0.14.1"
end
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
tty-prompt-files (0.1.2)
tty-prompt-files (1.0.0)
tty-prompt (~> 0.23.1)

GEM
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ prompt.select_element_from_file_system

After element is selected, output will be a element full path.

`select_element_from_file_system` has 3 parameters:
`select_element_from_file_system` has 5 parameters:

```rb
prompt.select_element_from_file_system(pattern: "*", path: ".", text: "")
prompt.select_element_from_file_system(text: "", pattern: "*", path: ".", multi_select: false, **options)
```

`pattern` is determining for what file will be output based on matching the pattern. If you want to display hidden files, use `"{.[^\.]*,*}"` pattern.\
`path` is determining path of the directory, can be relative or absolute. **Note**: relative **should not** end with `/`.\
`text` is text user will see along with elements.
`text` is text user will see along with elements. By default: `""`.\
`pattern` is determining for what file will be output based on matching the pattern. If you want to display hidden files, use `"{.[^\.]*,*}"` pattern. By default: `"*"`.\
`path` is determining path of the directory, can be relative or absolute. By default: `"."`.\
`multi_select` refers to original [API](https://github.com/piotrmurach/tty-prompt?tab=readme-ov-file#263-multi_select). By default: `false`.
`**options` you can add any params from original [API](https://github.com/piotrmurach/tty-prompt?tab=readme-ov-file#2621-cycle). No default value.\

## Contributing

Expand Down
20 changes: 16 additions & 4 deletions lib/tty/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@

module TTY
class Prompt
def select_element_from_file_system(pattern: "*", path: ".", text: "", options: {})
def select_element_from_file_system(text: "", pattern: "*", path: ".", multi_select: false, **options)
pathnames = get_pathnames_from_path(pattern, path)

file_names = pathnames.map do |pathname|
pathname.basename.to_s
end

selected_element = self.select(text, file_names)
unless multi_select
selected_element = self.select(text, file_names, **options)

get_selected_element_full_path(pathnames, selected_element)
return get_selected_element_full_path(pathnames, selected_element)
end

selected_elements = self.multi_select(text, file_names, **options)

selected_elements.map do |selected_element|
get_selected_element_full_path(pathnames, selected_element)
end
end

private

def get_pathnames_from_path(pattern, path)
pathnames = Pathname.glob(path + "/" + pattern)
if path[-1] != "/"
pathnames = Pathname.glob(path + "/" + pattern)
else
pathnames = Pathname.glob(path + pattern)
end

pathnames.map(&:realpath)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tty/files/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module TTY
module Files
VERSION = "0.1.2"
VERSION = "1.0.0"
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "tty-prompt"
require "tty/prompt/test"
require "pathname"
require "pry"

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
Expand Down
58 changes: 52 additions & 6 deletions spec/tty/files_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
expect(TTY::Files::VERSION).not_to be nil
end

describe "simulate file selection from test folder" do
describe "simulate selection from test folder" do
let(:prompt) { TTY::Prompt::Test.new }
let(:input) { StringIO.new }
let(:output) { StringIO.new }
Expand All @@ -24,13 +24,59 @@
allow(Pathname).to receive(:glob).and_return([file_txt_pathname, file_pdf_pathname])
end

it "return file_pdf absolute path" do
prompt.input << press_down << press_enter
prompt.input.rewind
describe "single selection" do
it "return file_txt absolute path" do
prompt.input << press_enter
prompt.input.rewind

result = prompt.select_element_from_file_system(path: test_folder_relative_path)
result = prompt.select_element_from_file_system(path: test_folder_relative_path)

expect(result).to eq(file_pdf_abosolute_path)
expect(result).to eq(file_txt_abosolute_path)
end

it "return file_pdf absolute path" do
prompt.input << press_down << press_enter
prompt.input.rewind

result = prompt.select_element_from_file_system(path: test_folder_relative_path)

expect(result).to eq(file_pdf_abosolute_path)
end
end

describe "multi selection" do
let(:press_space) { "\x20" }

describe "select one file" do
it "return array with file_txt absolute path" do
prompt.input << press_space << press_enter
prompt.input.rewind

result = prompt.select_element_from_file_system(path: test_folder_relative_path, multi_select: true)
expect(result).to match_array(file_txt_abosolute_path)
end
end

describe "select two files" do
it "return array with file_txt and file_pdf ablolutes paths" do
prompt.input << press_space << press_down << press_space << press_enter
prompt.input.rewind

result = prompt.select_element_from_file_system(path: test_folder_relative_path, multi_select: true)
expect(result).to match_array([file_txt_abosolute_path, file_pdf_abosolute_path])
end
end

describe "select none" do
it "return empty array" do
prompt.input << press_enter
prompt.input.rewind

result = prompt.select_element_from_file_system(path: test_folder_relative_path, multi_select: true)

expect(result).to be_empty
end
end
end
end
end

0 comments on commit 66b58a9

Please sign in to comment.