Created
November 3, 2021 01:24
-
-
Save cirrusUK/8d7df0e614f719a31a6cc4081f823ef8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# frozen_string_literal: true | |
require 'cgi' | |
class SearchEngine | |
def initialize(target, fallback: nil, prefixes: []) | |
@target = target | |
@fallback = fallback | |
@prefix = prefixes.map { |s| "#{s} " }.join | |
end | |
private | |
def combine(str) | |
if str.strip.empty? && [email protected]? | |
@fallback % escape("#{@prefix}#{str}") | |
else | |
@target % escape("#{@prefix}#{str}") | |
end | |
end | |
end | |
class Man < SearchEngine | |
def initialize | |
super('man %s') | |
end | |
def run(str) | |
`kitty --class man fish -c '#{combine(str)}'` | |
end | |
def escape(str) | |
str | |
end | |
end | |
class Browser < SearchEngine | |
def initialize(target, fallback: nil, prefixes: [], profile: nil) | |
super(target, fallback: fallback, prefixes: prefixes) | |
@profile = profile | |
end | |
def run(str) | |
`firefox --new-tab #{@profile} '#{combine(str)}'` | |
end | |
def escape(str) | |
CGI.escape(str) | |
end | |
end | |
class YouTube < Browser | |
def initialize | |
super(url('results?q=%s'), fallback: url, profile: '') | |
end | |
def run(str) | |
case str | |
when 'later' then `firefox --new-tab " '#{url('playlist?list=WL')}'` | |
else super(str) | |
end | |
end | |
private | |
def url(str=nil) | |
"https://www.youtube.com/#{str}" | |
end | |
end | |
class BrowserRaw < Browser | |
def escape(str) | |
str | |
end | |
end | |
defaultengine = Browser.new('https://duckduckgo.com/?gb=web&q=%s') | |
# rubocop:disable Layout/HashAlignment, Layout/FirstArgumentIndentation, Layout/LineLength | |
searchengines = { | |
'yt' => YouTube.new, | |
'map' => Browser.new('https://www.google.com/maps/search/%s/'), | |
'ddg' => Browser.new('https://duckduckgo.com/?gb=web&q=%s',profile: ''), | |
'am' => Browser.new('https://www.amazon.co.uk/s?k=%s', profile: ''), | |
'l' => Browser.new('http://localhost:%s'), | |
'th' => Browser.new('https://www.thesaurus.com/browse/%s?s=t'), | |
'dict' => Browser.new('https://www.larousse.fr/dictionnaires/francais/%s'), | |
'trad' => Browser.new('https://translate.google.com/#view=home&op=translate&sl=fr&tl=en&text=%s'), | |
'g' => BrowserRaw.new('https://google.co.uk/search?q=%s'), | |
'gh' => Browser.new('https://github.com/search?q=%s'), | |
'cm' => Browser.new('https://codemadness.org/idiotbox/?q=%s&o=date', profile: ''), | |
'aw' => Browser.new('https://wiki.archlinux.org/index.php/%s'), | |
'dt' => Browser.new('https://www.dndtools.net/spells/?name=%s&_filter=Filter', profile: ''), | |
'so' => Browser.new('http://stackoverflow.com/search?q=%s'), | |
'r' => Browser.new('https://reddit.com/r/%s', profile: ''), | |
'gem' => Browser.new('https://rubygems.org/search?query=%s'), | |
't' => Browser.new('https://www.xn--thepratebay-fcb.com/s/?q=%s', profile: ''), | |
'imdb' => Browser.new('https://www.imdb.com/find?q=%s'), | |
'wp' => Browser.new('https://wallhaven.cc/search?q=%s&atleast=3440x1440', profile: ''), | |
'w' => Browser.new('https://en.wikipedia.org/wiki/Special:Search?search=%s'), | |
'ym' => Browser.new('https://music.youtube.com/search?q=%s', profile: ''), | |
'mag' => Browser.new('https://www.magnetdl.me/search/%s/se/desc/', profile: ''), | |
'bbs' => Browser.new('https://bbs.archlinux.org/search.php/%s'), | |
'tpb' => Browser.new('https://pirate-proxy.dev/search.php?q=%s&all=on&search=Pirate+Search&page=0&orderby=', profile: ''), | |
'man' => Man.new | |
} | |
# rubocop:enable Layout/HashAlignment, Layout/FirstArgumentIndentation, Layout/LineLength | |
query = `rofi -dmenu -p "Search 🔍 ﰍ 🔎" -theme-str "#content { children: [inputbar]; }"` | |
words = query.split | |
if words.any? | |
engine = searchengines[words[0]] | |
if engine | |
words.shift | |
elsif words[0].match('http.?://.*') | |
engine = Browser.new(words.join) | |
end | |
(engine || defaultengine).run(words[0..-1].join(' ')) | |
elsif query == "\n" | |
`browser-with-context` | |
end | |
# vim: textwidth=100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment