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

Ordering parameter and function created #15

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions lib/bookmarker/bookmark.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
defmodule Bookmarker.Bookmark do
def drop_at(bookmarks, []), do: bookmarks

def drop_at(bookmarks, [dir | []]) do
bookmarks
|> Map.update("children", [], fn children ->
Enum.filter children, fn child -> child["name"] != dir end
end)
end

def drop_at(bookmarks, [dir | tail]) do
bookmarks
|> Map.update("children", [], fn children ->
Expand All @@ -18,7 +20,9 @@ defmodule Bookmarker.Bookmark do
end)
end)
end

def get_at(bookmarks, nil), do: bookmarks

def get_at(bookmarks, dir) do
bookmarks
|> Map.update("children", [], fn children ->
Expand Down
7 changes: 6 additions & 1 deletion lib/bookmarker/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Bookmarker.CLI do
help: :boolean,
file: :string,
title: :string,
order: :boolean,
description: :string,
timestamp: :boolean,
ignore: :keep,
Expand All @@ -16,6 +17,7 @@ defmodule Bookmarker.CLI do
h: :help,
f: :file,
t: :title,
or: :order,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be best to leave a letter only version '-o' instead of '-or'.

Copy link
Author

@DanAmador DanAmador Oct 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to use or as the char "o" was already being used for the "output" key in line 24.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. My bad.

Maybe we could rename the flag to "--sort", "-s".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I'll fix that in a sec 👍

d: :description,
i: :ignore,
p: :path,
Expand Down Expand Up @@ -64,7 +66,8 @@ defmodule Bookmarker.CLI do
Keyword.get(params, :path),
output:
Keyword.get(params, :output, :stdio),
}
order: Keyword.get(params, :order, false),
}
end
end

Expand All @@ -78,6 +81,8 @@ defmodule Bookmarker.CLI do
Default: Google Chrome Bookmarks
-d, --description Set a description for the rendered markdown.
Default: Generated by Bookmarker
-or, --order Set if the list is ordered
Default: false
--no-timestamp Prevent appending of build datetime after description.
Default: use timestamp
-i, --ignore Ignore folders. You may set this multiple times.
Expand Down
10 changes: 10 additions & 0 deletions lib/bookmarker/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ defmodule Bookmarker.Runner do
get_bookmarks(config.file)
|> ignore_paths(config.ignore)
|> restrict_to(config.path)
|> order_bookmarks
|> render_markdown(Map.take(config, [:title, :description, :timestamp?]))
|> output(config.output)
end

defp order_bookmarks(bookmarks) do
if bookmarks.order do
Enum.sort_by(bookmarks, fn(b) -> b.title end)
else
bookmarks
end
end

def get_bookmarks(file) do
file
|> Path.expand
Expand Down Expand Up @@ -40,6 +49,7 @@ defmodule Bookmarker.Runner do
File.write! dest, rendered
IO.puts "Saved output at #{dest}"
end

def output(rendered, _target) do
IO.puts rendered
end
Expand Down