tinylinks 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff3190a474ea8e4c3d3591730b77295d59d806034dce59410a5ebc08a9d8c540
4
- data.tar.gz: 2019b549e6959fabc443a06414a3e1567cf5f38954a5a583834dd0bb2d9aef04
3
+ metadata.gz: cb1187f175aed02622d80037b94fb2cf58d0f1cea257a654c8cc8fc4a8868d0b
4
+ data.tar.gz: d622dad03eba29081e838ba6774a36cf5cd25453d55f71f99fdcf070d8e0b1ed
5
5
  SHA512:
6
- metadata.gz: b6ca05746fc41a5f2ee89a37ce46e60079676bd9b7cc5bacf7c7bce7fdf6251b791f59056efc9ef3d923d05bd04560a03a66bb99cbf664aef652d31266bd1484
7
- data.tar.gz: c43ba22c4aeab230476184cc4aaf776b12f40b75e3efeaee1072e24039f39f7265be355dd94744edbc583873d4ca161ea7eb83fc52967b6cc67da05876cceab6
6
+ metadata.gz: 8157693f807253624a2469397aa5c96676d7c97f2ba22762d1aa70712e5eb6dc00d28ac7700cfc2a6ad47940ebe458d899ca608567b175dfccca3f68f43b6920
7
+ data.tar.gz: 79ef0a1e7121d704a93a1620aee9c6ff73931907ab39b614d3ddea196800da79166edc41dea539d709439424b71e82b5b3bbaa7ff73f3681a0886b681f9dc41f
data/lib/tinylinks/cli.rb CHANGED
@@ -9,6 +9,7 @@ module Tinylinks
9
9
  end
10
10
 
11
11
  class_option :help, aliases: "-h", type: :boolean, desc: "Show help for a command"
12
+ class_option :no_color, type: :boolean, default: false, desc: "Disable color output"
12
13
 
13
14
  no_commands do
14
15
  def invoke_command(command, *args)
@@ -19,9 +20,9 @@ module Tinylinks
19
20
  super
20
21
  rescue Client::ApiError => e
21
22
  if e.body.is_a?(Hash) && e.body["errors"]
22
- say_error formatter.errors(e.body)
23
+ say_error error_formatter.errors(e.body)
23
24
  else
24
- say_error e.message
25
+ say_error error_colorizer.red(e.message)
25
26
  end
26
27
  exit 1
27
28
  end
@@ -45,7 +46,7 @@ module Tinylinks
45
46
  end
46
47
  say "Login successful!"
47
48
  rescue RuntimeError => e
48
- say_error "Login failed: #{e.message}"
49
+ say_error error_colorizer.red("Login failed: #{e.message}")
49
50
  exit 1
50
51
  end
51
52
 
@@ -96,7 +97,7 @@ module Tinylinks
96
97
  body[:tags] = options[:tags].split(",").map(&:strip) if options[:tags]
97
98
 
98
99
  if body.empty?
99
- say_error "No changes specified. Use --title, --description, or --tags."
100
+ say_error error_colorizer.red("No changes specified. Use --title, --description, or --tags.")
100
101
  exit 1
101
102
  end
102
103
 
@@ -142,7 +143,7 @@ module Tinylinks
142
143
  @client ||= begin
143
144
  token = Auth.new.token
144
145
  unless token
145
- say_error "Not logged in. Run `tinylinks login` first."
146
+ say_error error_colorizer.red("Not logged in. Run `tinylinks login` first.")
146
147
  exit 1
147
148
  end
148
149
  Client.new(token: token)
@@ -150,11 +151,23 @@ module Tinylinks
150
151
  end
151
152
 
152
153
  def formatter
153
- @formatter ||= Formatter.new
154
+ @formatter ||= Formatter.new(color: color_enabled?($stdout))
155
+ end
156
+
157
+ def error_formatter
158
+ @error_formatter ||= Formatter.new(color: color_enabled?($stderr))
159
+ end
160
+
161
+ def error_colorizer
162
+ @error_colorizer ||= Colorizer.new(enabled: color_enabled?($stderr))
154
163
  end
