Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Fahad Hossain committed Dec 7, 2018
0 parents commit bc58fb9
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# i3-sensible-app

Opens a predefined app based on the currently focused i3 workspace.

## Usage
Change your hotkey from i3-sensible-terminal to i3-sensible-app

```diff
-bindsym Mod4+Return exec i3-sensible-terminal
+bindsym Mod4+Return exec i3-sensible-app
```

Create the default apps config in `~/.config/i3/defaults.json`

Example:

```json
{
"1: ": ["albert", "toggle"],
"2: ": ["google-chrome-stable"],
"3: ": ["i3-sensible-terminal"],
"4: ": ["code"],
"5: ": ["nautilus"],
"6: ?": ["albert", "toggle"],
"7: ": ["evince"],
"8: ?": ["albert", "toggle"],
"9: ": ["xdg-open", "http://localhost:6680/iris"],
"10: ": ["mpv", "--idle"]
}
```

**Now just shift to your desired workspace and press <kbd>⊞ Win</kbd> + <kbd>Enter ⏎</kbd>**


## Installation

### Pre-requisites

* i3 (and i3-msg)
* go (for compilation)

### INSTALL (with go) (*preferred*)
Open a terminal and run

```bash
go get github.com/fa7ad/i3-sensible-app
```

### INSTALL (from git)
Clone this repo and change into the directory

```bash
git clone https://github.com/fa7ad/i3-sensible-app.git
cd i3-sensible-app/
```

Build the binary

```bash
go build
```

Move the directory to a folder in PATH

```bash
sudo install -Dm755 i3-sensible-app /usr/bin
```

### INSTALL (manual)
Download the binary from [releases](https://github.com/fa7ad/i3-sensible-app/releases)

## LICENSE
MIT License
77 changes: 77 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
)

var conf []byte

type workspace struct {
Name string
Focused bool
}

func getEnv(env, def string) string {
val, ok := os.LookupEnv(env)
if ok {
return val
}
return def
}

func init() {
fback := path.Join(getEnv("HOME", "~"), ".config")
file := path.Join(getEnv("XDG_CONFIG_HOME", fback), "i3", "defaults.json")
cfile, err := filepath.Abs(file)
if err != nil {
log.Fatalf("Config file %s doesn't exist", file)
}
conf, err = ioutil.ReadFile(cfile)
if err != nil {
log.Fatalf("Failed to read config file %s!", cfile)
}
}

func getFocusedWs(wss []workspace) (workspace string, err error) {
for _, ws := range wss {
if ws.Focused {
workspace = ws.Name
}
}
if len(workspace) < 1 {
err = errors.New("no active workspace found")
}
return
}

func main() {
var config map[string]([]string)
err := json.Unmarshal(conf, &config)
if err != nil {
log.Fatal(err)
}
out, err := exec.Command("i3-msg", "-t", "get_workspaces").Output()
if err != nil {
log.Fatal(err)
}
var output []workspace
json.Unmarshal(out, &output)
ws, err := getFocusedWs(output)
if err != nil {
log.Fatalln(err)
}

args := config[ws]
cmd := exec.Command(args[0], args[1:]...)
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
}

0 comments on commit bc58fb9

Please sign in to comment.