Class: BlogTools::Commands::Lists

Inherits:
Thor
  • Object
show all
Defined in:
lib/blog-tools/commands/lists.rb

Overview

BlogTools::Commands::Lists handles post lists

List handles all of the subcommands dealing with making, editing, and creating lists of blog post ideas

Examples:

Show all posts in a list

blog-tools lists show list

Remove a post from a list

blog-tools lists remove list post

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Lists

Initializes the lists command with stored list data.

This constructor is automatically called by Thor when the lists subcommand is invoked. It loads existing lists from storage into the @lists instance variable.

Parameters:

  • args (Array<String>)

    Command-line arguments passed to the subcommand from Thor

See Also:



30
31
32
33
# File 'lib/blog-tools/commands/lists.rb', line 30

def initialize(*args)
  super
  @lists = Storage.read_lists || {}
end

Instance Method Details

#add(list, post_name) ⇒ Nil

Adds a post to a list

This command takes the name of a list and the name of a post to be added. Assuming that the list exists, it will add the post to the list, and write the result to the list configuration file

Examples:

Adding a post to a list

❯ blog-tools lists add test example
[✓] Added example to test list

Parameters:

  • list (String)

    The name of the list

  • post_name (String)

    The name of the post

Returns:

  • (Nil)


134
135
136
137
138
139
140
# File 'lib/blog-tools/commands/lists.rb', line 134

def add(list, post_name)
  return puts '[!] List not found'.colorize(:red) unless @lists[list]

  @lists[list][:posts][post_name] = { completed: false, in_progress: false }
  Storage.write_lists(@lists)
  puts "[✓] Added #{post_name} to #{list} list".colorize(:green)
end

#create(name) ⇒ Nil

Creates a new list

This command takes in a name for a list, and then creates a new list in the lists configuration file.

Examples:

creating a new list

❯ blog-tools lists create test
[✓] Created list: test

Parameters:

  • name (String)

    The name of the list

Returns:

  • (Nil)


115
116
117
118
119
# File 'lib/blog-tools/commands/lists.rb', line 115

def create(name)
  @lists[name] = { posts: {} }
  Storage.write_lists(@lists)
  puts "[✓] Created list: #{name}".colorize(:green)
end

#delete(list) ⇒ Nil

Deletes a list

This command takes in the name of a list and deletes it from the lists configuration file. It requires user input to confirm the deletion of the list. Once the user gives confirmation, it edits the @lists variable and writes it back to the lists configuration file.

Examples:

Deleting a list

❯ blog-tools lists delete test
[?] Are you sure you want to delete the 'test' list?
[?] This action cannot be undone. Proceed? (y/N)
> y
[✓] Deleted 'test' list

Parameters:

  • list (String)

    The name of the list

Returns:

  • (Nil)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/blog-tools/commands/lists.rb', line 158

def delete(list)
  return puts '[!] List not found'.colorize(:red) unless @lists[list]

  puts "[?] Are you sure you want to delete the '#{list}' list?".colorize(:yellow)
  print "[?] This action cannot be undone. Proceed? (y/N)\n> ".colorize(:yellow)
  input = $stdin.gets.chomp.strip

  case input.downcase
  when 'y'
    @lists.delete(list)
    Storage.write_lists(@lists)
    puts "[✓] Deleted '#{list}' list".colorize(:green)
  when 'n', ''
    puts '[i] Cancelled deletion.'.colorize(:blue)
  else
    puts '[!] Invalid input. Not deleting.'.colorize(:red)
  end
end

#remove(list, post_name) ⇒ Nil

Removes a post from a list

This command take in both a list name and a post name, and then removes the post from the list. It requires user confirmation in order to make the deletion. Once the user confirms, it edits the @lists variable, and writes it to the lists configuration file.

Examples:

❯ blog-tools lists remove example example-post-1
[?] Are you sure you want to delete the post 'example-post-1'?
[?] This action cannot be undone. Proceed? (y/N)
> y
[✓] Deleted 'example-post-1' post

Parameters:

  • list (String)

    The name of the list

  • post_name (String)

    The name of the post

Returns:

  • (Nil)


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/blog-tools/commands/lists.rb', line 194

