From 9bbe687099e0c68f39c26d697fa3b459599ba08c Mon Sep 17 00:00:00 2001 From: Mitchell Stanley Date: Wed, 10 Jul 2019 11:30:15 +0100 Subject: [PATCH] Refactor code --- spec/models/snippet_attribute_spec.cr | 3 +- spec/services/command_builder_spec.cr | 2 +- src/snipline_cli.cr | 2 +- src/snipline_cli/commands/init.cr | 2 -- src/snipline_cli/commands/login.cr | 1 - src/snipline_cli/commands/search.cr | 34 +++++++++++-------- src/snipline_cli/commands/web.cr | 3 +- src/snipline_cli/config/config.cr | 1 - src/snipline_cli/models/snippet.cr | 4 +-- src/snipline_cli/models/token.cr | 1 - src/snipline_cli/services/command_builder.cr | 10 +++--- .../services/create_config_directory.cr | 18 +++++----- src/snipline_cli/services/file_storage.cr | 1 - src/snipline_cli/services/load_snippets.cr | 1 - src/snipline_cli/services/log.cr | 1 - src/snipline_cli/services/snipline_api.cr | 1 - src/snipline_cli/services/store_snippets.cr | 2 -- 17 files changed, 38 insertions(+), 49 deletions(-) diff --git a/spec/models/snippet_attribute_spec.cr b/spec/models/snippet_attribute_spec.cr index 2090c43..6ab4c57 100644 --- a/spec/models/snippet_attribute_spec.cr +++ b/spec/models/snippet_attribute_spec.cr @@ -1,7 +1,7 @@ describe SniplineCli::SnippetAttribute do it "correctly parses snippets attributes" do attributes = SniplineCli::SnippetAttribute.from_json( - %( + %( { "is-pinned": true, "name": "Test", @@ -18,4 +18,3 @@ describe SniplineCli::SnippetAttribute do attributes.documentation.should eq nil end end - diff --git a/spec/services/command_builder_spec.cr b/spec/services/command_builder_spec.cr index 8bd31e2..24797d7 100644 --- a/spec/services/command_builder_spec.cr +++ b/spec/services/command_builder_spec.cr @@ -18,7 +18,7 @@ describe SniplineCli::Services::CommandBuilder do first_password = split[1] # If the second half of the split also has the same password # This checks if the password with the same name is repeating correctly. - "#{split[2]}".includes?(split[1]) + "#{split[2]}".includes?(first_password) end end diff --git a/src/snipline_cli.cr b/src/snipline_cli.cr index 3035b4c..30c4c4c 100644 --- a/src/snipline_cli.cr +++ b/src/snipline_cli.cr @@ -16,7 +16,7 @@ module SniplineCli end def self.config_file - ENV.has_key?("CONFIG_FILE") ? ENV["CONFIG_FILE"] : "~/.config/snipline/config.toml" + ENV.has_key?("CONFIG_FILE") ? ENV["CONFIG_FILE"] : "~/.config/snipline/config.toml" end def self.log diff --git a/src/snipline_cli/commands/init.cr b/src/snipline_cli/commands/init.cr index 283bc00..9a3f719 100644 --- a/src/snipline_cli/commands/init.cr +++ b/src/snipline_cli/commands/init.cr @@ -2,7 +2,6 @@ require "toml" module SniplineCli class Command < Admiral::Command - # The command to initialize Snipline CLI without an active Snipline account. # # This command generates a config file in the requested location. @@ -34,4 +33,3 @@ module SniplineCli register_sub_command :init, Init end end - diff --git a/src/snipline_cli/commands/login.cr b/src/snipline_cli/commands/login.cr index 6a5d4f9..e1f1b4e 100644 --- a/src/snipline_cli/commands/login.cr +++ b/src/snipline_cli/commands/login.cr @@ -2,7 +2,6 @@ require "toml" module SniplineCli class Command < Admiral::Command - # The command to initialize Snipline CLI _with_ an active Snipline account. # # This command generates a config file in the requested location. diff --git a/src/snipline_cli/commands/search.cr b/src/snipline_cli/commands/search.cr index d33b1c3..0b22e12 100644 --- a/src/snipline_cli/commands/search.cr +++ b/src/snipline_cli/commands/search.cr @@ -5,7 +5,6 @@ require "toml" module SniplineCli class Command < Admiral::Command - # The command to search your snippets. # # You can search snippets quickly by using @@ -35,9 +34,6 @@ module SniplineCli property results def run - config = SniplineCli.config - log = SniplineCli.log - search_term : String = arguments.search_term || "" snippets = SniplineCli::Services::LoadSnippets.run @@ -57,17 +53,7 @@ module SniplineCli } end - results = snippets.sort { |snippet_a, snippet_b| - if snippet_a.is_pinned && snippet_b.is_pinned - snippet_a.name <=> snippet_b.name - elsif snippet_a.is_pinned - -1 - elsif snippet_b.is_pinned - 1 - else - snippet_a.name <=> snippet_b.name - end - }.first(flags.limit) + results = sort_results(snippets, flags.limit) unless results.size > 0 puts "No results found." @@ -82,6 +68,24 @@ module SniplineCli puts "\nChoose a snippet" chosen_snippet_index = gets + handle_chosen_snippet(chosen_snippet_index, results) + end + + def sort_results(snippets, limit) + snippets.sort { |snippet_a, snippet_b| + if snippet_a.is_pinned && snippet_b.is_pinned + snippet_a.name <=> snippet_b.name + elsif snippet_a.is_pinned + -1 + elsif snippet_b.is_pinned + 1 + else + snippet_a.name <=> snippet_b.name + end + }.first(limit) + end + + def handle_chosen_snippet(chosen_snippet_index, results) if chosen_snippet_index if chosen_snippet_index.to_i? chosen_snippet_index = (chosen_snippet_index.to_u32 - 1) diff --git a/src/snipline_cli/commands/web.cr b/src/snipline_cli/commands/web.cr index a3e7394..a12289a 100644 --- a/src/snipline_cli/commands/web.cr +++ b/src/snipline_cli/commands/web.cr @@ -4,7 +4,6 @@ require "file" module SniplineCli class Command < Admiral::Command - # The command to start the web version of Snipline CLI # # ```bash @@ -21,7 +20,7 @@ module SniplineCli def run # add_context_storage_type([] of SniplineCli::Snippet) - get "/" do |env| + get "/" do # env.set "snippets", snippets render "src/snipline_cli/templates/index.ecr", "src/snipline_cli/templates/layout.ecr" end diff --git a/src/snipline_cli/config/config.cr b/src/snipline_cli/config/config.cr index 0ce3112..742a6f9 100644 --- a/src/snipline_cli/config/config.cr +++ b/src/snipline_cli/config/config.cr @@ -11,7 +11,6 @@ module SniplineCli # config.get("api.url") # ``` class Config - # Constant that creates a fresh version of itself - for use with self.config. INSTANCE = Config.new diff --git a/src/snipline_cli/models/snippet.cr b/src/snipline_cli/models/snippet.cr index 77ad5dc..59e0c40 100644 --- a/src/snipline_cli/models/snippet.cr +++ b/src/snipline_cli/models/snippet.cr @@ -44,7 +44,7 @@ module SniplineCli real_command.scan(/#(select)?\{\[(.+?)\]\}/) do |m| param_type = (m[1]?) ? m[1] : "variable" - if temp_array.select { |param| param.name == m[2] && param_type == param.type }.size == 0 + if temp_array.count { |param| param.name == m[2] && param_type == param.type } == 0 if param_type == "select" split_equals = m[2].split("=").map { |substring| substring } param_name = split_equals.shift @@ -70,7 +70,7 @@ module SniplineCli end end end - return temp_array + temp_array end # diff --git a/src/snipline_cli/models/token.cr b/src/snipline_cli/models/token.cr index 2ea9d8d..3575993 100644 --- a/src/snipline_cli/models/token.cr +++ b/src/snipline_cli/models/token.cr @@ -9,6 +9,5 @@ module SniplineCli @[JSON::Field(key: "token")] property token : String - end end diff --git a/src/snipline_cli/services/command_builder.cr b/src/snipline_cli/services/command_builder.cr index 7a7473c..a6835b2 100644 --- a/src/snipline_cli/services/command_builder.cr +++ b/src/snipline_cli/services/command_builder.cr @@ -1,6 +1,5 @@ module SniplineCli module Services - # Takes a `Snippet`, asks for users parameter changes, and then returns the output as a string. # # ```crystal @@ -15,14 +14,13 @@ module SniplineCli # end # ``` class CommandBuilder - def self.run(snippet : Snippet, input, output, user_input = [] of String) : String unless snippet.has_params return snippet.real_command end command_builder = snippet.real_command - snippet.interactive_params.each_with_index do |param, index| + snippet.interactive_params.each do |param| case param.type when "select" print("Choose number for #{param.name}:", output) @@ -56,6 +54,7 @@ module SniplineCli end end + # ameba:disable Lint/PercentArrays password_characters = %w{a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S @@ -85,11 +84,11 @@ module SniplineCli end # Handles user input. If an array of is pre-supplied (E.g. for tests) then they are used. - # Otherwise user is asked to input the variables in the terminal. + # Otherwise user is asked to input the variables in the terminal. private def self.gets(output, override_input : String | Nil, *args) case override_input when String - return override_input + override_input else output.gets(*args) end @@ -104,7 +103,6 @@ module SniplineCli {nil, user_input} end end - end end end diff --git a/src/snipline_cli/services/create_config_directory.cr b/src/snipline_cli/services/create_config_directory.cr index 996a5ae..468339b 100644 --- a/src/snipline_cli/services/create_config_directory.cr +++ b/src/snipline_cli/services/create_config_directory.cr @@ -1,13 +1,13 @@ module SniplineCli - module Services - class CreateConfigDirectory - def self.run(file) - directory_name = File.expand_path(File.dirname(file)) - unless File.directory?(directory_name) - SniplineCli.log.debug("Making config directory #{directory_name}") - Dir.mkdir(directory_name) - end - end + module Services + class CreateConfigDirectory + def self.run(file) + directory_name = File.expand_path(File.dirname(file)) + unless File.directory?(directory_name) + SniplineCli.log.debug("Making config directory #{directory_name}") + Dir.mkdir(directory_name) end + end end + end end diff --git a/src/snipline_cli/services/file_storage.cr b/src/snipline_cli/services/file_storage.cr index df7e554..3a43bfa 100644 --- a/src/snipline_cli/services/file_storage.cr +++ b/src/snipline_cli/services/file_storage.cr @@ -2,7 +2,6 @@ require "baked_file_system" module SniplineCli module Services - # FileStorage is used for baking in web assets into the final executable. class FileStorage extend BakedFileSystem diff --git a/src/snipline_cli/services/load_snippets.cr b/src/snipline_cli/services/load_snippets.cr index 3adf44a..d6efce7 100644 --- a/src/snipline_cli/services/load_snippets.cr +++ b/src/snipline_cli/services/load_snippets.cr @@ -1,6 +1,5 @@ module SniplineCli module Services - # LoadSnippets fetches all the snippets from the `snippet.json` file and parses them. # # ```crystal diff --git a/src/snipline_cli/services/log.cr b/src/snipline_cli/services/log.cr index 1af30e3..ffadc3f 100644 --- a/src/snipline_cli/services/log.cr +++ b/src/snipline_cli/services/log.cr @@ -18,7 +18,6 @@ module SniplineCli # env LOG_LEVEL=WARN snipcli sync # ``` class Log - # Constant that creates a fresh version of itself - for use with self.log. INSTANCE = Log.new diff --git a/src/snipline_cli/services/snipline_api.cr b/src/snipline_cli/services/snipline_api.cr index f762e2d..b954611 100644 --- a/src/snipline_cli/services/snipline_api.cr +++ b/src/snipline_cli/services/snipline_api.cr @@ -3,7 +3,6 @@ require "json" module SniplineCli::Services # For talking to the Snipline API. class SniplineApi - # Fetches the user's Snippets. def fetch(&block) config = SniplineCli.config diff --git a/src/snipline_cli/services/store_snippets.cr b/src/snipline_cli/services/store_snippets.cr index 4c360d6..f31b0ab 100644 --- a/src/snipline_cli/services/store_snippets.cr +++ b/src/snipline_cli/services/store_snippets.cr @@ -1,10 +1,8 @@ require "json" module SniplineCli::Services - # For saving Snippets locally. class StoreSnippets - # Takes an array of snippets and saves them to the `snippet.json` file. def store(snippets) config = SniplineCli.config