155
164
 
156
165
  def say_error(message)
157
166
  $stderr.puts message
158
167
  end
168
+
169
+ def color_enabled?(io)
170
+ !options[:no_color] && io.tty? && ENV["NO_COLOR"].nil?
171
+ end
159
172
  end
160
173
  end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinylinks
4
+ class Colorizer
5
+ def initialize(enabled: true)
6
+ @enabled = enabled
7
+ end
8
+
9
+ def bold(text) = wrap(text, 1)
10
+ def dim(text) = wrap(text, 2)
11
+ def red(text) = wrap(text, 31)
12
+ def green(text) = wrap(text, 32)
13
+ def cyan(text) = wrap(text, 36)
14
+
15
+ private
16
+
17
+ def wrap(text, code)
18
+ return text unless @enabled
19
+ "\e[#{code}m#{text}\e[0m"
20
+ end
21
+ end
22
+ end
@@ -2,28 +2,32 @@
2
2
 
3
3
  module Tinylinks
4
4
  class Formatter
5
+ def initialize(color: false)
6
+ @c = Colorizer.new(enabled: color)
7
+ end
8
+
5
9
  def link(data)
6
10
  lines = []
7
- lines << "#{data["title"] || "(untitled)"} [##{data["id"]}]"
8
- lines << " #{data["url"]}"
9
- lines << " #{data["description"]}" if data["description"] && !data["description"].empty?
10
- lines << " tags: #{data["tags"].join(", ")}" if data["tags"] && !data["tags"].empty?
11
+ lines << @c.bold("#{data["title"] || "(untitled)"} [##{data["id"]}]")
12
+ lines << " #{@c.cyan(data["url"])}"
13
+ lines << " #{@c.dim(data["description"])}" if data["description"] && !data["description"].empty?
14
+ lines << " tags: #{@c.green(data["tags"].join(", "))}" if data["tags"] && !data["tags"].empty?
11
15
  lines.join("\n")
12
16
  end
13
17
 
14
18
  def link_list(data)
15
19
  lines = data["links"].map { |l| link(l) }
16
- lines << pagination(data["meta"]) if data["meta"]
20
+ lines << @c.dim(pagination(data["meta"])) if data["meta"]
17
21
  lines.join("\n\n")
18
22
  end
19
23
 
20
24
  def tags(data)
21
- data["tags"].map { |t| "#{t["name"]} (#{t["count"]})" }.join("\n")
25
+ data["tags"].map { |t| "#{@c.green(t["name"])} #{@c.dim("(#{t["count"]})")}" }.join("\n")
22
26
  end
23
27
 
24
28
  def errors(data)
25
29
  data["errors"].flat_map do |field, messages|
26
- messages.map { |msg| "#{field} #{msg}" }
30
+ messages.map { |msg| @c.red("#{field} #{msg}") }
27
31
  end.join("\n")
28
32
  end
29
33
 
data/lib/tinylinks.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tinylinks
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  BASE_URL = "https://links.pati.to"
6
6
  API_BASE = "#{BASE_URL}/api/v1"
7
7
  end
8
8
 
9
+ require_relative "tinylinks/colorizer"
9
10
  require_relative "tinylinks/client"
10
11
  require_relative "tinylinks/auth"
11
12
  require_relative "tinylinks/formatter"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tinylinks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaime Rodas
@@ -34,6 +34,7 @@ files:
34
34
  - lib/tinylinks/auth.rb
35
35
  - lib/tinylinks/cli.rb
36
36
  - lib/tinylinks/client.rb
37
+ - lib/tinylinks/colorizer.rb
37
38
  - lib/tinylinks/formatter.rb
38
39
  homepage: https://github.com/jaimerodas/tinylinks-cli
39
40
  licenses: