Skip to content
Myr

Merge pull request #17 from junegunn/mouse

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add mouse support

authored by Junegunn Choi · committed by Junegunn Choi · parents c2943e7, c7e86ad · Browse files · 4 files+134 added−44 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+134 added−44 removed

  • tests:+6 added−2 removed
  • other:+128 added−42 removed

4 files+134 added−44 removed

modified:test/test_fzf.rb+6 added-2 removed
18 unmodified lines
19 assert_equal 1000, fzf.sort20 assert_equal false, fzf.multi21 assert_equal true, fzf.color22 assert_equal nil, fzf.rxflag23 assert_equal true, fzf.mouse24 end2526 def test_environment_variables27 # Deprecated28 ENV['FZF_DEFAULT_SORT'] = '20000'29 fzf = FZF.new []30 assert_equal 20000, fzf.sort3131 ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c'32 ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c --no-mouse'33 fzf = FZF.new []34 assert_equal 10000, fzf.sort35 assert_equal ' hello world ',36 fzf.query.get37 assert_equal true, fzf.extended38 assert_equal true, fzf.multi39 assert_equal false, fzf.color40 assert_equal false, fzf.mouse41 end4243 def test_option_parser44 # Long opts43 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --extended]45 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --extended --no-mouse]46 assert_equal 2000, fzf.sort47 assert_equal true, fzf.multi48 assert_equal false, fzf.color49 assert_equal false, fzf.mouse50 assert_equal 0, fzf.rxflag51 assert_equal 'hello', fzf.query.get52 assert_equal true, fzf.extended53
1 unmodified line
55 --no-sort -i --color --no-multi]56 assert_equal nil, fzf.sort57 assert_equal false, fzf.multi58 assert_equal true, fzf.color59 assert_equal true, fzf.mouse60 assert_equal 1, fzf.rxflag61 assert_equal 'hello', fzf.query.get62 assert_equal false, fzf.extended63
366 unmodified lines
modified:fzf+123 added-41 removed
5 unmodified lines
6# / /_/_ / / /_7# / __/ / /_/ __/8# /_/ /___/_/ Fuzzy finder for your shell9#10# Version: 0.6.2-devel (January 22, 2014)10# Version: 0.7.0 (January 30, 2014)11#12# Author: Junegunn Choi13# URL: https://github.com/junegunn/fzf14# License: MIT
24 unmodified lines
39require 'thread'40require 'curses'41require 'set'4243unless String.method_defined? :force_encoding44 class String45 def force_encoding *arg46 self47 end48 end49end5051class FZF52 C = Curses45 attr_reader :rxflag, :sort, :color, :multi, :query, :extended53 attr_reader :rxflag, :sort, :color, :mouse, :multi, :query, :extended5455 class AtomicVar56 def initialize value57 @value = value
22 unmodified lines
80 @sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i81 @color = true82 @multi = false83 @extended = false84 @mouse = true8586 argv =87 if opts = ENV['FZF_DEFAULT_OPTS']88 require 'shellwords'
12 unmodified lines
101 when '-i' then @rxflag = Regexp::IGNORECASE102 when '+i' then @rxflag = 0103 when '-c', '--color' then @color = true104 when '+c', '--no-color' then @color = false105 when '--no-mouse' then @mouse = false106 when '+s', '--no-sort' then @sort = nil107 when '-q', '--query'108 usage 1, 'query string required' unless query = argv.shift109 @query = AtomicVar.new query.dup
9 unmodified lines
119 usage 1, "illegal option: #{o}"120 end121 end122113 @source = source123 @source = source.clone124 @mtx = Mutex.new125 @cv = ConditionVariable.new126 @events = {}127 @new = []
44 unmodified lines
172 +s, --no-sort Do not sort the result. Keep the sequence unchanged.173 -i Case-insensitive match (default: smart-case match)174 +i Case-sensitive match175 +c, --no-color Disable colors176 --no-mouse Disable mouse177178 Environment variables179 FZF_DEFAULT_COMMAND Default command to use when input is tty180 FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
292 unmodified lines
473 end474475 def init_screen476 C.init_screen477 if @mouse478 C.mouseinterval 0479 C.mousemask C::ALL_MOUSE_EVENTS480 end481 C.stdscr.keypad(true)482 C.start_color483 dbg =484 if C.respond_to?(:use_default_colors)485 C.use_default_colors
1 unmodified line
487 else488 C::COLOR_BLACK489 end490 C.raw491 C.nonl492 C.noecho493494 if @color495 if C.can_change_color? && ENV['TERM'].to_s =~ /256/
47 unmodified lines
543 else544 exit 1545 end546 else547 $stdin.reopen IO.open(IO.sysopen('/dev/tty'), 'r')548 @source549 end550551 Thread.new do
130 unmodified lines
682683 def start_renderer684 Thread.new do685 begin686 refresh687 while blk = @queue.shift688 blk.call689 refresh690 end
7 unmodified lines
698 @queue.push blk699 nil700 end701702 def vselect &prc703 @vcursor.set { |v| @vcursors << v; prc.call v }704 update_list false705 end706707 def num_unicode_bytes chr708 # http://en.wikipedia.org/wiki/UTF-8709 if chr & 0b10000000 > 0710 bytes = 0711 7.downto(2) do |shift|712 break if (chr >> shift) & 0x1 == 0713 bytes += 1714 end715 bytes716 else717 1718 end719 end720721 def test_mouse st, *states722 states.any? { |s| s & st > 0 }723 end724725 def start_loop726 got = nil727 begin686 tty = IO.open(IO.sysopen('/dev/tty'), 'r')728 input = @query.get.dup729 cursor = input.length730 backword = proc {731 cursor = (input[0, cursor].rindex(/\s\S/) || -1) + 1
7 unmodified lines
739 },740 ctrl(:u) => proc { input = input[cursor..-1]; cursor = 0 },741 ctrl(:a) => proc { cursor = 0; nil },742 ctrl(:e) => proc { cursor = input.length; nil },702 ctrl(:j) => proc { @vcursor.set { |v| @vcursors << v; v - 1 }; update_list false },703 ctrl(:k) => proc { @vcursor.set { |v| @vcursors << v; v + 1 }; update_list false },743 ctrl(:j) => proc { vselect { |v| v - 1 } },744 ctrl(:k) => proc { vselect { |v| v + 1 } },745 ctrl(:w) => proc {746 pcursor = cursor747 backword.call748 input = input[0...cursor] + input[pcursor..-1]749 },709 127 => proc { input[cursor -= 1] = '' if cursor > 0 },710 9 => proc { |o|750 ctrl(:h) => proc { input[cursor -= 1] = '' if cursor > 0 },751 ctrl(:i) => proc { |o|752 if @multi && sel = pick753 if @selects.has_key? sel754 @selects.delete sel755 else756 @selects[sel] = 1757 end717 @vcursor.set { |v|718 @vcursors << v719 v + (o == :stab ? 1 : -1)758 vselect { |v|759 v + case o760 when :select then 0761 when C::KEY_BTAB then 1762 else -1763 end764 }721 update_list false765 end766 },724 :left => proc { cursor = [0, cursor - 1].max; nil },725 :right => proc { cursor = [input.length, cursor + 1].min; nil },767 ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },768 ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil },769 :alt_b => proc { backword.call; nil },770 :alt_f => proc {771 cursor += (input[cursor..-1].index(/(\S\s)|(.$)/) || -1) + 1772 nil773 },774 }732 actions[ctrl(:b)] = actions[:left]733 actions[ctrl(:f)] = actions[:right]734 actions[ctrl(:h)] = actions[127]735 actions[ctrl(:n)] = actions[ctrl(:j)]736 actions[ctrl(:p)] = actions[ctrl(:k)]775 actions[C::KEY_UP] = actions[ctrl(:p)] = actions[ctrl(:k)]776 actions[C::KEY_DOWN] = actions[ctrl(:n)] = actions[ctrl(:j)]777 actions[C::KEY_LEFT] = actions[ctrl(:b)]778 actions[C::KEY_RIGHT] = actions[ctrl(:f)]779 actions[C::KEY_BTAB] = actions[:select] = actions[ctrl(:i)]780 actions[C::KEY_BACKSPACE] = actions[127] = actions[ctrl(:h)]781 actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]738 actions[:stab] = actions[9]782783 emit(:key) { [@query.get, cursor] } unless @query.empty?784 pmv = nil785 while true786 @cursor_x.set cursor787 render { print_input }788745 ord = tty.getc.ord746 ord =747 case ord = (tty.read_nonblock(1).ord rescue :esc)748 when 91749 case (tty.read_nonblock(1).ord rescue nil)750 when 68 then :left751 when 67 then :right752 when 66 then ctrl(:j)753 when 65 then ctrl(:k)754 when 90 then :stab755 else next789 C.stdscr.timeout = -1790 ch = C.getch791792 case ch793 when C::KEY_MOUSE794 if m = C.getmouse795 st = m.bstate796 if test_mouse(st, C::BUTTON1_PRESSED, C::BUTTON1_RELEASED)797 if m.y == cursor_y798 # TODO Wide-characters799 cursor = [0, [input.length, m.x - 2].min].max800 elsif m.x > 1 && m.y <= max_items801 vselect { |v|802 tv = max_items - m.y - 1803 if test_mouse(st, C::BUTTON1_RELEASED)804 if test_mouse(st, C::BUTTON_SHIFT)805 ch = :select806 elsif pmv == tv807 ch = ctrl(:m)808 end809 pmv = tv810 end811 tv812 }813 end814 elsif test_mouse(st, 0x8000000, C::BUTTON2_PRESSED)815 ch = C::KEY_DOWN816 elsif test_mouse(st, C::BUTTON4_PRESSED)817 ch = C::KEY_UP818 end819 end820 when 27821 C.stdscr.timeout = 0822 ch =823 case ch2 = C.getch757 when 'b'.ord then :alt_b758 when 'f'.ord then :alt_f759 when :esc then :esc760 else next761 end if ord == 27824 when 'b' then :alt_b825 when 'f' then :alt_f826 when nil then :esc827 else828 ch2829 end830 end831763 upd = actions.fetch(ord, proc { |ord|764 char = [ord].pack('U*')765 if char =~ /[[:print:]]/766 input.insert cursor, char832 upd = actions.fetch(ch, proc { |ch|833 if ch.is_a?(Fixnum)834 # Ruby 1.8835 if (ch.chr rescue '') =~ /[[:print:]]/836 ch = ch.chr837 elsif (nch = num_unicode_bytes(ch)) > 1838 chs = [ch]839 (nch - 1).times do |i|840 chs << C.getch841 end842 # UTF-8 TODO Ruby 1.8843 ch = chs.pack('C*').force_encoding('UTF-8')844 end845 end846847 if ch.is_a?(String) && ch =~ /[[:print:]]/848 input.insert cursor, ch849 cursor += 1850 end769 }).call(ord)851 }).call(ch)852853 # Dispatch key event854 emit(:key) { [@query.set(input.dup), cursor] } if upd855 end
137 unmodified lines
modified:fzf.gemspec+1 added-1 removed
1# coding: utf-82Gem::Specification.new do |spec|3 spec.name = 'fzf'4 spec.version = '0.6.2'4 spec.version = '0.7.0'5 spec.authors = ['Junegunn Choi']6 spec.email = ['junegunn.c@gmail.com']7 spec.description = %q{Fuzzy finder for your shell}8 spec.summary = %q{Fuzzy finder for your shell}
9 unmodified lines
modified:README.md+4 added
58 unmodified lines
59 +s, --no-sort Do not sort the result. Keep the sequence unchanged.60 -i Case-insensitive match (default: smart-case match)61 +i Case-sensitive match62 +c, --no-color Disable colors63 --no-mouse Disable mouse6465 Environment variables66 FZF_DEFAULT_COMMAND Default command to use when input is tty67 FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")
35 unmodified lines
103104If you enable multi-select mode with `-m` option, you can select multiple items105with TAB or Shift-TAB key.106107You can also use mouse. Click on an item to select it or shift-click to select108multiple items. Use mouse wheel to move the cursor up and down.109110### Extended-search mode111112With `-x` or `--extended` option, fzf will start in "extended-search mode".113
229 unmodified lines