Skip to content

Commit

Permalink
Add inline commands
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed May 13, 2021
1 parent a27b620 commit 46214a6
Show file tree
Hide file tree
Showing 33 changed files with 378 additions and 550 deletions.
Binary file added Alfred-Sublime-Text.afdesign
Binary file not shown.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
Sublime Text Projects Alfred Workflow
=====================================

View, filter and open your Sublime Text 3 (or VSCode) project files.
View, filter and open your Sublime Text (or VSCode) project files.

![][demo]

<!-- MarkdownTOC autolink="true" autoanchor="true" -->

- [Download & Installation](#download--installation)
- [Catalina](#catalina)
- [Catalina and later](#catalina-and-later)
- [Usage](#usage)
- [How it works](#how-it-works)
- [Configuration](#configuration)
Expand All @@ -25,10 +25,10 @@ Download & Installation
Download the workflow from [GitHub][gh-releases] and install by double-clicking the `Sublime-Text-Projects-X.X.X.alfredworkflow` file.


<a id="catalina"></a>
### Catalina ###
<a id="catalina-and-later"></a>
### Catalina and later ###

If you're running Catalina (macOS 10.15), you'll need to [grant the workflow executable permission to run][catalina].
If you're running Catalina or later (macOS 10.15+), you'll need to [grant the workflow executable permission to run][catalina].


<a id="usage"></a>
Expand All @@ -38,14 +38,17 @@ Usage
- `.st [<query>]` — List/filter your `.sublime-project` files
+ `↩` — Open result in Sublime Text
+ `⌘+↩` — Reveal file in Finder
- `.stconfig` — Show the current settings
- `.st rescan` — Reload cached list of projects
- `.st config` — Show the current settings
- `Workflow Is Up To Date` / `Workflow Update Available` — Install update or check for update
- `Edit Config File` — Open workflow's config file in Sublime Text
- `View Help File` — Open README in your browser
- `Report Issue` — Open GitHub issue tracker in your browser
- `Visit Forum Thread` — Open workflow's thread on [alfredforum.com][forum]
- `Rescan Projects` — Reload list of projects

You can enter the words `search` or `config` as a search query anywhere to jump to that screen.


<a id="how-it-works"></a>
How it works
Expand Down
143 changes: 119 additions & 24 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"log"
"os"
"os/exec"
"strings"
"time"

aw "github.com/deanishe/awgo"
Expand All @@ -29,6 +30,7 @@ var (
// project with "Sublime Text.app" doesn't.
sublPaths = []string{
"/usr/local/bin/subl",
"/Applications/Sublime Text 4.app/Contents/SharedSupport/bin/subl",
"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl",
}
// Candidate paths to `code` command-line program.
Expand Down Expand Up @@ -58,25 +60,26 @@ type options struct {
}

func init() {
cli.BoolVar(&opts.Search, "search", false, "search projects")
cli.BoolVar(&opts.Config, "conf", false, "show/filter configuration")
cli.BoolVar(&opts.Open, "open", false, "open specified file in default app")
cli.BoolVar(&opts.OpenProject, "project", false, "open specified project")
cli.BoolVar(&opts.OpenFolder, "folder", false, "open specified project")
cli.BoolVar(&opts.Rescan, "rescan", false, "re-scan for projects")
cli.BoolVar(&opts.Force, "force", false, "force rescan")
cli.Usage = func() {
fmt.Fprint(os.Stderr, `usage: alfsubl [options] [arguments]
fmt.Fprint(os.Stderr, `usage: alfred-sublime [options] [arguments]
Alfred workflow to show Sublime Text/VSCode projects.
Usage:
alfsubl [<query>]
alfsubl -conf [<query>]
alfsubl -open <path>
alfsubl -project <project file>
alfsubl -folder <project file>
alfsubl -rescan [-force]
alfsubl -h|-help
alfred-sublime -search [<query>]
alfred-sublime -conf [<query>]
alfred-sublime -open <path>
alfred-sublime -project <project file>
alfred-sublime -folder <project file>
alfred-sublime -rescan [-force]
alfred-sublime -h|-help
Options:
`)
Expand Down Expand Up @@ -156,6 +159,8 @@ func runConfig() {
// prevent Alfred from re-ordering results
if opts.Query == "" {
wf.Configure(aw.SuppressUIDs(true))
} else {
wf.Var("query", opts.Query)
}

log.Printf("filtering config %q ...", opts.Query)
Expand All @@ -179,45 +184,49 @@ func runConfig() {
wf.NewItem("Edit Config File").
Subtitle("Edit directories to scan").
Valid(true).
Arg(configFile).
Arg("-open", "--", configFile).
UID("config").
Icon(iconSettings).
Var("action", "-open")
Var("hide_alfred", "true")

wf.NewItem("View Help File").
Subtitle("Open workflow help in your browser").
Arg("README.html").
Arg("-open", "README.html").
UID("help").
Valid(true).
Icon(iconHelp).
Var("action", "-open")
Var("hide_alfred", "true")

wf.NewItem("Report Issue").
Subtitle("Open workflow issue tracker in your browser").
Arg(issueTrackerURL).
Arg("-open", issueTrackerURL).
UID("issue").
Valid(true).
Icon(iconIssue).
Var("action", "-open")
Var("hide_alfred", "true")

wf.NewItem("Visit Forum Thread").
Subtitle("Open workflow thread on alfredforum.com in your browser").
Arg(forumThreadURL).
Arg("-open", forumThreadURL).
UID("forum").
Valid(true).
Icon(iconURL).
Var("action", "-open")
Var("hide_alfred", "true")

// TODO: add "back to" setting for rescan command
wf.NewItem("Rescan Projects").
Subtitle("Rebuild cached list of projects").
Arg("-rescan", "-force").
Valid(true).
UID("rescan").
Icon(iconReload).
Var("action", "rescan").
Var("notification", "Reloading project list…")
// Var("hide_alfred", "false").
Var("notification", "Reloading project list…").
Var("trigger", "config")

if opts.Query != "" {
wf.Filter(opts.Query)
addNavigationItems(opts.Query, "config", "rescan")
}

wf.WarnEmpty("No Matching Items", "Try a different query")
Expand All @@ -244,6 +253,7 @@ func runScan() {
if err := sm.Scan(); err != nil {
wf.FatalError(err)
}
fmt.Print("Project scan completed")
}

// Open path/URL
Expand Down Expand Up @@ -292,7 +302,7 @@ func runSearch() {
wf.NewItem("Scanning projects…").
Subtitle("Results will refresh in a few seconds").
Valid(false).
Icon(ReloadIcon())
Icon(iconSpinner())

wf.SendFeedback()
return
Expand All @@ -308,12 +318,11 @@ func runSearch() {
it := wf.NewItem(proj.Name()).
Subtitle(util.PrettyPath(proj.Path)).
Valid(true).
Arg(proj.Path).
Arg("-project", "--", proj.Path).
UID(proj.Path).
IsFile(true).
Icon(icon).
Var("action", "-project").
Var("close", "true")
Var("hide_alfred", "true")

if len(proj.Folders) > 0 {
sub := "Open Project Folder"
Expand All @@ -323,17 +332,103 @@ func runSearch() {
it.NewModifier("cmd").
Subtitle(sub).
Icon(&aw.Icon{Value: "public.folder", Type: "filetype"}).
Var("action", "-folder")
Arg("-folder", proj.Path)
}
}

if opts.Query != "" {
res := wf.Filter(opts.Query)
for _, r := range res {
log.Printf("[search] %0.2f %#v", r.Score, r.SortKey)
log.Printf("[search] %6.2f %#v", r.Score, r.SortKey)
}
addNavigationItems(opts.Query, "search")
}

wf.WarnEmpty("No Projects Found", "Try a different query?")
wf.SendFeedback()
}

func addNavigationItems(query string, ignore ...string) {
if len(query) < 3 {
return
}
var (
items = []struct {
keywords []string
trigger string
title string
subtitle string
arg []string
note string
icon *aw.Icon
}{
{
[]string{"reload", "rescan"},
"rescan",
"Rescan Projects",
"Rescan disk & update cached list of projects",
[]string{"-rescan", "-force"},
"Reloading project list …",
iconReload,
},
{
[]string{"config", "prefs", "settings"},
"config",
"Workflow Settings",
"Access workflow's preferences",
nil,
"",
iconSettings,
},
{
[]string{"search", "projects", ".st"},
"search",
"Search Projects",
"Search scanned projects",
nil,
"",
aw.IconWorkflow,
},
}
)

query = strings.ToLower(query)
for _, conf := range items {
if sliceContains(ignore, conf.trigger) {
continue
}
for _, kw := range conf.keywords {
if strings.HasPrefix(strings.ToLower(kw), query) {
it := wf.NewItem(conf.title).
Subtitle(conf.subtitle).
Icon(conf.icon).
UID("navigation-action."+conf.trigger).
Valid(true).
Var("trigger", conf.trigger).
Var("query", "")

if conf.trigger == "rescan" {
it.Var("trigger", "search")
}

if conf.arg != nil {
it.Arg(conf.arg...)
}

if conf.note != "" {
it.Var("notification", conf.note)
}
break
}
}
}
}

func sliceContains(sl []string, s string) bool {
for _, v := range sl {
if v == s {
return true
}
}
return false
}
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ module github.com/deanishe/alfred-sublime-text

require (
github.com/BurntSushi/toml v0.3.1
github.com/bmatcuk/doublestar v1.3.0 // indirect
github.com/bmatcuk/doublestar v1.3.4 // indirect
github.com/davecgh/go-spew v1.1.1
github.com/deanishe/awgo v0.22.1
github.com/disintegration/imaging v1.6.2
github.com/deanishe/awgo v0.28.0
github.com/gobwas/glob v0.2.3
github.com/magefile/mage v1.9.0
github.com/magefile/mage v1.11.0
github.com/yosuke-furukawa/json5 v0.1.1
golang.org/x/image v0.0.0-20200430140353-33d19683fad8 // indirect
howett.net/plist v0.0.0-20200419221736-3b63eb3a43b5 // indirect
golang.org/x/text v0.3.6 // indirect
howett.net/plist v0.0.0-20201203080718-1454fab16a06 // indirect
)

go 1.13
Loading

0 comments on commit 46214a6

Please sign in to comment.