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

27 command history #71

Merged
merged 2 commits into from
Sep 16, 2018
Merged
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
35 changes: 35 additions & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,40 @@
"keys": ["g"],
"command": "shell_command_refresh",
"context": [{ "key": "setting.ShellCommand" }]
},
{
/**
* Select previous item in history:
*/

"keys": ["up"],
"command": "shell_command_history",
"args": {
"backwards": true
},
"context":
[
{
"key": "setting.shell_command_panel",
"operator": "equal",
"operand": true
}
]
},
{
/**
* Select next item in history:
*/

"keys": ["down"],
"command": "shell_command_history",
"context":
[
{
"key": "setting.shell_command_panel",
"operator": "equal",
"operand": true
}
]
}
]
6 changes: 5 additions & 1 deletion ShellCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from . import SublimeHelper as SH
from . import OsShell
from .hist import history


class ShellCommandCommand(SH.TextCommand):
Expand Down Expand Up @@ -69,6 +70,7 @@ def _C1(commands):

commands[idx] = command

history.insert('; '.join(commands))
self.run_shell_command(commands, stdin=stdin, panel=panel, target=target, title=title, syntax=syntax, refresh=refresh, wait_for_completion=wait_for_completion, root_dir=root_dir)

# If no command is specified then we prompt for one, otherwise
Expand All @@ -77,7 +79,9 @@ def _C1(commands):
if command is None:
if prompt is None:
prompt = self.default_prompt
window.show_input_panel(prompt, '', _C1, None, None)
initial = history.last() or ''
panel = window.show_input_panel(prompt, initial, _C1, None, None)
panel.settings().set("shell_command_panel", True)
else:
# A command can contain variables for substitution. The actual
# substitution takes place in the module VariableSubstitution,
Expand Down
65 changes: 65 additions & 0 deletions hist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# NOTE(kaste): Take from https://github.com/randy3k/AlignTab

# Copyright (c) 2015 Randy Lai <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


import sublime
import sublime_plugin


class History:
hist = []
index = None

def insert(self, user_input):
if not self.hist or (user_input != self.last() and user_input != "last_regex"):
self.hist.append(user_input)
self.index = None

def roll(self, backwards=False):
if self.index is None:
self.index = -1 if backwards else 0
else:
self.index += -1 if backwards else 1

if self.index == len(self.hist) or self.index < -len(self.hist):
self.index = -1 if backwards else 0

def last(self):
return self.hist[-1] if self.hist else None

def get(self, index=None):
if not index:
index = self.index
return self.hist[index] if self.hist else None

def reset_index(self):
self.index = None

if 'history' not in globals():
history = History()


class ShellCommandHistory(sublime_plugin.TextCommand):
def run(self, edit, backwards=False):
history.roll(backwards)
self.view.erase(edit, sublime.Region(0, self.view.size()))
self.view.insert(edit, 0, history.get())