Skip to content
Myr

Reimplement mouse input without using Curses.getch

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

authored by Junegunn Choi · committed by Junegunn Choi · parent 2fb8ae0 · Browse files · 3 files+124 added−11 removed
This commit

State

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

Diff stats

3 files+124 added−11 removed

  • tests:+22 added−3 removed
  • other:+102 added−8 removed

3 files+124 added−11 removed

modified:test/test_fzf.rb+22 added-3 removed
5 unmodified lines
6ENV['FZF_EXECUTABLE'] = '0'7load 'fzf'89class TestFZF < MiniTest::Unit::TestCase1010 def setup11 ENV.delete 'FZF_DEFAULT_SORT'12 ENV.delete 'FZF_DEFAULT_OPTS'13 ENV.delete 'FZF_DEFAULT_COMMAND'
4 unmodified lines
18 assert_equal 1000, fzf.sort19 assert_equal false, fzf.multi20 assert_equal true, fzf.color21 assert_equal nil, fzf.rxflag22 assert_equal true, fzf.mouse23 end2425 def test_environment_variables26 # Deprecated27 ENV['FZF_DEFAULT_SORT'] = '20000'28 fzf = FZF.new []29 assert_equal 20000, fzf.sort3031 ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c -f "goodbye world"'31 ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c --no-mouse -f "goodbye world"'32 fzf = FZF.new []33 assert_equal 10000, fzf.sort34 assert_equal ' hello world ',35 fzf.query.get
1 unmodified line
37 fzf.filter38 assert_equal :fuzzy, fzf.extended39 assert_equal true, fzf.multi40 assert_equal false, fzf.color41 assert_equal false, fzf.mouse42 end4344 def test_option_parser45 # Long opts46 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello46 --filter=howdy --extended-exact]47 --filter=howdy --extended-exact --no-mouse]48 assert_equal 2000, fzf.sort49 assert_equal true, fzf.multi50 assert_equal false, fzf.color51 assert_equal false, fzf.mouse52 assert_equal 0, fzf.rxflag53 assert_equal 'hello', fzf.query.get54 assert_equal 'howdy', fzf.filter55 assert_equal :exact, fzf.extended
3 unmodified lines
59 --no-sort -i --color --no-multi]60 assert_equal nil, fzf.sort61 assert_equal false, fzf.multi62 assert_equal true, fzf.color63 assert_equal true, fzf.mouse64 assert_equal 1, fzf.rxflag65 assert_equal 'b', fzf.filter66 assert_equal 'hello', fzf.query.get67 assert_equal nil, fzf.extended
382 unmodified lines
450 line, offsets = fzf.convert_item item451 tokens = fzf.format line, 80, offsets452 assert_equal [], tokens453 end454455 def test_mouse_event456 interval = FZF::MouseEvent::DOUBLE_CLICK_INTERVAL457 me = FZF::MouseEvent.new nil458 me.v = 10459 assert_equal false, me.double?(10)460 assert_equal false, me.double?(20)461 me.v = 20462 assert_equal false, me.double?(10)463 assert_equal false, me.double?(20)464 me.v = 20465 assert_equal false, me.double?(10)466 assert_equal true, me.double?(20)467 sleep interval468 assert_equal false, me.double?(20)469 end470end471
modified:fzf+98 added-8 removed
48 unmodified lines
49end5051class FZF52 C = Curses53 attr_reader :rxflag, :sort, :color, :multi, :query, :filter, :extended53 attr_reader :rxflag, :sort, :color, :mouse, :multi, :query, :filter, :extended5455 class AtomicVar56 def initialize value57 @value = value
19 unmodified lines
77 @rxflag = nil78 @sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i79 @color = true80 @multi = false81 @mouse = true82 @extended = nil83 @filter = nil8485 argv =
14 unmodified lines
100 when '-i' then @rxflag = Regexp::IGNORECASE101 when '+i' then @rxflag = 0102 when '-c', '--color' then @color = true103 when '+c', '--no-color' then @color = false104 when '--no-mouse' then @mouse = false105 when '+s', '--no-sort' then @sort = nil106 when '-q', '--query'107 usage 1, 'query string required' unless query = argv.shift108 @query = AtomicVar.new query.dup
96 unmodified lines
205 +s, --no-sort Do not sort the result. Keep the sequence unchanged.206 -i Case-insensitive match (default: smart-case match)207 +i Case-sensitive match208 +c, --no-color Disable colors209 --no-mouse Disable mouse210211 Environment variables212 FZF_DEFAULT_COMMAND Default command to use when input is tty213 FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")] + $/ + $/
294 unmodified lines
508 end509510 def init_screen511 C.init_screen512 C.mousemask C::ALL_MOUSE_EVENTS if @mouse513 C.start_color514 dbg =515 if C.respond_to?(:use_default_colors)516 C.use_default_colors
230 unmodified lines
747 1748 end749 end750751 def read_nb chars = 1, default = nil752 @tty.read_nonblock(chars).ord rescue default753 end754755 def get_mouse756 case ord = read_nb757 when 32, 36, # mouse-down / shift-mouse-down758 35, 39 # mouse-up / shift-mouse-up759 x = read_nb - 33760 y = read_nb - 33761 { :event => (ord % 2 == 0 ? :click : :release),762 :x => x, :y => y, :shift => ord >= 36 }763 when 96, 100, # scroll-up / shift-scroll-up764 97, 101 # scroll-down / shift-scroll-down765 read_nb(2)766 { :event => :scroll, :diff => (ord % 2 == 0 ? -1 : 1), :shift => ord >= 100 }767 else768 # e.g. 40, 43, 104, 105769 read_nb(2)770 nil771 end772 end773774 def get_input actions775 @tty ||= IO.open(IO.sysopen('/dev/tty'), 'r')776777 if pending = @pending
24 unmodified lines
802 end803 end804805 ord =779 case ord = (@tty.read_nonblock(1).ord rescue :esc)806 case ord = read_nb(1, :esc)807 when 91781 case (@tty.read_nonblock(1).ord rescue nil)808 case read_nb(1, nil)809 when 68 then ctrl(:b)810 when 67 then ctrl(:f)811 when 66 then ctrl(:j)812 when 65 then ctrl(:k)813 when 90 then :stab787 else next814 when 77815 get_mouse816 end817 when 'b', 98 then :alt_b818 when 'f', 102 then :alt_f819 when :esc then :esc820 else next821 end if ord == 27822823 return ord if ord.nil? || ord.is_a?(Hash)824825 if actions.has_key?(ord)826 if str.empty?827 return ord828 else
8 unmodified lines
837 end838 end839 end840841 class MouseEvent842 DOUBLE_CLICK_INTERVAL = 0.5843844 attr_reader :v845846 def initialize v = nil847 @c = 0848 @v = v849 @t = Time.at 0850 end851852 def v= v853 @c = (@v == v && within?) ? @c + 1 : 0854 @v = v855 @t = Time.now856 end857858 def double? v859 @c == 1 && @v == v && within?860 end861862 def within?863 (Time.now - @t) < DOUBLE_CLICK_INTERVAL864 end865 end866867 def start_loop868 got = nil869 begin870 input = @query.get.dup
25 unmodified lines
896 @selects.delete sel897 else898 @selects[sel] = 1899 end844 vselect { |v| v + (o == :stab ? 1 : -1) }900 vselect { |v| v + case o901 when :stab then 1902 when :sclick then 0903 else -1904 end }905 end906 },907 ctrl(:b) => proc { cursor = [0, cursor - 1].max; nil },908 ctrl(:f) => proc { cursor = [input.length, cursor + 1].min; nil },
10 unmodified lines
919 actions[127] = actions[ctrl(:h)]920 actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]921922 emit(:key) { [@query.get, cursor] } unless @query.empty?923 mouse = MouseEvent.new924 while true925 @cursor_x.set cursor926 render { print_input }927928 if key = get_input(actions)868 upd = actions.fetch(key, proc { |str|869 input.insert cursor, str870 cursor += str.length929 upd = actions.fetch(key, proc { |val|930 case val931 when String932 input.insert cursor, val933 cursor += val.length934 when Hash935 event = val[:event]936 case event937 when :click, :release938 x, y, shift = val.values_at :x, :y, :shift939 if y == cursor_y940 cursor = [0, [input.length, x - 2].min].max941 elsif x > 1 && y <= max_items942 tv = max_items - y - 1943944 case event945 when :click946 vselect { |_| tv }947 actions[ctrl(:i)].call(:sclick) if shift948 mouse.v = tv949 when :release950 if !shift && mouse.double?(tv)951 actions[ctrl(:m)].call952 end953 end954 end955 when :scroll956 diff, shift = val.values_at :diff, :shift957 actions[ctrl(:i)].call(:sclick) if shift958 actions[ctrl(diff > 0 ? :j : :k)].call959 end960 end961 }).call(key)962963 # Dispatch key event964 emit(:key) { [@query.set(input.dup), cursor] } if upd
147 unmodified lines
modified:README.md+4 added
56 unmodified lines
57 +s, --no-sort Do not sort the result. Keep the sequence unchanged.58 -i Case-insensitive match (default: smart-case match)59 +i Case-sensitive match60 +c, --no-color Disable colors61 --no-mouse Disable mouse6263 Environment variables64 FZF_DEFAULT_COMMAND Default command to use when input is tty65 FZF_DEFAULT_OPTS Defaults options. (e.g. "-x -m --sort 10000")
35 unmodified lines
101102If you enable multi-select mode with `-m` option, you can select multiple items103with TAB or Shift-TAB key.104105You can also use mouse. Double-click on an item to select it or shift-click to106select multiple items. Use mouse wheel to move the cursor up and down.107108### Extended-search mode109110With `-x` or `--extended` option, fzf will start in "extended-search mode".111
232 unmodified lines