Skip to content

Commit

Permalink
Added directory change following.
Browse files Browse the repository at this point in the history
  • Loading branch information
raguay committed Dec 20, 2018
1 parent 39a9395 commit 35baeac
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 43 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ After restarting **fman**, you will be able to open the current directory in [iT

### Usage

Pressing **shift-o** will open the current directory in [iTerm2](https://www.iterm2.com/).
Pressing **shift-o** will open the current directory in [iTerm2](https://www.iterm2.com/). You can use the new commands below to open iTerm2 windows in the current directory and for having iTerm2 follow your directory changes.

### New Commands

Expand All @@ -24,6 +24,10 @@ This command turns on tab mode so that a new tab will be created.

This command toggles the tab mode.

#### `Toggle Follow Left Pane iTerm2` and `Toggle Follow Right Pane iTerm2`

These commands will toggle the follow directory changes for the left and right panels.

### Features

- Opens current directory in [iTerm2](https://www.iterm2.com/).
112 changes: 70 additions & 42 deletions openiterm2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,54 @@
from fman import DirectoryPaneCommand, show_alert, DATA_DIRECTORY, FMAN_VERSION, load_json, save_json
from fman import DirectoryPaneCommand, show_alert, DATA_DIRECTORY, FMAN_VERSION, load_json, save_json, DirectoryPaneListener
import os
from fman.url import as_human_readable
from fman.url import as_url

ntab = False
fmanv = 0

class OpenIterm2(DirectoryPaneCommand):
def __call__(self):
ntab = False
loc = load_json('OpenIterm2.json')
if loc is None:
jloc = dict()
jloc['NewTab'] = False
save_json('OpenIterm2.json', jloc)
loadVars()
dirName = as_human_readable(self.pane.get_path())
goToDir(dirName)

def loadVars():
global fmanv, ntab
loc = load_json('OpenIterm2.json')
if loc is None:
jloc = dict()
jloc['NewTab'] = False
save_json('OpenIterm2.json', jloc)
else:
ntab = loc['NewTab']
if isinstance(FMAN_VERSION, str):
fmanvParts = FMAN_VERSION.split('.')
if fmanvParts[0] == '0':
fmanv = int(fmanvParts[1])
else:
ntab = loc['NewTab']
fmanv = 0
if isinstance(FMAN_VERSION, str):
fmanvParts = FMAN_VERSION.split('.')
if fmanvParts[0] == '0':
fmanv = int(fmanvParts[1])
else:
fmanv = int(fmanvParts[0])*10 + int(fmanvParts[1])
fmanv = int(fmanvParts[0])*10 + int(fmanvParts[1])
else:
fmanv = FMAN_VERSION

def goToDir(dirName):
global fmanv, ntab
if fmanv > 4:
if ntab:
os.system("/usr/bin/osascript '" + DATA_DIRECTORY + "/Plugins/Third-party/OpeniTerm2/OpeniTerm2-newtab.scpt' '" + dirName + "'")
else:
fmanv = FMAN_VERSION
selected_files = self.pane.get_selected_files()
if len(selected_files) >= 1 or (len(selected_files) == 0 and self.get_chosen_files()):
if len(selected_files) == 0 and self.get_chosen_files():
selected_files.append(self.get_chosen_files()[0])
dirName = as_human_readable(selected_files[0])
if os.path.isfile(dirName):
dirName = os.path.dirname(dirName)
print(fmanv)
if fmanv > 4:
if ntab:
os.system("/usr/bin/osascript '" + DATA_DIRECTORY + "/Plugins/Third-party/OpeniTerm2/OpeniTerm2-newtab.scpt' '" + dirName + "'")
else:
os.system("/usr/bin/osascript '" + DATA_DIRECTORY + "/Plugins/Third-party/OpeniTerm2/OpeniTerm2.scpt' '" + dirName + "'")
else:
if ntab:
os.system("/usr/bin/osascript '" + DATA_DIRECTORY + "/Plugins/OpeniTerm2/OpeniTerm2-newtab.scpt' '" + dirName + "'")
else:
os.system("/usr/bin/osascript '" + DATA_DIRECTORY + "/Plugins/OpeniTerm2/OpeniTerm2.scpt' '" + dirName + "'")
os.system("/usr/bin/osascript '" + DATA_DIRECTORY + "/Plugins/Third-party/OpeniTerm2/OpeniTerm2.scpt' '" + dirName + "'")
else:
if ntab:
os.system("/usr/bin/osascript '" + DATA_DIRECTORY + "/Plugins/OpeniTerm2/OpeniTerm2-newtab.scpt' '" + dirName + "'")
else:
show_alert("No directory selected")
os.system("/usr/bin/osascript '" + DATA_DIRECTORY + "/Plugins/OpeniTerm2/OpeniTerm2.scpt' '" + dirName + "'")


class OpenIterm2(DirectoryPaneCommand):
def __call__(self):
loadVars()
dirName = as_human_readable(self.pane.get_path())
goToDir(dirName)

class SetOpenIterm2TabOff(DirectoryPaneCommand):
def __call__(self):
Expand All @@ -58,14 +64,36 @@ def __call__(self):

class ToggleOpenIterm2Tab(DirectoryPaneCommand):
def __call__(self):
loc = load_json('OpenIterm2.json')
ntab = False
if loc is None:
global fmanv, ntab
loc = load_json('OpenIterm2.json')
if loc is None:
jloc = dict()
jloc['NewTab'] = False
save_json('OpenIterm2.json',jloc)
else:
save_json('OpenIterm2.json', jloc)
else:
ntab = loc['NewTab']
loc['NewTab'] = not ntab
save_json("OpenIterm2.json",loc)
loc['NewTab'] = not ntab
ntab = not ntab
save_json("OpenIterm2.json",loc)

fLeft = False
fRight = False

class DirectoryPaneListener(DirectoryPaneListener):
def on_path_changed(self):
global fLeft, fRight
panes = self.pane.window.get_panes()
if(fmanv == 0):
loadVars()
if((fLeft and (panes[0] == self.pane)) or (fRight and (panes[1] == self.pane))):
goToDir(as_human_readable(self.pane.get_path()))

class ToggleFollowLeftPaneIterm2(DirectoryPaneCommand):
def __call__(self):
global fLeft
fLeft = not fLeft

class ToggleFollowRightPaneIterm2(DirectoryPaneCommand):
def __call__(self):
global fRight
fRight = not fRight

0 comments on commit 35baeac

Please sign in to comment.