Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0.0 release #2

Merged
merged 7 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading