From e96bafcf789f642b4c0fefa353951d9075ec25cf Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sat, 29 Sep 2018 00:03:56 -0400 Subject: [PATCH] added tfetch ruby script implementation --- .gitignore | 1 - TODO.md | 2 +- tfetch | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 2 deletions(-) create mode 100755 tfetch diff --git a/.gitignore b/.gitignore index 395ebbc..790cbd6 100644 --- a/.gitignore +++ b/.gitignore @@ -53,6 +53,5 @@ pick tests/pick hl-cpp pty -tfetch tctl flaws.txt diff --git a/TODO.md b/TODO.md index b6800cd..8c1bc57 100644 --- a/TODO.md +++ b/TODO.md @@ -2,7 +2,7 @@ ## STAGING -* implement new version of tfetch +* implement new version of tfetch (plumb) * implement tide registrar * implement tctl command * implement mouse warping on search, jump to line, and focus change diff --git a/tfetch b/tfetch new file mode 100755 index 0000000..24d47c8 --- /dev/null +++ b/tfetch @@ -0,0 +1,128 @@ +#!/usr/bin/env ruby + +#------------------------------------------------------------------------------- +# Function Definitions +#------------------------------------------------------------------------------- +def expand(str) + str +end + +def rule(&block) + begin + block.call() + exit 0 + rescue + end +end + +def matches(var, regex) + if not Regexp.new(expand(regex)) =~ ENV[var] then + raise "match failed" + end +end + +def is(var, val) + if not ENV[var] == expand(val) then + raise "comparison failed" + end +end + +def is_set(var) + if not ENV[var] then + raise "variable not set" + end +end + +def is_dir(path) + if not File.directory?(expand(path)) then + raise "not a directory" + end +end + +def is_file(path) + if not File.file?(expand(path)) then + raise "not a file" + end +end + +def set(var, val) + ENV[var] = val +end + +def unset(var) + ENV[var] = nil +end + +def exec(cmd) + if not system(cmd) then + raise "command failed" + end +end + +def launch(cmd) + spawn(expand(cmd)) +end + +def import(path) + if File.file?(expand(path)) then + load(path) + end +end + +#------------------------------------------------------------------------------- +# Builtin Rule Definitions +#------------------------------------------------------------------------------- +ENV["data"] = ARGV[0] # store the data + +import("./plumbing") +import("$HOME/lib/plumbing") + +# Look up .c or .h files in Code/ +rule do + is_set "EDITOR" + matches "data", "\\.[ch]$" + is_dir "Code" + exec "[[ $$(find Code -type f -name '*$data') ]]" + launch "find Code -type f -name '*$data' | xargs -r $EDITOR" +end + +# Match URLS and open them with the browser +rule do + is_set "BROWSER" + matches "data", "^(https?|ftp)://.*" + launch "$BROWSER $0" +end + +# Open files with addresses in the editor +rule do + is_set "EDITOR" + matches "data", "^([^:]+):([0-9]+)" + is_file "$1" + launch "tctl $0" +end + +# If it's an existing text file, open it with editor +rule do + is_set "EDITOR" + is_file "$data" + exec "file --mime '$file' | grep -q 'text/'" + launch "$EDITOR $file" +end + +# Look it up in ctags database +rule do + is_set "EDITOR" + is_file "tags" + exec "grep -q '^$data\\s\\+' tags" + launch "picktag fetch tags $data | xargs -r tide" +end + +# If it's an existing directory, open it with system default +rule do + is_dir "$data" + launch "open $data" +end + +exit 1 #if we made it here then no rules matched + + -- 2.51.0