From 69ad77c40c1df353aca905acbadcdfb771727475 Mon Sep 17 00:00:00 2001 From: Set27 Date: Sun, 1 Sep 2024 17:56:01 +0300 Subject: [PATCH 1/7] Minor: store gems in gemspec --- Gemfile | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Gemfile b/Gemfile index fd6ee7b..ed1569f 100644 --- a/Gemfile +++ b/Gemfile @@ -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 From f0ac9d736b432ca01aa93198b005ac0a8c0480ad Mon Sep 17 00:00:00 2001 From: Set27 Date: Sun, 1 Sep 2024 18:06:22 +0300 Subject: [PATCH 2/7] Feat: check slash for path --- lib/tty/files.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/tty/files.rb b/lib/tty/files.rb index cade9e3..7224e4b 100644 --- a/lib/tty/files.rb +++ b/lib/tty/files.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative "files/version" +require 'pry' module TTY class Prompt @@ -19,7 +20,11 @@ def select_element_from_file_system(pattern: "*", path: ".", text: "", options: 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 From e5b82b0f0e1184d8190d7f1590030ae70c3faaa0 Mon Sep 17 00:00:00 2001 From: Set27 Date: Sun, 1 Sep 2024 18:18:41 +0300 Subject: [PATCH 3/7] Feat: add test for file_txt --- spec/tty/files_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spec/tty/files_spec.rb b/spec/tty/files_spec.rb index fdf9ee7..07356b6 100644 --- a/spec/tty/files_spec.rb +++ b/spec/tty/files_spec.rb @@ -24,6 +24,15 @@ allow(Pathname).to receive(:glob).and_return([file_txt_pathname, file_pdf_pathname]) end + 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) + + 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 From 64478ce6884949ed2a223d4ae25a52f7273137bc Mon Sep 17 00:00:00 2001 From: Set27 Date: Sun, 1 Sep 2024 19:43:36 +0300 Subject: [PATCH 4/7] Feat: support multi_select and all other API options --- lib/tty/files.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/tty/files.rb b/lib/tty/files.rb index 7224e4b..a66cb99 100644 --- a/lib/tty/files.rb +++ b/lib/tty/files.rb @@ -1,26 +1,33 @@ # frozen_string_literal: true require_relative "files/version" -require 'pry' 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) - if path[-1] != '/' + if path[-1] != "/" pathnames = Pathname.glob(path + "/" + pattern) else pathnames = Pathname.glob(path + pattern) From 4c1131085e01ca410c030c3017267992ff0a8eb7 Mon Sep 17 00:00:00 2001 From: Set27 Date: Sun, 1 Sep 2024 19:43:49 +0300 Subject: [PATCH 5/7] Feat: add tests --- spec/spec_helper.rb | 1 + spec/tty/files_spec.rb | 59 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 17bec12..eaf7dac 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/tty/files_spec.rb b/spec/tty/files_spec.rb index 07356b6..39c4b3c 100644 --- a/spec/tty/files_spec.rb +++ b/spec/tty/files_spec.rb @@ -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 } @@ -24,22 +24,59 @@ allow(Pathname).to receive(:glob).and_return([file_txt_pathname, file_pdf_pathname]) end - it "return file_txt absolute path" do - prompt.input << 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_txt_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 - it "return file_pdf absolute path" do - prompt.input << press_down << press_enter - prompt.input.rewind + 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) + result = prompt.select_element_from_file_system(path: test_folder_relative_path, multi_select: true) - expect(result).to eq(file_pdf_abosolute_path) + expect(result).to be_empty + end + end end end end From e116856ab76d78ca00cae3f97137b18b0ced5c01 Mon Sep 17 00:00:00 2001 From: Set27 Date: Sun, 1 Sep 2024 20:00:08 +0300 Subject: [PATCH 6/7] Docs: update readme --- README.md | 12 +++++++----- lib/tty/files/version.rb | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fbd6973..1a2db9c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/tty/files/version.rb b/lib/tty/files/version.rb index 9cd4cd5..be76596 100644 --- a/lib/tty/files/version.rb +++ b/lib/tty/files/version.rb @@ -2,6 +2,6 @@ module TTY module Files - VERSION = "0.1.2" + VERSION = "1.0.0" end end From c63e2df6b69d6cc937331307822759904529988b Mon Sep 17 00:00:00 2001 From: Set27 Date: Sun, 1 Sep 2024 20:06:25 +0300 Subject: [PATCH 7/7] Fix: gemfile lock --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index d0b5b51..7ed1b8f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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