kuzya 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9934032c8c87c655de6c945864e05493774586afaa993348293567cee78fd9fb
4
+ data.tar.gz: 38009f41e6cf724ba37e05618b7afa5a5e26e02cda414c09fba03ede81f8032e
5
+ SHA512:
6
+ metadata.gz: 12e3cd198b290369fe94ae30a00f97c93053ae5b609767b960d96ce2289f8e91c713f5f6e367cf9989969ffcc2a7aeae041493f515f822fe5fa7207b74b20770
7
+ data.tar.gz: ff65055331b13bc43c6c77f275263fde3d1bf14900a44ad41cebb807224e67826d2690c03d42aee4171a3a5cfdfa4a29b77f988c455c9aae4738850e8190262d
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Kuzya
2
+
3
+ __Kuzya__ - application to quickly open links from you terminal without mouse.
4
+
5
+ ___
6
+
7
+ ## How to install
8
+
9
+ ### by rubygems
10
+
11
+ To install Kuzya as gem run.
12
+ ```bash
13
+ gem install kuzya
14
+ ```
15
+ You will need Ruby 2.4.0 or higher
16
+
17
+ If you using RVM you can install kuzya to @global gemset (but keep in mind that this per ruby interpeter, not for all rubies)
18
+ ```bash
19
+ rvm @global do gem install kuzya
20
+ ```
21
+
22
+ ___
23
+
24
+ ## How to use
25
+
26
+ ### Open links
27
+
28
+ To open the link just run
29
+ ```
30
+ kuzya https://example.com
31
+ ```
32
+ Kuzya will open it on your browser.
33
+
34
+ ### Adding Shortcuts
35
+
36
+ You can make shortcuts to open links faster!
37
+ Run
38
+ ```bash
39
+ kuzya add SHORTCUT_NAME https://example.com
40
+ ```
41
+ You also can create nested shortcuts, just run
42
+ ```bash
43
+ kuzya add PARENT_SHORTCUT CHILD_SHORTCUT some_text
44
+ ```
45
+ (Note: you need to create parent shortcuts firstly to have ability to add nested ones)
46
+ (Note #2: you can't create shortcuts named __add__ | __remove__ | __list__ | __help__ )
47
+
48
+ Example:
49
+
50
+ ```bash
51
+ kuzya add gh https://github.com`
52
+ kuzya add gh rb ruby
53
+ kuzya gh rb rake # Kuzya will open https://github.com/ruby/rake
54
+ ```
55
+
56
+ ### Removing Shurtcuts
57
+
58
+ You can remove shortcuts by
59
+ ```bash
60
+ kuzya remove SHORTCUT_NAME
61
+ ```
62
+ But please keep in mind that if shorcut contains another shortcuts they aslo will be Add frozen_string_literal. Cleanup. Add normal path to config file
63
+
64
+ ### List saved shorcuts
65
+
66
+ To see the list of your shorcuts run
67
+ ```bash
68
+ kuzya list
69
+ ```
70
+
71
+ ### Help command
72
+
73
+ Kuzya can show you information about itself, run
74
+ ```bash
75
+ kuzya help
76
+ ```
77
+
78
+ ___
79
+ ___
80
+
81
+ https://opensource.org/licenses/isc-license.txt
82
+
83
+ ISC License (ISC)
84
+
85
+ Copyright (c) 2021 Dmitry Krushinsky
86
+
87
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
88
+
89
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
data/bin/kuzya ADDED
@@ -0,0 +1,4 @@
1
+ #!usr/bin/env ruby
2
+
3
+ require 'kuzya'
4
+ Kuzya::CLI.call(ARGV)
data/kuzya.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'kuzya'
3
+ s.version = '0.0.1'
4
+ s.summary = 'Utility to open urls in browser from terminal.'
5
+ s.description = "Wrapper for 'launchy' gem to using shortcuts."
6
+ s.author = 'Dmitry Krushinsky'
7
+ s.email = 'd.krushinsky.00@gmail.com'
8
+ s.files = `git ls-files`.split("\n")
9
+ s.license = 'MIT'
10
+
11
+ s.add_dependency 'launchy'
12
+
13
+ s.executables = ['kuzya']
14
+ end
data/lib/kuzya.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'kuzya/core_ext/hash/deep_merge'
2
+ require 'kuzya/core_ext/hash/deep_compact'
3
+ require 'kuzya/core_ext/string/formatting'
4
+
5
+ module Kuzya
6
+ SUMMARY = "#{'Kuzya'.bold} - app to open link from you lovely terminal!".freeze
7
+ end
8
+
9
+ require 'kuzya/service'
10
+ require 'kuzya/config'
11
+ require 'kuzya/url_builder'
12
+ require 'kuzya/commands'
13
+ require 'kuzya/cli'
data/lib/kuzya/cli.rb ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kuzya
4
+ class CLI < Kuzya::Service
5
+ def initialize(args)
6
+ @args = args
7
+ end
8
+
9
+ def call
10
+ config = Kuzya::Config.new
11
+
12
+ build_command.call(args, config)
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :args
18
+
19
+ def build_command
20
+ command = Kuzya::Commands.reserved?(args.first) ? args.first : 'open'
21
+
22
+ Object.const_get("Kuzya::Commands::#{command.capitalize}")
23
+ end
24
+
25
+ def args_without_command
26
+ args.dup.tap { |ary| ary.shift }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kuzya
4
+ module Commands
5
+ RESERVED_COMMANDS = %w[
6
+ add
7
+ remove
8
+ list
9
+ help
10
+ ].freeze
11
+
12
+ def self.reserved?(first_arg)
13
+ RESERVED_COMMANDS.include? first_arg
14
+ end
15
+ end
16
+ end
17
+
18
+ require 'kuzya/commands/base'
19
+ require 'kuzya/commands/open'
20
+ require 'kuzya/commands/add'
21
+ require 'kuzya/commands/remove'
22
+ require 'kuzya/commands/list'
23
+ require 'kuzya/commands/help'
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kuzya
4
+ module Commands
5
+ class Add < Kuzya::Commands::Base
6
+ class << self
7
+ def info
8
+ <<-INFO
9
+ #{'add'.bold} - to add new shortcut use #{'kuzya add short www.example.com'.italic}.
10
+ You can adds shortcuts inside other shurtcuts, e.g.
11
+ #{'kuzya add stack https://stackoverflow.com'.italic}
12
+ #{'kuzya add stack q questions'.italic}
13
+ #{'kuzya add stack q active ?tab=Active'.italic}
14
+ And now if run '#{'kuzya stack q active'.italic}' you will see all active questions
15
+ Note: You can't add first-level shortcuts named as [add, remove, list, help]
16
+ INFO
17
+ end
18
+
19
+ def prepare(args, config)
20
+ [args_without_command(args), config]
21
+ end
22
+ end
23
+
24
+ def initialize(args, config)
25
+ @args = args
26
+ @config = config
27
+ end
28
+
29
+ def call
30
+ config.add(args)
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :args, :config
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kuzya
4
+ module Commands
5
+ class Base
6
+ class << self
7
+ def info
8
+ raise NotImplementedError
9
+ end
10
+
11
+ def call(args, config)
12
+ new(*prepare(args, config)).call
13
+ end
14
+
15
+ # Returns array of arguments that will be passed to constructor
16
+ def prepare(_args, _config)
17
+ raise NotImplementedError
18
+ end
19
+
20
+ def args_without_command(args)
21
+ args.dup.tap { |ary| ary.shift }
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kuzya
4
+ module Commands
5
+ class Help < Kuzya::Commands::Base
6
+ SUPPORTED_COMMANDS = [
7
+ Kuzya::Commands::Open,
8
+ Kuzya::Commands::Help,
9
+ Kuzya::Commands::Add,
10
+ Kuzya::Commands::Remove,
11
+ Kuzya::Commands::List
12
+ ].freeze
13
+
14
+ class << self
15
+ def info
16
+ <<-INFO
17
+ #{'help'.bold} - to read info about kuzya's commands run '#{'kuzya help'.italic}'
18
+ INFO
19
+ end
20
+
21
+ def prepare(_args, _config)
22
+ # Do nothing
23
+ end
24
+ end
25
+
26
+ def initialize
27
+ end
28
+
29
+ def call
30
+ puts "#{Kuzya::SUMMARY}\n\n"
31
+
32
+ SUPPORTED_COMMANDS.each do |command|
33
+ puts "#{command.info.to_s}\n"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kuzya
4
+ module Commands
5
+ class List < Kuzya::Commands::Base
6
+ class << self
7
+ def info
8
+ <<-INFO
9
+ #{'list'.bold} - to see all shortcuts run '#{'kuzya list'.italic}'
10
+ INFO
11
+ end
12
+
13
+ def prepare(_args, config)
14
+ [config]
15
+ end
16
+ end
17
+
18
+ def initialize(config)
19
+ @config = config
20
+ end
21
+
22
+ def call
23
+ resolved = []
24
+ resolve_shortcuts([], [], fetch_keys(config.shortcuts), resolved)
25
+
26
+ resolved.each do |shortcuts|
27
+ puts "#{shortcuts.join(' ').bold}: #{Kuzya::UrlBuilder.call(shortcuts, config).italic}"
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :config
34
+
35
+ def fetch_keys(hash)
36
+ hash.dup.tap { |h| h.delete('_url') }.map do |key, value|
37
+ next key unless value.is_a?(Hash)
38
+
39
+ result = [key, fetch_keys(value)].reject(&:empty?)
40
+ result.length == 1 ? key : result
41
+ end.reject(&:empty?)
42
+ end
43
+
44
+ def resolve_shortcuts(com, prev_com, shortcuts, resolved)
45
+ shortcuts.each do |arg|
46
+ if arg.is_a?(String)
47
+ com = prev_com.dup.concat([arg])
48
+ resolved.push(com)
49
+ next
50
+ end
51
+
52
+ prev_com = com.dup
53
+ resolve_shortcuts(com, prev_com, arg, resolved)
54
+ com = []
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'launchy'
4
+
5
+ module Kuzya
6
+ module Commands
7
+ class Open < Kuzya::Commands::Base
8
+ URL_REGEXP = /\A#{URI::regexp(['http', 'https'])}\z/.freeze
9
+
10
+ class << self
11
+ def info
12
+ <<-INFO
13
+ #{'open'.bold} - to open any link run '#{'kuzya www.example.com'.italic}'
14
+ Also you can use your saved shortcuts instead of writing entire URL
15
+ e.g. add 'gh' shortcut as 'https://github.com' and 'rb' as 'ruby'
16
+ with 'kuzya add' command and run '#{'kuzya gh rb rake'.italic}'. Kuzya will make
17
+ the URL and open 'https://github.com/ruby/rake'.
18
+ INFO
19
+ end
20
+
21
+ def prepare(args, config)
22
+ [Kuzya::UrlBuilder.call(args, config)]
23
+ end
24
+ end
25
+
26
+ def initialize(url)
27
+ @url = url
28
+ end
29
+
30
+ def call
31
+ puts "Open #{url}"
32
+ raise unless url =~ URL_REGEXP
33
+
34
+ Launchy.open(url)
35
+ rescue
36
+ puts "Can't open '#{url}', please check it, maybe you don't have shortcuts or URL is invalid".red
37
+ end
38
+
39
+ private
40
+
41
+ attr_reader :url
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kuzya
4
+ module Commands
5
+ class Remove < Kuzya::Commands::Base
6
+ class << self
7
+ def info
8
+ <<-INFO
9
+ #{'remove'.bold} - if you want to remove shortcut you can run:
10
+ #{'kuzya remove shortcut_name'.italic}
11
+ Also you can remove nested shortcuts, to do it run:
12
+ #{'kuzya remove parent_shortcut child_shortcut'.italic}
13
+ Note: Removing shortcut that have nested shortcuts also removes them
14
+ INFO
15
+ end
16
+
17
+ def prepare(args, config)
18
+ [args_without_command(args), config]
19
+ end
20
+ end
21
+
22
+ def initialize(args, config)
23
+ @args = args
24
+ @config = config
25
+ end
26
+
27
+ def call
28
+ config.remove(args)
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :args, :config
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+ require 'yaml'
5
+
6
+ module Kuzya
7
+ class Config
8
+ PATH_TO_DIR = "#{Dir.home}/.kuzya".freeze
9
+ PATH_TO_CONFIG = "#{Dir.home}/.kuzya/kuzya.yml".freeze
10
+
11
+ attr_reader :shortcuts
12
+
13
+ def initialize
14
+ FileUtils.mkdir_p(PATH_TO_DIR) # Creates file if not exists
15
+ FileUtils.touch(PATH_TO_CONFIG) # Creating file if not exists
16
+ @shortcuts = YAML.load(File.read(PATH_TO_CONFIG)) || {}
17
+ end
18
+
19
+ def add(keys)
20
+ value = keys.pop
21
+
22
+ scan_shortcuts(keys)
23
+
24
+ new_shortcut = build_shortcut(keys, value)
25
+ @shortcuts.deep_merge!(new_shortcut)
26
+ save
27
+
28
+ puts 'Done.'
29
+ end
30
+
31
+ def remove(keys)
32
+ scan_shortcuts(keys)
33
+
34
+ empty_shortcut = build_empty_shortcut(keys)
35
+ @shortcuts.deep_merge!(empty_shortcut)
36
+ @shortcuts = Hash.deep_compact(@shortcuts)
37
+
38
+ save
39
+ end
40
+
41
+ private
42
+
43
+ def save
44
+ File.open(PATH_TO_CONFIG, 'w') { |file| file.write(@shortcuts.to_yaml) }
45
+ end
46
+
47
+ def build_empty_shortcut(keys)
48
+ keys.reverse.inject(nil) { |assigned_value, key| { key => assigned_value } }
49
+ end
50
+
51
+ def build_shortcut(keys, value)
52
+ keys.reverse.inject({ '_url' => value }) do |assigned_value, key|
53
+ { key => assigned_value }
54
+ end
55
+ end
56
+
57
+ def scan_shortcuts(keys)
58
+ shortcuts.dup do |hash|
59
+ keys.each do |key|
60
+ next hash = hash[key] if hash.key?(key)
61
+
62
+ puts "Key '#{key}' not found. Check spelling or add it by 'kuzya #{keys.join(' ')} www.example.com'."
63
+ exit(1)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Hash
4
+ def self.deep_compact(hash)
5
+ result = hash.map do |key, value|
6
+ value = deep_compact(value) if value.is_a?(Hash)
7
+
8
+ value = nil if [{}, []].include?(value)
9
+ [key, value]
10
+ end
11
+
12
+ result.to_h.compact
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Hash
4
+ # Copy-pasted from activesupport/lib/active_support/core_ext/hash/deep_merge.rb
5
+ def deep_merge!(other_hash, &block)
6
+ merge!(other_hash) do |key, this_val, other_val|
7
+ if this_val.is_a?(Hash) && other_val.is_a?(Hash)
8
+ this_val.deep_merge(other_val, &block)
9
+ elsif block_given?
10
+ block.call(key, this_val, other_val)
11
+ else
12
+ other_val
13
+ end
14
+ end
15
+ end
16
+
17
+ def deep_merge(other_hash, &block)
18
+ dup.deep_merge!(other_hash, &block)
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class String
4
+ def bold
5
+ "\e[1m#{self}\e[22m"
6
+ end
7
+
8
+ def italic
9
+ "\e[3m#{self}\e[23m"
10
+ end
11
+
12
+ def red
13
+ "\e[31m#{self}\e[0m"
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kuzya
4
+ class Service
5
+ def self.call(*args)
6
+ new(*args).call
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kuzya
4
+ class UrlBuilder < Kuzya::Service
5
+ def initialize(args, config)
6
+ @args = args
7
+ @config = config
8
+ end
9
+
10
+ def call
11
+ url = ''
12
+
13
+ config.shortcuts.dup.tap do |conf|
14
+ args.each do |key|
15
+ conf = conf.fetch(key, { '_url' => key })
16
+ url += "#{conf.fetch('_url')}/"
17
+ end
18
+ end
19
+
20
+ url
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :config, :args
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kuzya
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Krushinsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: launchy
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Wrapper for 'launchy' gem to using shortcuts.
28
+ email: d.krushinsky.00@gmail.com
29
+ executables:
30
+ - kuzya
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - README.md
36
+ - bin/kuzya
37
+ - kuzya.gemspec
38
+ - lib/kuzya.rb
39
+ - lib/kuzya/cli.rb
40
+ - lib/kuzya/commands.rb
41
+ - lib/kuzya/commands/add.rb
42
+ - lib/kuzya/commands/base.rb
43
+ - lib/kuzya/commands/help.rb
44
+ - lib/kuzya/commands/list.rb
45
+ - lib/kuzya/commands/open.rb
46
+ - lib/kuzya/commands/remove.rb
47
+ - lib/kuzya/config.rb
48
+ - lib/kuzya/core_ext/hash/deep_compact.rb
49
+ - lib/kuzya/core_ext/hash/deep_merge.rb
50
+ - lib/kuzya/core_ext/string/formatting.rb
51
+ - lib/kuzya/service.rb
52
+ - lib/kuzya/url_builder.rb
53
+ homepage:
54
+ licenses:
55
+ - MIT
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.0.8
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Utility to open urls in browser from terminal.
76
+ test_files: []