]> git.mdlowis.com Git - projs/tide.git/commitdiff
added tfetch ruby script implementation
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 29 Sep 2018 04:03:56 +0000 (00:03 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 29 Sep 2018 04:03:56 +0000 (00:03 -0400)
.gitignore
TODO.md
tfetch [new file with mode: 0755]

index 395ebbc0b9a27a5c7d9b2c1e48a24d26f8822a2b..790cbd66cbbdb01f12a7e95cbba24d344109da5f 100644 (file)
@@ -53,6 +53,5 @@ pick
 tests/pick
 hl-cpp
 pty
-tfetch
 tctl
 flaws.txt
diff --git a/TODO.md b/TODO.md
index b6800cdaaab91ad81a8ed3a8208736f09b9153ea..8c1bc576c25374fd58813f576ef42963deb594d3 100644 (file)
--- 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 (executable)
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
+
+