From c443949f6643a36cc82415758a57d4a8a9517e07 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Wed, 19 Jul 2017 12:55:23 -0400 Subject: [PATCH] added fetch script and tweaked match allow characters for filenames --- lib/utf8.c | 2 +- tide-fetch.rb | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100755 tide-fetch.rb diff --git a/lib/utf8.c b/lib/utf8.c index 379d396..a7fd88e 100644 --- a/lib/utf8.c +++ b/lib/utf8.c @@ -114,7 +114,7 @@ bool rissigil(Rune r) { } bool risfile(Rune r) { - return (risword(r) || r == '/' || r == '.' || r == ':'); + return (risword(r) || r == '/' || r == '.' || r == ':' || r == '-' || r == '.'); } bool riscmd(Rune r) { diff --git a/tide-fetch.rb b/tide-fetch.rb new file mode 100755 index 0000000..a92ea66 --- /dev/null +++ b/tide-fetch.rb @@ -0,0 +1,103 @@ +#!/usr/bin/env ruby + +if not ARGV[0] then + $stderr.puts "Usage: tide-fetch.rb [ITEM]" + exit 1 +end + +$item = ARGV[0] +$attr = {} +$match = [] +UserRules = "#{ENV["HOME"]}/.config/tide/fetch-rules.rb" +Rules = [] +Apps = {} + +# Define the Rule Language +#------------------------------------------------------------------------------- +class RuleError < StandardError; end + +def rule(&block) + Rules << block +end + +def match(regex) + $match = $item.match(regex) + if not $match then + raise RuleError.new() + end +end + +def open_file + system("xdg-open #{$item}") +end + +def open_with(app) + app = Apps[app] || ENV[app.to_s.upcase] + raise RuleError.new() if not app + system("#{app} #{$item}") +end + +def find_files(file) + files = Dir.glob("./**/#{file}").sort_by(&:length) + raise RuleError.new() if files.length == 0 + files +end + +def find_file(file) + find_files(file).first +end + +def mimetype(regex) + mtype = `file -i #{$item} | cut -d' ' -f2` + if not mtype.match(regex) then + raise RuleError.new() + end +end + +# Builtin Rules +#------------------------------------------------------------------------------- + +# Run user rules first +if File.exists?(UserRules) + load UserRules +end + +# open urls in the browser +rule do + match /(https?|ftp):\/\/[a-zA-Z0-9_@\-]+([.:][a-zA-Z0-9_@\-]+)*\/?[a-zA-Z0-9_?,%#~&\/\-+=]+([:.][a-zA-Z0-9_?,%#~&\/\-+=]+)*/ + open_with :browser +end + +# open html files with browser +rule do + match /^.+\.html?/ + $item = find_file($item) + open_with :browser +end + +# open files with address in the text editor +rule do + match /^([^:]+):([0-9]+)/ + f = find_file($match[1]) + $item = "#{f}:#{$match[2]}" + open_with :editor +end + +# if the file is a text file, edit it +rule do + $item = find_file($item) + mimetype /^text\// + open_with :editor +end + +# Main Execution +#------------------------------------------------------------------------------- + +Rules.each do |rule| + begin + rule.call($item) + exit 0 # Found a match, positive response + rescue RuleError + end +end +exit 1 # No match return error -- 2.49.0