Skip to content
This repository
Browse code

Lots of plumbing.

  • Loading branch information...
commit bc138857133a0916e151889c59ee5b1e2e0f260e 1 parent 6458432
Victor Hugo Borja authored
2  Gemfile
... ... @@ -0,0 +1,2 @@
  1 +source :rubygems
  2 +gem 'command-t'
10 Gemfile.lock
... ... @@ -0,0 +1,10 @@
  1 +GEM
  2 + remote: http://rubygems.org/
  3 + specs:
  4 + command-t (1.0.1)
  5 +
  6 +PLATFORMS
  7 + ruby
  8 +
  9 +DEPENDENCIES
  10 + command-t
24 LICENSE
... ... @@ -0,0 +1,24 @@
  1 +Copyright (c) 2011, Victor Hugo Borja <vic.borja@gmail.com>
  2 +All rights reserved.
  3 +
  4 +Redistribution and use in source and binary forms, with or without
  5 +modification, are permitted provided that the following conditions are met:
  6 + * Redistributions of source code must retain the above copyright
  7 + notice, this list of conditions and the following disclaimer.
  8 + * Redistributions in binary form must reproduce the above copyright
  9 + notice, this list of conditions and the following disclaimer in the
  10 + documentation and/or other materials provided with the distribution.
  11 + * Neither the name of the <organization> nor the
  12 + names of its contributors may be used to endorse or promote products
  13 + derived from this software without specific prior written permission.
  14 +
  15 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16 +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18 +DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  19 +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20 +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21 +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22 +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24 +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 README.md
Source Rendered
... ... @@ -1,18 +1,58 @@
1 1 Mr T
2 2 ====
3 3
4   -Textmate-like file finder on a curses interface. Intended for file completion on
5   -shell prompt.
  4 +What is it.
  5 +-----------
