Created
October 23, 2018 22:16
-
-
Save emad-elsaid/c95e9c59cacd99aad6904c266e8684b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# How to use that script | |
#======================= | |
# I write several small scripts to aid me in my workflow, I usually keep them in | |
# one directory added to my PATH but that means to execute one of them I need to | |
# recall the name of every script, but in reality I forget that That I even | |
# wrote it and sometimes I rewrite the same script many times because of this. | |
# So what I learned here is that these scripts needs a long meaningful name, and | |
# some interface to search through them an interface that's flexible enough like | |
# Emacs helm package, a fuzzy finder that search through a long list of scripts | |
# and when I find the script I need I'll execute it. I found that FZF is a good | |
# interface and I wrote that script around it, this script below will open FZF | |
# passing all executable scripts found in "Sources Directories" (coming below | |
# what are they), when you choose a script it will execute it. | |
# another feature I added is a documentation panel, that renders markdown comments | |
# in the beginning of a file to terminal colored string. | |
# so when you cycle through the list you see the documentation of it. | |
# | |
# Source Directories: directories that has executable scripts, that script keeps a list | |
# of them in ~/.cmd/sources you can add your directories with `cmd add-sources /path/dir1...` | |
# or simply edit the file with your favorite text editor | |
require 'fileutils' | |
command = ARGV.first | |
separator = "\t" | |
conf_dir = File.expand_path(File.join('~/.cmd')) | |
history = File.join(conf_dir, 'history') | |
sources = File.join(conf_dir, 'sources') | |
Dir.mkdir(conf_dir) unless File.exist?(conf_dir) | |
FileUtils.touch(sources) unless File.exist?(sources) | |
case command | |
when nil | |
line = `#{__FILE__} list | fzf --preview="#{__FILE__} preview {2}" --reverse --preview-window=right:50%:wrap --nth=1 --with-nth=1 --delimiter="#{separator}" --history=#{history}` | |
script = line.split(separator)[1] | |
script.gsub!(' ', '\ ') | |
system(script) | |
when 'list' | |
sources_dirs = File.read(sources).lines.map(&:strip).reject(&:empty?).join(',') | |
files = Dir.glob("{#{sources_dirs}}/**/*").select { |f| File.file?(f) && File.executable?(f) } | |
files.map! { |f| "#{File.basename(f)}#{separator}#{f}" } | |
puts files | |
when 'preview' | |
begin | |
require 'tty-markdown' | |
path = ARGV[1] | |
lines = File.read(path).lines.take_while { |l| l.strip.start_with?('#') } | |
lines = lines[1..-1] if !lines.empty? && lines.first.start_with?('#!') | |
lines = lines.map { |l| l.lstrip[1..-1] } | |
text = lines.join | |
markdown = TTY::Markdown.parse(text) | |
puts markdown | |
rescue StandardError => e | |
puts "An error occured while load preview...\n#{e}" | |
end | |
when 'list-sources' | |
puts File.read(sources) | |
when 'add-sources' | |
new_sources = ARGV[1..-1] | |
current_sources = File.read(sources).lines.map(&:strip).reject(&:empty?) | |
all_sources = current_sources + new_sources | |
File.write(sources, all_sources.join("\n")) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment