From 7e645d70db173f5d30991bc2e998bd1eb832fce4 Mon Sep 17 00:00:00 2001 From: moshee Date: Sat, 15 Mar 2014 21:58:47 -0700 Subject: [PATCH] Command history --- lib/command-view.coffee | 25 ++++++++++++++++++++++++- lib/pipe.coffee | 8 +++++++- package.json | 19 +++++++++++++++++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/command-view.coffee b/lib/command-view.coffee index dd2eebb..8ac7c49 100644 --- a/lib/command-view.coffee +++ b/lib/command-view.coffee @@ -20,13 +20,36 @@ class CommandView extends View @samplePlaceholder: -> @placeholders[Math.floor(Math.random()*@placeholders.length)] - initialize: (callback) -> + initialize: (history, callback) -> + historyPos = history.length + cur = '' + @on 'core:cancel core:close', => callback(null) @detach() @on 'core:confirm', => callback(@commandLine.getText()) @detach() + @commandLine.on 'keydown', (e) => + if history.length is 0 then return + + switch e.keyCode + when 38 # up + unless historyPos <= 0 + historyPos-- + @commandLine.setText history[historyPos] + + when 40 # down + if historyPos >= history.length-1 + historyPos = history.length + @commandLine.setText cur + else + historyPos++ + @commandLine.setText history[historyPos] + + else + if historyPos >= history.length + cur = @commandLine.getText() atom.workspaceView.append(this) @commandLine.focus() diff --git a/lib/pipe.coffee b/lib/pipe.coffee index ec25ab5..8db43da 100644 --- a/lib/pipe.coffee +++ b/lib/pipe.coffee @@ -2,6 +2,8 @@ {spawn} = require 'child_process' CommandView = require './command-view' +history = [] + module.exports = activate: -> atom.workspaceView.command 'pipe:run', => @run() @@ -11,11 +13,15 @@ module.exports = view = atom.workspaceView.getActiveView() return if not editor? - new CommandView (commandString) -> + new CommandView history, (commandString) -> if not commandString view.focus() return + history.push commandString + if history.length > 300 + history.shift() + range = editor.getSelectedBufferRange() stdout = '' stderr = '' diff --git a/package.json b/package.json index 58b21ec..db6aa09 100644 --- a/package.json +++ b/package.json @@ -6,10 +6,25 @@ "activationEvents": [ "pipe:run" ], - "repository": "https://github.com/moshee/atom-pipe", + "repository": { + "type": "git", + "url": "https://github.com/moshee/atom-pipe" + }, "license": "MIT", "engines": { "atom": ">0.72.0" }, - "dependencies": {} + "dependencies": {}, + "readme": "# pipe\n\nSelect text to pipe into external commands and replace it with the output. Sort\nof like `!` in vim. Commands are sent to your `$SHELL`.\n\nDefault keybinding to activate is `cmd-;`.\n\n![pipe in action](https://i.ktkr.us/hoeJ)\n", + "readmeFilename": "README.md", + "bugs": { + "url": "https://github.com/moshee/atom-pipe/issues" + }, + "homepage": "https://github.com/moshee/atom-pipe", + "_id": "pipe@0.1.1", + "dist": { + "shasum": "065fe0c782ea86980b8cf8e19d2fc40b5851c941" + }, + "_resolved": "/var/folders/hv/nq12s2fx5lv48rtffsd8v2_h0000gn/T/d-114214-49660-1x5dm6u/package.tgz", + "_from": "/var/folders/hv/nq12s2fx5lv48rtffsd8v2_h0000gn/T/d-114214-49660-1x5dm6u/package.tgz" } -- 2.39.2