6 6
7   -Mr T requires a ruby compiled with curses devel, it also makes use of the ruby
8   -extension provided by (Command-T)[https://wincent.com/products/command-t]
  7 +Mr T is a curses based file finder. If you're familiar with Textmate's cmd-t or
  8 +Vim's Command-T plugin, you'll feel right at home.
9 9
  10 +Mr T allows you to have fast, file completion from your shell prompt. You can
  11 +use it as-is by invoking the _mrT_ command, or use it as your default file
  12 +completion strategy for some unix commands.
10 13
11   -== Basic Usage
12 14
13   - mrT [basedir]
  15 +Requirements
  16 +------------
14 17
15   -== TODO
  18 +Mr T requires a ruby compiled with the standard curses library (you need to have
  19 +curses-devel installed at ruby configuration time), it also makes use of the ruby
  20 +extension provided by [Command-T][http://wincent.com/products/command-t]
  21 +
  22 +
  23 +Installation
  24 +------------
  25 +
  26 +rake install
  27 +
  28 +
  29 +
  30 +Usage
  31 +-----
  32 +
  33 +After installation, you might be able to use the _mrT_ binary, right now it
  34 +takes an optional directory as only argument.
  35 +
  36 + mrT
  37 +
  38 +
  39 +Future
  40 +------
  41 +
  42 +Use Command-T caching features, expose Command-T options as command line flags.
  43 +
  44 +
  45 +Contribute
  46 +----------
  47 +
  48 +Feel free to adapt mrT to your needs, report any issue or send pull requests
  49 +to the github repository:
  50 +
  51 + http://github.com/vic/mrT
  52 +
  53 +Authors
  54 +-------
  55 +
  56 +Victor Hugo Borja <vic.borja@gmail.com>
16 57
17   -Instructions on how to setup as bash file completion mechanism.
18 58
10 Rakefile
... ... @@ -0,0 +1,10 @@
  1 +task :gem do
  2 + sh 'gem build mrT.gemspec'
  3 +end
  4 +
  5 +task :install => :gem do
  6 + v = `git describe --abbrev=0`.chomp
  7 + sh "gem install mrT-#{v}.gem"
  8 +end
  9 +
  10 +task :default => :install
146 bin/mrT
... ... @@ -1,141 +1,15 @@
1 1 #!/usr/bin/env ruby
2 2
3   -# TODO: make command-t a reusable gem!
4   -$LOAD_PATH.unshift File.expand_path('~/.vim/bundle/command-t/ruby')
5   -
6   -require 'curses'
7   -require 'command-t/finder'
8   -
9   -class BashT
10   -
11   - attr_reader :str, :shown_from, :pwd
12   -
13   - def initialize(pwd)
14   - @str = []
15   - @options = {
16   - }
17   - @pwd = pwd
18   - @finder = CommandT::Finder.new @pwd, @options
19   - end
20   -
21   - def run
22   - value = with_curses { interact }
23   - File.expand_path(value, @pwd) if value
24   - end
25   -
26   - def with_curses
27   - Curses.init_screen
28   - Curses.nonl
29   - Curses.cbreak
30   - Curses.noecho
31   -
32   - @screen = Curses.stdscr
33   - @screen.scrollok true
34   - @screen.keypad true
35   -
36   - begin
37   - value = yield
38   - rescue Interrupt
39   - Curses.close_screen
40   - ensure
41   - Curses.close_screen
42   - end
43   - end
44   -
45   - def interact
46   - catch :done do
47   - filter
48   - while true
49   - Curses.setpos(0,0)
50   - @screen.clrtoeol
51   - Curses.addstr('>> ')
52   - Curses.addstr(str.join)
53   - @screen.refresh
54   - c = Curses.getch
55   - case c
56   - when 27 # ESCAPE
57   - throw :done
58   - when Curses::KEY_ENTER, 13
59   - throw :done, @matches[@selected]
60   - when Curses::KEY_RIGHT, 9 # TAB
61   - item_incr(10)
62   - when Curses::KEY_LEFT, 353 # SHIT-TAB
63   - item_incr(-10)
64   - when Curses::KEY_UP, Curses::KEY_CTRL_P
65   - item_incr(-1)
66   - when Curses::KEY_DOWN, Curses::KEY_CTRL_N
67   - item_incr(1)
68   - when Curses::KEY_NPAGE
69   - item_incr(page_size)
70   - when Curses::KEY_PPAGE
71   - item_incr(-page_size)
72   - when Curses::KEY_BACKSPACE
73   - str.pop
74   - filter
75   - when (0..255)
76   - str << c.chr
77   - filter
78   - end
79   - end
80   - end
81   - end
82   -
83   - def shown_to
84   - shown_from + page_size
85   - end
86   -
87   - def page_size
88   - @screen.maxy - 2
89   - end
90   -
91   - def row(index = @selected)
92   - index - shown_from + 1
93   - end
94   -
95   - def select(index)
96   - render_item
97   - if index > shown_to
98   - render(index - shown_to + shown_from)
99   - elsif index < shown_from
100   - render(index)
101   - end
102   - @selected = index
103   - render_item index, true
104   - end
105   -
106   - def item_incr(incr)
107   - index = @selected + incr
108   - index = 0 if index < 0
109   - index = @matches.size - 1 unless index < @matches.size
110   - select index
111   - end
112   -
113   - def render_item(index = @selected, standout = false)
114   - Curses.setpos(row(index) , 0)
115   - @screen.clrtoeol
116   - Curses.standout if standout
117   - Curses.addstr @matches[index]
118   - Curses.standend if standout
119   - end
120   -
121   - def render(from = 0)
122   - @shown_from = from
123   - (0..page_size).map { |y| render_item y + from }
124   - end
  3 +if __FILE__ == $0
  4 + $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
  5 + require 'rubygems'
  6 +end
125 7
126   - def filter
127   - pattern = str.join
128   - @selected = 0
129   - @matches = @finder.sorted_matches_for pattern
130   - render @selected
131   - render_item @selected, true
132   - end
  8 +gem 'command-t'
133 9
134   -end
  10 +require 'mrT'
135 11
136   -if $0 == __FILE__
137   - dir = Dir.pwd
138   - dir = ARGV.first if ARGV.first && File.directory?(ARGV.first)
139   - res = BashT.new(dir).run
140   - puts res if res
141   -end
  12 +dir = Dir.pwd
  13 +dir = ARGV.first if ARGV.first && File.directory?(ARGV.first)
  14 +res = MrT.new(dir).run
  15 +puts res if res
130 lib/mrT.rb
... ... @@ -0,0 +1,130 @@
  1 +require 'curses'
  2 +require 'command-t/finder'
  3 +
  4 +class MrT
  5 +
  6 + attr_reader :str, :shown_from, :pwd
  7 +
  8 + def initialize(pwd)
  9 + @str = []
  10 + @options = {
  11 + }
  12 + @pwd = pwd
  13 + @finder = CommandT::Finder.new @pwd, @options
  14 + end
  15 +
  16 + def run
  17 + value = with_curses { interact }
  18 + File.expand_path(value, @pwd) if value
  19 + end
  20 +
  21 + def with_curses
  22 + Curses.init_screen
  23 + Curses.nonl
  24 + Curses.cbreak
  25 + Curses.noecho
  26 +
  27 + @screen = Curses.stdscr
  28 + @screen.scrollok true
  29 + @screen.keypad true
  30 +
  31 + begin
  32 + value = yield
  33 + rescue Interrupt
  34 + Curses.close_screen
  35 + ensure
  36 + Curses.close_screen
  37 + end
  38 + end
  39 +
  40 + def interact
  41 + catch :done do
  42 + filter
  43 + while true
  44 + Curses.setpos(0,0)
  45 + @screen.clrtoeol
  46 + Curses.addstr('>> ')
  47 + Curses.addstr(str.join)
  48 + @screen.refresh
  49 + c = Curses.getch
  50 + case c
  51 + when 27 # ESCAPE
  52 + throw :done
  53 + when Curses::KEY_ENTER, 13
  54 + throw :done, @matches[@selected]
  55 + when Curses::KEY_RIGHT, 9 # TAB
  56 + item_incr(10)
  57 + when Curses::KEY_LEFT, 353 # SHIT-TAB
  58 + item_incr(-10)
  59 + when Curses::KEY_UP, Curses::KEY_CTRL_P
  60 + item_incr(-1)
  61 + when Curses::KEY_DOWN, Curses::KEY_CTRL_N
  62 + item_incr(1)
  63 + when Curses::KEY_NPAGE
  64 + item_incr(page_size)
  65 + when Curses::KEY_PPAGE
  66 + item_incr(-page_size)
  67 + when Curses::KEY_BACKSPACE
  68 + str.pop
  69 + filter
  70 + when (0..255)
  71 + str << c.chr
  72 + filter
  73 + end
  74 + end
  75 + end
  76 + end
  77 +
  78 + def shown_to
  79 + shown_from + page_size
  80 + end
  81 +
  82 + def page_size
  83 + @screen.maxy - 2
  84 + end
  85 +
  86 + def row(index = @selected)
  87 + index - shown_from + 1
  88 + end
  89 +
  90 + def select(index)
  91 + render_item
  92 + if index > shown_to
  93 + render(index - shown_to + shown_from)
  94 + elsif index < shown_from
  95 + render(index)
  96 + end
  97 + @selected = index
  98 + render_item index, true
  99 + end
  100 +
  101 + def item_incr(incr)
  102 + index = @selected + incr
  103 + index = 0 if index < 0
  104 + index = @matches.size - 1 unless index < @matches.size
  105 + select index
  106 + end
  107 +
  108 + def render_item(index = @selected, standout = false)
  109 + Curses.setpos(row(index) , 0)
  110 + @screen.clrtoeol
  111 + Curses.standout if standout
  112 + Curses.addstr @matches[index]
  113 + Curses.standend if standout
  114 + end
  115 +
  116 + def render(from = 0)
  117 + @shown_from = from
  118 + (0..page_size).map { |y| render_item y + from }
  119 + end
  120 +
  121 + def filter
  122 + pattern = str.join
  123 + @selected = 0
  124 + @matches = @finder.sorted_matches_for pattern
  125 + render @selected
  126 + render_item @selected, true
  127 + end
  128 +
  129 +end
  130 +
35 mrT.gemspec
... ... @@ -0,0 +1,35 @@
  1 +Gem::Specification.new do |s|
  2 + s.name = "mrT"
  3 + v = `git describe --abbrev=0`.chomp
  4 + s.version = v
  5 +
  6 + s.authors = ["Victor Hugo Borja"]
  7 + s.date = "2011-03-17"
  8 + s.email = "vic.borja@gmail.com"
  9 +
  10 + files =
  11 + ["README.md", "LICENSE", "Gemfile", "Rakefile"] +
  12 + Dir.glob("{lib,bin}/**/*")
  13 +
  14 + files = files.reject { |f| f =~ /\.(rbc|o|log|plist|dSYM)/ }
  15 +
  16 + s.files = files
  17 + s.require_path = "lib"
  18 +
  19 + s.executables = ["mrT"]
  20 +
  21 + s.has_rdoc = false
  22 + s.homepage = "http://github.com/vic/mrT"
  23 +
  24 + s.summary = "A curses based file finder using Command-T algorithm."
  25 +
  26 + s.description = <<EOS
  27 + MrT is a tiny curses application that allows you to easily find a file
  28 + from your shell prompt. It uses the ruby binding from Command-T vim plugin[0].
  29 +
  30 + You can use MrT standalone or as your default file completion function in bash.
  31 +
  32 + [0] https://wincent.com/products/command-t
  33 +EOS
  34 +
  35 +end

0 comments on commit bc13885

Please sign in to comment.
Something went wrong with that request. Please try again.