Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitchell Stanley committed Jul 10, 2019
1 parent 2cf4b74 commit 9bbe687
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 49 deletions.
3 changes: 1 addition & 2 deletions spec/models/snippet_attribute_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe SniplineCli::SnippetAttribute do
it "correctly parses snippets attributes" do
attributes = SniplineCli::SnippetAttribute.from_json(
%(
%(
{
"is-pinned": true,
"name": "Test",
Expand All @@ -18,4 +18,3 @@ describe SniplineCli::SnippetAttribute do
attributes.documentation.should eq nil
end
end

2 changes: 1 addition & 1 deletion spec/services/command_builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/snipline_cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/snipline_cli/commands/init.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -34,4 +33,3 @@ module SniplineCli
register_sub_command :init, Init
end
end

1 change: 0 additions & 1 deletion src/snipline_cli/commands/login.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 19 additions & 15 deletions src/snipline_cli/commands/search.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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."
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/snipline_cli/commands/web.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ require "file"

module SniplineCli
class Command < Admiral::Command

# The command to start the web version of Snipline CLI
#
# ```bash
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/snipline_cli/config/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/snipline_cli/models/snippet.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -70,7 +70,7 @@ module SniplineCli
end
end
end
return temp_array
temp_array
end

#
Expand Down
1 change: 0 additions & 1 deletion src/snipline_cli/models/token.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ module SniplineCli

@[JSON::Field(key: "token")]
property token : String

end
end
10 changes: 4 additions & 6 deletions src/snipline_cli/services/command_builder.cr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -104,7 +103,6 @@ module SniplineCli
{nil, user_input}
end
end

end
end
end
18 changes: 9 additions & 9 deletions src/snipline_cli/services/create_config_directory.cr
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion src/snipline_cli/services/file_storage.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/snipline_cli/services/load_snippets.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module SniplineCli
module Services

# LoadSnippets fetches all the snippets from the `snippet.json` file and parses them.
#
# ```crystal
Expand Down
1 change: 0 additions & 1 deletion src/snipline_cli/services/log.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion src/snipline_cli/services/snipline_api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/snipline_cli/services/store_snippets.cr
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 9bbe687

Please sign in to comment.