def remove(list, post_name)
  return puts '[!] List not found'.colorize(:red) unless @lists[list]

  puts "[?] Are you sure you want to delete the post '#{post_name}'?".colorize(:yellow)
  print "[?] This action cannot be undone. Proceed? (y/N)\n> ".colorize(:yellow)
  input = $stdin.gets.chomp.strip

  case input.downcase
  when 'y'
    @lists[list][:posts].delete(post_name)
    Storage.write_lists(@lists)
    puts "[✓] Deleted '#{post_name}' post".colorize(:green)
  when 'n', ''
    puts '[i] Cancelled deletion.'.colorize(:blue)
  else
    puts '[!] Invalid input. Not deleting.'.colorize(:red)
  end
end

#show(list) ⇒ Nil

Shows all posts that are in a list

This command reads the list from the @lists variable, and then shows it You can customize what it shows (completed/uncompleted), and if it shows the status of the post.

Examples:

Basic usage

❯ blog-tools lists show example
EXAMPLE
- example-post-1
- example-post-2
- example-post-3
- example-post-4
- example-post-5

using the --status flag

❯ blog-tools lists show example --status
EXAMPLE
- [✓] example-post-1
- [~] example-post-2
- [✓] example-post-3
- [ ] example-post-4
- [ ] example-post-5

using the --completed flag

❯ blog-tools lists show example --completed
EXAMPLE
- example-post-1
- example-post-3

using the --in-progress flag

❯ blog-tools lists show example --in-progress
EXAMPLE
- example-post-2

Parameters:

  • list (String)

    The name of the list

  • options (Hash)

    a customizable set of options

Returns:

  • (Nil)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/blog-tools/commands/lists.rb', line 79

def show(list)
  return puts '[!] List not found'.colorize(:red) unless @lists[list]

  puts list.upcase
  @lists[list][:posts].each do |item, data|
    next if options[:completed] && !data[:completed]
    next if options[:in_progress] && !data[:in_progress]

    if options[:status]
      status =
        if data[:completed]
          '[✓]'
        elsif data[:in_progress]
          '[~]'
        else
          '[ ]'
        end
      puts "- #{status} #{item}"
    else
      puts "- #{item}"
    end
  end
end

#update(list, post_name) ⇒ Nil

Updates aspects of a post in a list

This command takes a list and post, and requires a flag to be set to run. If no flag is set it will return. The set flag and the parameter with the flag are added into the lists configuration file under the post.

Examples:

Using the --completed flag.

❯ blog-tools lists update example example-post-1 --completed
[✓] Marked 'example-post-1' as complete

Using the --in-progress flag.

❯ blog-tools lists update example example-post-2 --in-progress
[✓] Marked 'example-post-2' as in progress

Using the --path flag.

❯ blog-tools lists update example example-post-1 --path ~/documents/writeup.md
The post content is located at: /home/slavetomints/documents/writeup.md

Using the --tags flag.

❯ blog-tools lists update example example-post-3 --tags 1 2 3
Added the following tags to the post: ["1", "2", "3"]

Parameters:

  • list (String)

    The name of the list

  • post_name (String)

    The name of the post

  • options (Hash)

    a customizable set of options

Returns:

  • (Nil)


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/blog-tools/commands/lists.rb', line 244

def update(list, post_name)
  unless options[:completed] || options[:in_progress] || options[:tags] || options[:path]
    return puts '[!] Please specify a status. Type "blog-tools lists help update" for more info.'.colorize(:red)
  end
  return puts '[!] List not found'.colorize(:red) unless @lists[list]
  return puts '[!] Post not found'.colorize(:red) unless @lists[list][:posts].key?(post_name)

  post = @lists[list][:posts][post_name]

  if options[:completed]
    if post[:completed]
      puts '[!] Post already marked as complete'.colorize(:redd)
    else
      post[:completed] = true
      puts "[✓] Marked '#{post_name}' as complete".colorize(:green)
    end
  end

  if options[:in_progress]
    if post[:in_progress]
      puts '[!] Post already marked as in progress'.colorize(:red)
    else
      post[:in_progress] = true
      puts "[✓] Marked '#{post_name}' as in progress".colorize(:green)
    end
  end
  post[:tags] = options[:tags] if options[:tags]
  puts "Added the following tags to the post: #{options[:tags]}" if options[:tags]
  post[:path] = options[:path] if options[:path]
  puts "The post content is located at: #{options[:path]}" if options[:path]

  Storage.write_lists(@lists)
  nil
end