Skip to content

Commit

Permalink
Update dependencies and tidy up code
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Oct 19, 2019
1 parent 3c2cf74 commit 2fe734b
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 274 deletions.
116 changes: 70 additions & 46 deletions README.html

Large diffs are not rendered by default.

46 changes: 35 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,36 @@
Sublime Text Projects Alfred Workflow
=====================================

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

![][demo]

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

- [Download & Installation](#download--installation)
- [Catalina](#catalina)
- [Usage](#usage)
- [How it works](#how-it-works)
- [Configuration](#configuration)
- [Licensing, thanks](#licensing-thanks)

<!-- /MarkdownTOC -->


<a id="download--installation"></a>
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 ###

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


<a id="usage"></a>
Usage
-----

Expand All @@ -25,30 +44,34 @@ Usage
- `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


<a id="how-it-works"></a>
How it works
------------

The workflow scans your system for `.sublime-project` files using `locate`, `mdfind` and (optionally) `find`. It then caches the list of projects for 10 minutes (by default).
The workflow scans your system for `.sublime-project` (or `.code-workspace`) files using `locate`, `mdfind` and (optionally) `find`. It then caches the list of projects for 10 minutes (by default).

As the `locate` database isn't enabled on most machines (and isn't updated frequently in any case), and `mdfind` ignores hidden directories, there is an additional, optional `find`-based scanner to "fill the gaps", which you must specifically configure (see below).


<a id="configuration"></a>
Configuration
-------------

Scan intervals are configured in the [workflow's configuration sheet in Alfred Preferences][confsheet]:

| Variable | Usage |
|-------------------|-----------------------------------------------|
| `INTERVAL_FIND` | How long to cache `find` search results for |
| `INTERVAL_LOCATE` | How long to cache `locate` search results for |
| `INTERVAL_MDFIND` | How long to cache `mdfind` search results for |
| Variable | Usage |
|-------------------|-----------------------------------------------------------|
| `INTERVAL_FIND` | How long to cache `find` search results for |
| `INTERVAL_LOCATE` | How long to cache `locate` search results for |
| `INTERVAL_MDFIND` | How long to cache `mdfind` search results for |
| `VSCODE` | Set to `1` or `true` to switch to Visual Studio Code mode |

The values should be of the form `10m` or `2h`. Set to `0` to disable a particular scanner.
The interval values should be of the form `10m` or `2h`. Set to `0` to disable a particular scanner.

The workflow should work "out of the box", but if you have project files in directories that `mdfind` doesn't see (hidden directories, network shares), you may have to explicitly add some search paths to the `sublime.toml` configuration file in the workflow's data directory. The file is created on first run, and you can use `.stconfig > Edit Config File` to open it in Sublime Text.
The workflow should work "out of the box", but if you have project files in directories that `mdfind` doesn't see (hidden directories, network shares), you may have to explicitly add some search paths to the `sublime.toml` configuration file in the workflow's data directory. The file is created on first run, and you can use `.stconfig > Edit Config File` to open it.

These directories are searched with `find`.

Expand All @@ -57,12 +80,13 @@ You can also add glob patterns to the `excludes` list in the settings file to ig
The options are documented in the settings file itself.


<a id="licensing-thanks"></a>
Licensing, thanks
-----------------

All the code is released under the [MIT Licence][mit].

The workflow is based on the [AwGo workflow library][awgo] and [docopt][docopt], both also released under the [MIT Licence][mit].
The workflow is based on the [AwGo workflow library][awgo], also released under the [MIT Licence][mit].

The icons are based on [Font Awesome][awesome] and [Material Design Icons][matcom].

Expand All @@ -74,4 +98,4 @@ The icons are based on [Font Awesome][awesome] and [Material Design Icons][matco
[gh-releases]: https://github.com/deanishe/alfred-sublime-text/releases/latest
[mit]: http://opensource.org/licenses/MIT
[confsheet]: https://www.alfredapp.com/help/workflows/advanced/variables/#environment
[docopt]: https://github.com/docopt/docopt.go
[catalina]: https://github.com/deanishe/awgo/wiki/Catalina
116 changes: 49 additions & 67 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,20 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"time"

"github.com/pkg/errors"

aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/util"
docopt "github.com/docopt/docopt-go"
)

var (
usage = `alfsubl <command> [<args>]
Alfred workflow to show Sublime Text projects.
Usage:
alfsubl search [<query>]
alfsubl config [<query>]
alfsubl open [--subl] <path>
alfsubl open-project <projfile>
alfsubl open-folder <projfile>
alfsubl rescan [--force]
Options:
-s, --subl open file in Sublime Text instead of default app
-f, --force ignore cached data
-h, --help show this message and exit
--version show version number and exit
`

conf *config
opts = &options{}
cli = flag.NewFlagSet("alfsubl", flag.ContinueOnError)

// Candidate paths to `subl` command-line program. We'll open projects
// via `subl` because it correctly loads the workspace. Opening a
Expand All @@ -61,40 +41,48 @@ Options:

// CLI flags
type options struct {
// Sub-commands
// Commands
Search bool
Config bool
Ignore bool
Open bool
OpenProject bool `docopt:"open-project"`
OpenFolder bool `docopt:"open-folder"`
OpenProject bool
OpenFolder bool
Rescan bool

// Options
Force bool
UseSublime bool `docopt:"--subl"`
Force bool

// Arguments
Query string
Path string
ProjectPath string `docopt:"<projfile>"`
Query string
}

// Parse command-line flags
func parseArgs(argv []string) error {
// log.Printf("argv=%#v", argv)
args, err := docopt.ParseArgs(usage, argv, wf.Version())
if err != nil {
return err
}
func init() {
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]
if err := args.Bind(opts); err != nil {
return errors.Wrap(err, "bind CLI flags")
}
Alfred workflow to show Sublime Text/VSCode projects.
log.Printf("opts=%#v", opts)
Usage:
alfsubl [<query>]
alfsubl -conf [<query>]
alfsubl -open <path>
alfsubl -project <project file>
alfsubl -folder <project file>
alfsubl -rescan [-force]
alfsubl -h|-help
Options:
`)

return nil
cli.PrintDefaults()
}
}

func commandForProject(path string) *exec.Cmd {
Expand Down Expand Up @@ -122,8 +110,8 @@ func commandForProject(path string) *exec.Cmd {
func runOpenProject() {
wf.Configure(aw.TextErrors(true))

log.Printf("opening project %q ...", opts.ProjectPath)
cmd := commandForProject(opts.ProjectPath)
log.Printf("opening project %q ...", opts.Query)
cmd := commandForProject(opts.Query)

if _, err := util.RunCmd(cmd); err != nil {
wf.Fatalf("exec command %#v: %v", cmd, err)
Expand All @@ -145,7 +133,7 @@ func runOpenFolder() {
}

for _, proj := range projs {
if proj.Path == opts.ProjectPath {
if proj.Path == opts.Query {
for _, path := range proj.Folders {

log.Printf("opening folder %q ...", path)
Expand All @@ -159,7 +147,7 @@ func runOpenFolder() {
}
}

wf.Fatalf("no folders found for project %q", opts.ProjectPath)
wf.Fatalf("no folders found for project %q", opts.Query)
}

// Filter configuration in Alfred
Expand Down Expand Up @@ -194,31 +182,31 @@ func runConfig() {
Arg(configFile).
UID("config").
Icon(iconSettings).
Var("action", "open")
Var("action", "-open")

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

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

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

wf.NewItem("Rescan Projects").
Subtitle("Rebuild cached list of projects").
Expand All @@ -242,13 +230,13 @@ func runScan() {

if opts.Force {
if conf.FindInterval != 0 {
conf.FindInterval = time.Nanosecond * 1
conf.FindInterval = time.Nanosecond
}
if conf.MDFindInterval != 0 {
conf.MDFindInterval = time.Nanosecond * 1
conf.MDFindInterval = time.Nanosecond
}
if conf.LocateInterval != 0 {
conf.LocateInterval = time.Nanosecond * 1
conf.LocateInterval = time.Nanosecond
}
}

Expand All @@ -258,26 +246,21 @@ func runScan() {
}
}

// Open path/URL, optionally in Sublime
// Open path/URL
func runOpen() {

wf.Configure(aw.TextErrors(true))

var args []string
if opts.UseSublime {
args = []string{"-a", "Sublime Text"}
}
args = append(args, opts.Path)
args = append(args, opts.Query)

cmd := exec.Command("open", args...)
if _, err := util.RunCmd(cmd); err != nil {
wf.Fatalf("open %q: %v", opts.Path, err)
wf.Fatalf("open %q: %v", opts.Query, err)
}
}

// Filter Sublime projects in Alfred
func runSearch() {

var (
projs []Project
err error
Expand All @@ -291,9 +274,9 @@ func runSearch() {
// Run "alfsubl rescan" in background if need be
if sm.ScanDue() && !wf.IsRunning("rescan") {
log.Println("rescanning for projects ...")
cmd := exec.Command(os.Args[0], "rescan")
cmd := exec.Command(os.Args[0], "-rescan")
if err := wf.RunInBackground("rescan", cmd); err != nil {
log.Printf(`error running "%s rescan": %v`, os.Args[0], err)
log.Printf("error running rescan: %v", err)
wf.Fatal("Error scanning for repos. See log file.")
}
}
Expand All @@ -306,7 +289,6 @@ func runSearch() {
if len(projs) == 0 && wf.IsRunning("rescan") {

wf.Rerun(0.1)

wf.NewItem("Scanning projects…").
Subtitle("Results will refresh in a few seconds").
Valid(false).
Expand All @@ -330,7 +312,7 @@ func runSearch() {
UID(proj.Path).
IsFile(true).
Icon(icon).
Var("action", "open-project").
Var("action", "-project").
Var("close", "true")

if len(proj.Folders) > 0 {
Expand All @@ -343,7 +325,7 @@ func runSearch() {
it.NewModifier("cmd").
Subtitle(sub).
Icon(&aw.Icon{Value: "public.folder", Type: "filetype"}).
Var("action", "open-folder")
Var("action", "-folder")
}
}

Expand Down
Loading

0 comments on commit 2fe734b

Please sign in to comment.