Skip to content

Commit

Permalink
Lots of plumbing.
Browse files Browse the repository at this point in the history
  • Loading branch information
vic committed Mar 17, 2011
1 parent 6458432 commit bc13885
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 144 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source :rubygems
gem 'command-t'
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GEM
remote: http://rubygems.org/
specs:
command-t (1.0.1)

PLATFORMS
ruby

DEPENDENCIES
command-t
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2011, Victor Hugo Borja <vic.borja@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 changes: 48 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,58 @@
Mr T
====

Textmate-like file finder on a curses interface. Intended for file completion on
shell prompt.
What is it.
-----------

Mr T requires a ruby compiled with curses devel, it also makes use of the ruby
extension provided by (Command-T)[https://wincent.com/products/command-t]
Mr T is a curses based file finder. If you're familiar with Textmate's cmd-t or
Vim's Command-T plugin, you'll feel right at home.

Mr T allows you to have fast, file completion from your shell prompt. You can
use it as-is by invoking the _mrT_ command, or use it as your default file
completion strategy for some unix commands.

== Basic Usage

mrT [basedir]
Requirements
------------

== TODO
Mr T requires a ruby compiled with the standard curses library (you need to have
curses-devel installed at ruby configuration time), it also makes use of the ruby
extension provided by [Command-T][http://wincent.com/products/command-t]


Installation
------------

rake install



Usage
-----

After installation, you might be able to use the _mrT_ binary, right now it
takes an optional directory as only argument.

mrT


Future
------

Use Command-T caching features, expose Command-T options as command line flags.


Contribute
----------

Feel free to adapt mrT to your needs, report any issue or send pull requests
to the github repository:

http://github.com/vic/mrT

Authors
-------

Victor Hugo Borja <vic.borja@gmail.com>

Instructions on how to setup as bash file completion mechanism.

10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
task :gem do
sh 'gem build mrT.gemspec'
end

task :install => :gem do
v = `git describe --abbrev=0`.chomp
sh "gem install mrT-#{v}.gem"
end

task :default => :install
146 changes: 10 additions & 136 deletions bin/mrT
Original file line number Diff line number Diff line change
@@ -1,141 +1,15 @@
#!/usr/bin/env ruby

# TODO: make command-t a reusable gem!
$LOAD_PATH.unshift File.expand_path('~/.vim/bundle/command-t/ruby')

require 'curses'
require 'command-t/finder'

class BashT

attr_reader :str, :shown_from, :pwd

def initialize(pwd)
@str = []
@options = {
}
@pwd = pwd
@finder = CommandT::Finder.new @pwd, @options
end

def run
value = with_curses { interact }
File.expand_path(value, @pwd) if value
end

def with_curses
Curses.init_screen
Curses.nonl
Curses.cbreak
Curses.noecho

@screen = Curses.stdscr
@screen.scrollok true
@screen.keypad true

begin
value = yield
rescue Interrupt
Curses.close_screen
ensure
Curses.close_screen
end
end

def interact
catch :done do
filter
while true
Curses.setpos(0,0)
@screen.clrtoeol
Curses.addstr('>> ')
Curses.addstr(str.join)
@screen.refresh
c = Curses.getch
case c
when 27 # ESCAPE
throw :done
when Curses::KEY_ENTER, 13
throw :done, @matches[@selected]
when Curses::KEY_RIGHT, 9 # TAB
item_incr(10)
when Curses::KEY_LEFT, 353 # SHIT-TAB
item_incr(-10)
when Curses::KEY_UP, Curses::KEY_CTRL_P
item_incr(-1)
when Curses::KEY_DOWN, Curses::KEY_CTRL_N
item_incr(1)
when Curses::KEY_NPAGE
item_incr(page_size)
when Curses::KEY_PPAGE
item_incr(-page_size)
when Curses::KEY_BACKSPACE
str.pop
filter
when (0..255)
str << c.chr
filter
end
end
end
end

def shown_to
shown_from + page_size
end

def page_size
@screen.maxy - 2
end

def row(index = @selected)
index - shown_from + 1
end

def select(index)
render_item
if index > shown_to
render(index - shown_to + shown_from)
elsif index < shown_from
render(index)
end
@selected = index
render_item index, true
end

def item_incr(incr)
index = @selected + incr
index = 0 if index < 0
index = @matches.size - 1 unless index < @matches.size
select index
end

def render_item(index = @selected, standout = false)
Curses.setpos(row(index) , 0)
@screen.clrtoeol
Curses.standout if standout
Curses.addstr @matches[index]
Curses.standend if standout
end

def render(from = 0)
@shown_from = from
(0..page_size).map { |y| render_item y + from }
end
if __FILE__ == $0
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'rubygems'
end

def filter
pattern = str.join
@selected = 0
@matches = @finder.sorted_matches_for pattern
render @selected
render_item @selected, true
end
gem 'command-t'

end
require 'mrT'

if $0 == __FILE__
dir = Dir.pwd
dir = ARGV.first if ARGV.first && File.directory?(ARGV.first)
res = BashT.new(dir).run
puts res if res
end
dir = Dir.pwd
dir = ARGV.first if ARGV.first && File.directory?(ARGV.first)
res = MrT.new(dir).run
puts res if res
130 changes: 130 additions & 0 deletions lib/mrT.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
require 'curses'
require 'command-t/finder'

class MrT

attr_reader :str, :shown_from, :pwd

def initialize(pwd)
@str = []
@options = {
}
@pwd = pwd
@finder = CommandT::Finder.new @pwd, @options
end

def run
value = with_curses { interact }
File.expand_path(value, @pwd) if value
end

def with_curses
Curses.init_screen
Curses.nonl
Curses.cbreak
Curses.noecho

@screen = Curses.stdscr
@screen.scrollok true
@screen.keypad true

begin
value = yield
rescue Interrupt
Curses.close_screen
ensure
Curses.close_screen
end
end

def interact
catch :done do
filter
while true
Curses.setpos(0,0)
@screen.clrtoeol
Curses.addstr('>> ')
Curses.addstr(str.join)
@screen.refresh
c = Curses.getch
case c
when 27 # ESCAPE
throw :done
when Curses::KEY_ENTER, 13
throw :done, @matches[@selected]
when Curses::KEY_RIGHT, 9 # TAB
item_incr(10)
when Curses::KEY_LEFT, 353 # SHIT-TAB
item_incr(-10)
when Curses::KEY_UP, Curses::KEY_CTRL_P
item_incr(-1)
when Curses::KEY_DOWN, Curses::KEY_CTRL_N
item_incr(1)
when Curses::KEY_NPAGE
item_incr(page_size)
when Curses::KEY_PPAGE
item_incr(-page_size)
when Curses::KEY_BACKSPACE
str.pop
filter
when (0..255)
str << c.chr
filter
end
end
end
end

def shown_to
shown_from + page_size
end

def page_size
@screen.maxy - 2
end

def row(index = @selected)
index - shown_from + 1
end

def select(index)
render_item
if index > shown_to
render(index - shown_to + shown_from)
elsif index < shown_from
render(index)
end
@selected = index
render_item index, true
end

def item_incr(incr)
index = @selected + incr
index = 0 if index < 0
index = @matches.size - 1 unless index < @matches.size
select index
end

def render_item(index = @selected, standout = false)
Curses.setpos(row(index) , 0)
@screen.clrtoeol
Curses.standout if standout
Curses.addstr @matches[index]
Curses.standend if standout
end

def render(from = 0)
@shown_from = from
(0..page_size).map { |y| render_item y + from }
end

def filter
pattern = str.join
@selected = 0
@matches = @finder.sorted_matches_for pattern
render @selected
render_item @selected, true
end

end

Loading

0 comments on commit bc13885

Please sign in to comment.