Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow preference to active file folder #72

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Shell Turtlestein.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"surround_cmd": ["", ""],
"exec_args": {},
"cmd_settings": [],
"active_file_folder": false, // run commands under active file folder
"input_widget": {
// overridden for silly non-unixy OSes
"syntax": "Packages/ShellScript/Shell-Unix-Generic.tmLanguage"
Expand Down
30 changes: 19 additions & 11 deletions shell_turtlestein.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
from functools import partial


def settings():
return sublime.load_settings('Shell Turtlestein.sublime-settings')


def cwd_for_window(window):
"""
Return the working directory in which the window's commands should run.

If "active_file_folder" settings is true, return active file folder
In the common case when the user has one folder open, return that.
Otherwise, return one of the following (in order of preference):
1) One of the open folders, preferring a folder containing the active
Expand All @@ -15,17 +20,18 @@ def cwd_for_window(window):
3) The user's home directory.
"""
folders = window.folders()
if len(folders) == 1:
active_view = window.active_view()
active_file_name = active_view.file_name() if active_view else ''
if active_file_name and settings().get('active_file_folder'):
return os.path.dirname(active_file_name)
elif len(folders) == 1:
return folders[0]
else:
active_view = window.active_view()
active_file_name = active_view.file_name() if active_view else None
if not active_file_name:
return folders[0] if len(folders) else os.path.expanduser("~")
for folder in folders:
if active_file_name.startswith(folder):
return folder
return os.path.dirname(active_file_name)
default = folders and folder[0] or os.path.dirname(active_file_name)
return default or os.path.expanduser("~")


def abbreviate_user(path):
Expand All @@ -39,10 +45,6 @@ def abbreviate_user(path):
return path


def settings():
return sublime.load_settings('Shell Turtlestein.sublime-settings')


def cmd_settings(cmd):
"""
Return the default settings with settings for the command merged in
Expand Down Expand Up @@ -88,6 +90,7 @@ def run_cmd(cwd, cmd, wait, input_str=None):
subprocess.Popen(cmd, cwd=cwd, shell=shell)
return (False, None)


def show_in_output_panel(message):
window = sublime.active_window()
panel_name = 'shell_turtlestein'
Expand All @@ -97,6 +100,7 @@ def show_in_output_panel(message):
panel.end_edit(edit)
window.run_command('show_panel', {'panel': 'output.' + panel_name})


class ShellPromptCommand(sublime_plugin.WindowCommand):
"""
Prompt the user for a shell command to run in the window's directory
Expand All @@ -118,7 +122,6 @@ def run(self, **args):
for (setting, value) in list(settings().get('input_widget').items()):
inputview.settings().set(setting, value)


def on_done(self, cwd, cmd_str):
cmd = parse_cmd(cmd_str)
if not cmd['input'] and cmd['output'] == '|':
Expand Down Expand Up @@ -194,6 +197,7 @@ def run(self, edit, region_start=None, region_end=None, text=None):
else:
self.view.insert(edit, 0, text)


class SubprocessInCwdCommand(sublime_plugin.WindowCommand):
"""
Launch a subprocess using the window's working directory
Expand All @@ -212,6 +216,7 @@ def run(self, cmd=None, wait=False):

active_input_row = -1


def callback_with_history(callback, cmd_history, input_text):
if callback:
cmd = input_text.split("\n")[active_input_row]
Expand All @@ -220,6 +225,7 @@ def callback_with_history(callback, cmd_history, input_text):
cmd_history.append(cmd)
return callback(cmd)


def show_input_panel_with_readline(window, caption, cmd_history,
on_done, on_change, on_cancel):
global active_input_row
Expand All @@ -231,6 +237,7 @@ def show_input_panel_with_readline(window, caption, cmd_history,
view.show(view.size())
return view


class ReadlineHistoryChange(sublime_plugin.TextCommand):
def run_(self, someIntNotUsed, args):
# Override default run_ so that an edit isn't created.
Expand All @@ -244,6 +251,7 @@ def run(self, movement, movement_args):
global active_input_row
active_input_row, _ = self.view.rowcol(self.view.sel()[0].b)


class LeftDeleteOnLine(sublime_plugin.TextCommand):
def run(self, edit):
if self.view.rowcol(self.view.sel()[0].b)[1]:
Expand Down