snibbets 2.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b4b5be01b0a7d1768bef21a63ffd7c1f1f176a77d6d3fa1be5ff84db7b0b27eb
4
+ data.tar.gz: 7c65f52ecb3dacbeea62c8225f9dc70db7d90d358a4c284050b7149a9b19fe75
5
+ SHA512:
6
+ metadata.gz: e4adffb0b196b8f891ac9363fdcb91a67dccfc579530568e336dd1ced2d7d8f6c7f4496fa1c4cbe992e24655a37a7808bd49c48e44d75623a2c1fa0fb1615bb9
7
+ data.tar.gz: 796ec5249c2c7332132ac2138d5281c05aab7259b75726319f247499543a15c4b839d6929e764850a8bbc907747a42a179d3b61625e4b4fff7ca90f698c24509
data/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ ### 2.0.7
2
+
3
+ 2023-04-15 12:02
4
+
5
+ ### 2.0.6
6
+
7
+ 2023-04-15 11:55
8
+
9
+ #### IMPROVED
10
+
11
+ - Refactor script as modules and classes
12
+
13
+ ## 2.0.2
14
+
15
+ Initial release as a gem
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in snibbets.gemspec.
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,96 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ snibbets (2.0.3)
5
+ tty-which (~> 0.5, >= 0.5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ansi (1.5.0)
11
+ ast (2.4.2)
12
+ diff-lcs (1.5.0)
13
+ docile (1.4.0)
14
+ gem-release (2.2.2)
15
+ json (2.6.3)
16
+ language_server-protocol (3.17.0.3)
17
+ multi_json (1.15.0)
18
+ parallel (1.22.1)
19
+ parse_gemspec (1.0.0)
20
+ parse_gemspec-cli (1.0.0)
21
+ multi_json
22
+ parse_gemspec
23
+ thor
24
+ parser (3.2.2.0)
25
+ ast (~> 2.4.1)
26
+ rainbow (3.1.1)
27
+ rake (13.0.6)
28
+ rdoc (4.3.0)
29
+ regexp_parser (2.7.0)
30
+ rexml (3.2.5)
31
+ rspec (3.12.0)
32
+ rspec-core (~> 3.12.0)
33
+ rspec-expectations (~> 3.12.0)
34
+ rspec-mocks (~> 3.12.0)
35
+ rspec-core (3.12.1)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-expectations (3.12.2)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.12.0)
40
+ rspec-mocks (3.12.5)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.12.0)
43
+ rspec-support (3.12.0)
44
+ rubocop (1.48.1)
45
+ json (~> 2.3)
46
+ parallel (~> 1.10)
47
+ parser (>= 3.2.0.0)
48
+ rainbow (>= 2.2.2, < 4.0)
49
+ regexp_parser (>= 1.8, < 3.0)
50
+ rexml (>= 3.2.5, < 4.0)
51
+ rubocop-ast (>= 1.26.0, < 2.0)
52
+ ruby-progressbar (~> 1.7)
53
+ unicode-display_width (>= 2.4.0, < 3.0)
54
+ rubocop-ast (1.28.0)
55
+ parser (>= 3.2.1.0)
56
+ rubocop-performance (1.16.0)
57
+ rubocop (>= 1.7.0, < 2.0)
58
+ rubocop-ast (>= 0.4.0)
59
+ ruby-progressbar (1.13.0)
60
+ simplecov (0.22.0)
61
+ docile (~> 1.1)
62
+ simplecov-html (~> 0.11)
63
+ simplecov_json_formatter (~> 0.1)
64
+ simplecov-console (0.9.1)
65
+ ansi
66
+ simplecov
67
+ terminal-table
68
+ simplecov-html (0.12.3)
69
+ simplecov_json_formatter (0.1.4)
70
+ standard (1.26.0)
71
+ language_server-protocol (~> 3.17.0.2)
72
+ rubocop (~> 1.48.1)
73
+ rubocop-performance (~> 1.16.0)
74
+ terminal-table (3.0.2)
75
+ unicode-display_width (>= 1.1.1, < 3)
76
+ thor (1.2.1)
77
+ tty-which (0.5.0)
78
+ unicode-display_width (2.4.2)
79
+
80
+ PLATFORMS
81
+ arm64-darwin-20
82
+
83
+ DEPENDENCIES
84
+ bundler (~> 2.0)
85
+ gem-release (~> 2.2)
86
+ parse_gemspec-cli (~> 1.0)
87
+ rake (~> 13.0)
88
+ rdoc (~> 4.3)
89
+ rspec (~> 3.0)
90
+ simplecov (~> 0.21)
91
+ simplecov-console (~> 0.9)
92
+ snibbets!
93
+ standard (~> 1.3)
94
+
95
+ BUNDLED WITH
96
+ 2.2.29
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Brett Terpstra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,173 @@
1
+ # Snibbets
2
+
3
+ [![RubyGems.org](https://img.shields.io/gem/v/snibbets)](https://rubygems.org/gems/snibbets)
4
+ [![GitHub Actions](https://github.com/ttscoff/snibbets/actions/workflows/check.yml/badge.svg)](https://github.com/ttscoff/snibbets/actions/workflows/check.yml)
5
+
6
+ <!--README-->
7
+ A tool for accessing code snippets contained in a folder of plain text Markdown files.
8
+
9
+ Snibbets allows me to keep code snippets in raw files, not relying on a dedicated code snippet app. I can collect and edit my snippets using a text editor, nvALT (nvUltra), or simply by saving snippets from my clipboard to a text file using *NIX redirection on the command line. I can add descriptive names and extended descriptions/notes to code snippets using standard Markdown.
10
+
11
+ What Snibbets does is simply allow a quick search for a specific snippet that I can either output to the terminal, pipe to my clipboard, or access via LaunchBar (via the included LaunchBar Action). It's basically a wrapper for `find` and `grep` with the ability to separate code blocks from other text in my Markdown files.
12
+
13
+ <!--GITHUB-->
14
+ ![Snibbets in action](https://cdn3.brettterpstra.com/uploads/2023/04/snibbets2.gif)
15
+ <!--END GITHUB-->
16
+ <!--JEKYLL{% gif /uploads/2023/04/snibbets2.gif %}-->
17
+
18
+ ## Collecting Snippets
19
+
20
+ Snibbets is designed to work with a folder containing Markdown files. Each Markdown file can have somewhat flexible formatting, as long as there's at least one code block (either indented by 4 spaces/1 tab or fenced with backticks).
21
+
22
+ I recommend using filenames with multiple extensions (ending with your markdown extension), primarily to define the syntax for a snippet. For example, a css snippet would be `*.css.md`, a ruby snippet would be `*.rb.md`. This can aid in searching and makes it easy to script things like adding language tags automatically.
23
+
24
+ The name of the file should be the description of the snippet, at least in the case where there's only one snippet in the document. Call it 'javascript url parser.js.md' or similar. If I got the snippet from StackOverflow, I give it a name based on the question I searched to find it. Be descriptive.
25
+
26
+ You can combine multiple snippets in a file, though. For example, I have a file called 'Ruby hash snippets.rb.md'. That file contains an array of useful snippets, and each one has a descriptive title in an h3 header above it. Those (ATX) headers are used to split the file, and when you search from the command line, you'll get a menu of all of the snippets in the selected file. (And if you have [fzf](https://github.com/junegunn/fzf) or [gum](https://github.com/charmbracelet/gum) installed, you can quickly filter through with fuzzy searching and find exactly what you need.)
27
+
28
+ If a file contains multiple snippets, they should be separated by ATX-style headers (one or more `#`) describing the snippets. Additional descriptions can be included outside of the code block. For example:
29
+
30
+ A file titled `unix find.bash.md`:
31
+
32
+ title: Unix find commands
33
+ tags: bash, shell, unix
34
+
35
+ ### Find by name and execute command
36
+
37
+ find . -name "rc.conf" -exec chmod o r '{}' \;
38
+
39
+ ### Find by name and grep contents
40
+
41
+ find . -name "*.php" -exec grep -H googleapis '{}' \;
42
+
43
+ ### Find by age range to CSV
44
+
45
+ Finds files older than 18months and newer than 24 months, cats the output to a CSV in the format `/some/path/somewhere, size in bytes, Access Time, Modified Time`
46
+
47
+ find /dir/dir -type f -mtime +540 -mtime -720 -printf \"%p\",\"%s\",\"%AD\",|"%TD\"\\n > /dir/dir/output.csv
48
+
49
+ You can include MultiMarkdown metadata in your snippets, either in a YAML block or just at the top of the file with raw key/value pairs. I mostly use this for adding tags, which are then synced to macOS tags when I save. It makes it easy to search for snippets in [nvUltra](https://nvultra.com/), and also allows you to do searches like `snibbets tag:javascript url parser` in Snibbets.
50
+
51
+ ## CLI
52
+
53
+ ### Dependencies
54
+
55
+ Snibbets requires Ruby 3.0+. On recent versions of macOS, this is not included by default. You can install it via the Command Line Tools from Apple. On macOS and most other systems, you can use something like [Homebrew], [rbenv], [rvm], or [asdf] to install Ruby 3.
56
+
57
+ If available, menus are generated by [fzf] or [gum]. If neither are available, a basic Readline menu system will be displayed, so neither are required, just nice to have as they provide fuzzy filtering, scrolling, and type-ahead completion.
58
+
59
+ [homebrew]: https://brew.sh/ "Homebrew—The Missing Package Manager for macOS (or Linux)"
60
+ [rbenv]: https://github.com/rbenv/rbenv "rbenv/rbenv:Manage your app's Ruby environment"
61
+ [rvm]: https://rvm.io/ "Ruby Version Manager"
62
+ [asdf]: https://asdf-vm.com/ "ASDF environment manager"
63
+ [fzf]: https://github.com/junegunn/fzf "junegunn/fzf:A command-line fuzzy finder"
64
+ [gum]: https://github.com/charmbracelet/gum "charmbracelet/gum:A tool for glamorous shell scripts 🎀"
65
+
66
+ ### Installation
67
+
68
+ To install Snibbets:
69
+
70
+ gem install snibbets
71
+
72
+ If you're using the system Ruby, you may need to use `sudo gem install snibbets`.
73
+
74
+ ### Configuration
75
+
76
+ When you run it the first time, Snibbets will write a configuration file to `~/.config/snibbets/snibbets.yml`. You can edit that file to set things like your snippets directory, your preferred file extension, and a few other options. Options specified in the config file can always be overriden on the command line with flags.
77
+
78
+ Once an editor is defined, you can use `snibbets --configure` to open it automatically. Running that commmand before configuring an editor will use whatever app/utility your system defaults to for YAML files.
79
+
80
+ Default config:
81
+
82
+ ```yaml
83
+ ---
84
+ all: false
85
+ copy: false
86
+ editor:
87
+ extension: md
88
+ highlight: false
89
+ interactive: true
90
+ launchbar: false
91
+ name_only: false
92
+ output: raw
93
+ source: "~/Dropbox/Snippets"
94
+ ```
95
+
96
+ #### Snippet Location
97
+
98
+ Set the `source` key to the folder where you keep your Markdown snippets. Optionally adjust the `extension` setting if you use an extension other than `md` (e.g. `markdown` or `txt`).
99
+
100
+ #### Other Options
101
+
102
+ The `all` setting determines how Snibbets handles files containing multiple snippets. If `all` is true, then it will always display every snippet in the selected file. If false, it will offer a menu and let you choose which snippet to display. You can use `--all` on the command line to just enable this once.
103
+
104
+ The `copy` setting determines whether the output is copied to the clipboard in addition to being displayed on STDOUT. This is the equivalent of running `snibbets QUERY | pbcopy` (macOS) or `snibbets QUERY | xclip` (Linux). This can be enabled for just one run with `--copy` on the command line. Setting it to true in the config will copy to the clipboard every time a snippet is displayed. On Mac this will work automatically, on Windows/Linux you may need to [install `xclip` or `xsel`][xclip].
105
+
106
+ [xclip]: https://ostechnix.com/access-clipboard-contents-using-xclip-and-xsel-in-linux/
107
+
108
+ The `editor` setting is used to open the config file, and to open snippets for editing when using the `--edit` flag. This setting can be any command line utility (`code`, `subl`, `vim`, `nano`, etc.), or on macOS it can be an application name (`BBEdit`, `VS Code`, etc.) or a bundle identifier (`com.sublimetext.4`, `com.microsoft.VSCode`, etc.). If no editor is set, then the file will be opened by whatever the system default is (using `open` on macOS, `start` on Windows, or `xdg-open`on Linux).
109
+
110
+ The `highlight` key turns on syntax highlighting. This requires that either `pygmentize` or `skyligting` is available on your system (both available via package managers like Homebrew). This feature is still in development and results may be mixed.
111
+
112
+ The `interactive` setting determines whether menus will be displayed. This should generally be true, but if you want silent operation that just displays the best match automatically, set it to false.
113
+
114
+ The `name_only` key will permanently set Snibbets to only search for snippets by their filename rather than examining their contents. You can enable this at runtime using `--name-only` in the command.
115
+
116
+ ### Usage
117
+
118
+ Usage: snibbets [options] query
119
+ -a, --all If a file contains multiple snippets, output all of them (no menu)
120
+ -c, --[no-]copy Copy the output to the clibpoard (also displays on STDOUT)
121
+ -e, --edit Open the selected snippet in your configured editor
122
+ -n, --[no-]name-only Only search file names, not content
123
+ -o, --output FORMAT Output format (json|launchbar|*raw)
124
+ -p, --paste Interactively create a new snippet from clipboard contents (Mac only)
125
+ -q, --quiet Skip menus and display first match
126
+ -s, --source FOLDER Snippets folder to search
127
+ --configure Open the configuration file in your default editor
128
+ --highlight Use pygments or skylighting to syntax highlight (if installed)
129
+ --save Save the current command line options to the YAML configuration
130
+ -h, --help Display this screen
131
+ -v, --version Display version information
132
+
133
+ If your Snippets folder is set in the config, simply running `snibbets [search query]` will perform the search and output the code blocks, presenting a menu if more than one match is found or the target file contains more than one snippet. Selected contents are output raw to STDOUT.
134
+
135
+ > If you have fzf or gum installed, snibbets will use those for menus, providing fuzzy filtering of options.
136
+
137
+ #### JSON output
138
+
139
+ An undocumented output option is `-o json`, which will output all of the matches and their code blocks as a JSON string that can be incorporated into other scripts. It's similar to the `-o launchbar` option, but doesn't contain the extra keys required for the LaunchBar action.
140
+
141
+ #### Open snippets in your editor
142
+
143
+ Use the `--edit` flag on any search to open the found snippet file in your editor. Configure your default editor in the config file. `snibbets configure` will open that, but if you don't have an editor set, it might have strange results. To edit manually, open `~/.config/snibbets/snibbets.yml` in your text editor of choice.
144
+
145
+ #### Creating new snippets
146
+
147
+ I do most of my snippet editing in [nvUltra], but sometimes I have a function in my clipboard that just needs quick saving and there are so few moving parts to creating a snippet that it just feels like they could be automated/simplified. That's why I added the `--paste` flag. If you have a code snippet in your clipboard, you can just run `snibbets --paste` (or just `-p`) and you'll get a prompt asking you to describe the snippet (used for filename) and one asking what language(s) are represented.
148
+
149
+ You can input the languages as names, e.g. `rust`, `typescript`, or `scala`, or you can just add file extensions that represent the language. If I say `ts` to that prompt, it will generate an extension of `.ts.md` and then add a metadata tag of `typescript` to the file. The code from the clipboard goes into a fenced code block in the document. You can always go add notes to it later, but it's a great way to save snippets as you come across them (or solutions you figure out after a week of banging your head).
150
+
151
+ This command requires that a clipboard utility be available. On macOS, you have `pbpaste` by default and don't need to do anything. On Windows and Linux, you'll need to [install either `xclip` or `xsel`][xclip].
152
+
153
+ [nvUltra]: https://nvultra.com "nvUltra for Mac"
154
+
155
+
156
+ #### Saving Settings When Running
157
+
158
+ Any time you specify things like a source folder with the `--source` flag, or turn on highlighting or name-only search, you can add the flag `--save` to write those to your config and make them the default options.
159
+
160
+ ## LaunchBar Action
161
+
162
+ _I'm currently reworking the LaunchBar action, and it doesn't function very well at this time. I'll update when I have a chance._
163
+
164
+ ### Installation
165
+
166
+ The LaunchBar action can be installed simply by double clicking the `.lbaction` file in Finder. The CLI is not required for the LaunchBar action to function.
167
+
168
+ Once installed, run the action (type `snib` and hit return on the result) to select your Snippets folder.
169
+
170
+ ### Usage
171
+
172
+ Type `snib` to bring the Action up, then hit Space to enter your query text. Matching files will be presented. If the selected file contains more than one snippet, a list of snippets (based on ATX headers in the file) will be presented as a child menu. Selecting a snippet and hitting return will copy the associated code block to the clipboard.
173
+ <!--END README-->
data/README.rdoc ADDED
@@ -0,0 +1,6 @@
1
+ = Snibbets
2
+
3
+ A command line tool for managing a code snippet collection in plain text (Markdown).
4
+
5
+
6
+
data/Rakefile ADDED
@@ -0,0 +1,87 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rdoc/task'
4
+ require 'standard/rake'
5
+ require 'yard'
6
+
7
+ Rake::RDocTask.new do |rd|
8
+ rd.main = "README.rdoc"
9
+ rd.rdoc_files.include("README.rdoc", "lib/**/*.rb", "bin/**/*")
10
+ rd.title = 'Snibbets'
11
+ end
12
+
13
+ YARD::Rake::YardocTask.new do |t|
14
+ t.files = ['lib/na/*.rb']
15
+ t.options = ['--markup-provider=redcarpet', '--markup=markdown', '--no-private', '-p', 'yard_templates']
16
+ # t.stats_options = ['--list-undoc']
17
+ end
18
+
19
+ RSpec::Core::RakeTask.new(:spec) do |t|
20
+ t.rspec_opts = "--pattern {spec,lib}/**/*_spec.rb"
21
+ end
22
+
23
+ task default: %i[test]
24
+
25
+ desc 'Alias for build (so it shows up in rake -T)'
26
+ task :package do
27
+ Rake::Task['build'].invoke
28
+ end
29
+
30
+ task test: "spec"
31
+ task lint: "standard"
32
+ task format: "standard:fix"
33
+
34
+ desc "Open an interactive ruby console"
35
+ task :console do
36
+ require "irb"
37
+ require "bundler/setup"
38
+ require "snibbets"
39
+ ARGV.clear
40
+ IRB.start
41
+ end
42
+
43
+ desc 'Development version check'
44
+ task :ver do
45
+ gver = `git ver`
46
+ cver = IO.read(File.join(File.dirname(__FILE__), 'CHANGELOG.md')).match(/^#+ (\d+\.\d+\.\d+(\w+)?)/)[1]
47
+ res = `grep VERSION lib/snibbets/version.rb`
48
+ version = res.match(/VERSION *= *['"](\d+\.\d+\.\d+(\w+)?)/)[1]
49
+ puts "git tag: #{gver}"
50
+ puts "version.rb: #{version}"
51
+ puts "changelog: #{cver}"
52
+ end
53
+
54
+ desc 'Changelog version check'
55
+ task :cver do
56
+ puts IO.read(File.join(File.dirname(__FILE__), 'CHANGELOG.md')).match(/^#+ (\d+\.\d+\.\d+(\w+)?)/)[1]
57
+ end
58
+
59
+ desc 'Bump incremental version number'
60
+ task :bump, :type do |_, args|
61
+ args.with_defaults(type: 'inc')
62
+ version_file = 'lib/snibbets/version.rb'
63
+ content = IO.read(version_file)
64
+ content.sub!(/VERSION = '(?<major>\d+)\.(?<minor>\d+)\.(?<inc>\d+)(?<pre>\S+)?'/) do
65
+ m = Regexp.last_match
66
+ major = m['major'].to_i
67
+ minor = m['minor'].to_i
68
+ inc = m['inc'].to_i
69
+ pre = m['pre']
70
+
71
+ case args[:type]
72
+ when /^maj/
73
+ major += 1
74
+ minor = 0
75
+ inc = 0
76
+ when /^min/
77
+ minor += 1
78
+ inc = 0
79
+ else
80
+ inc += 1
81
+ end
82
+
83
+ $stdout.puts "At version #{major}.#{minor}.#{inc}#{pre}"
84
+ "VERSION = '#{major}.#{minor}.#{inc}#{pre}'"
85
+ end
86
+ File.open(version_file, 'w+') { |f| f.puts content }
87
+ end