-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nikita Voloboev
committed
Feb 8, 2018
1 parent
2f578bc
commit e34f02a
Showing
5 changed files
with
167 additions
and
120 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/csv" | ||
"log" | ||
"os" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// doSearch makes a search for websites and returns results to Alfred. | ||
func doSearch() error { | ||
showUpdateStatus() | ||
|
||
log.Printf("query=%s", query) | ||
|
||
links := parseCSV() | ||
|
||
var re1 = regexp.MustCompile(`.: `) | ||
var re2 = regexp.MustCompile(`(all)`) | ||
|
||
for key, value := range links { | ||
if strings.Contains(key, "r: ") { | ||
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(redditIcon).Var("RECENT", re2.ReplaceAllString(value, `week`)).Subtitle("⌃ = Search past week") | ||
} else if strings.Contains(key, "d: ") { | ||
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(docIcon) | ||
} else if strings.Contains(key, "g: ") { | ||
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(githubIcon) | ||
} else if strings.Contains(key, "s: ") { | ||
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(stackIcon) | ||
} else if strings.Contains(key, "f: ") { | ||
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(forumsIcon) | ||
} else if strings.Contains(key, "t: ") { | ||
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key).Icon(translateIcon) | ||
} else { | ||
wf.NewItem(key).Valid(true).Var("URL", value).Var("ARG", re1.ReplaceAllString(key, ``)).UID(key) | ||
} | ||
} | ||
|
||
if query != "" { | ||
wf.Filter(query) | ||
} | ||
|
||
wf.WarnEmpty("No matching items", "Try a different query?") | ||
wf.SendFeedback() | ||
return nil | ||
} | ||
|
||
// parseCSV parses CSV for links and arguments. | ||
func parseCSV() map[string]string { | ||
var err error | ||
|
||
// Load values from file to a hash map | ||
f, err := os.Open("websites.csv") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer f.Close() | ||
|
||
r := csv.NewReader(f) | ||
|
||
records, err := r.ReadAll() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Holds user's search arguments and an appropriate search URL | ||
links := make(map[string]string) | ||
|
||
for _, record := range records { | ||
links[record[0]] = record[1] | ||
} | ||
|
||
return links | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"os/exec" | ||
|
||
aw "github.com/deanishe/awgo" | ||
) | ||
|
||
// doUpdate checks for a newer version of the workflow. | ||
func doUpdate() error { | ||
log.Println("Checking for update...") | ||
return wf.CheckForUpdate() | ||
} | ||
|
||
// checkForUpdate runs "./alsf update" in the background if an update check is due. | ||
func checkForUpdate() error { | ||
if !wf.UpdateCheckDue() || aw.IsRunning("update") { | ||
return nil | ||
} | ||
cmd := exec.Command(os.Args[0], "update") | ||
return aw.RunInBackground("update", cmd) | ||
} | ||
|
||
// showUpdateStatus adds an "update available!" message to Script Filters if an update is available | ||
// and query is empty. | ||
func showUpdateStatus() { | ||
if query != "" { | ||
return | ||
} | ||
|
||
if wf.UpdateAvailable() { | ||
wf.Configure(aw.SuppressUIDs(true)) | ||
log.Println("Update available!") | ||
wf.NewItem("An update is available!"). | ||
Subtitle("⇥ or ↩ to install update"). | ||
Valid(false). | ||
Autocomplete("workflow:update"). | ||
Icon(updateAvailable) | ||
} | ||
} |
Binary file not shown.
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