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
Provenance
- Commit
dcb4694- Tree
0507ea3- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
2fb8ae0
3 files changed+124 added−11 removed
modified:test/test_fzf.rb+22 added-3 removed
5 unmodified lines6 ENV['FZF_EXECUTABLE'] = '0'7 load 'fzf'8 9 class TestFZF < MiniTest::Unit::TestCase10-10 def setup11 ENV.delete 'FZF_DEFAULT_SORT'12 ENV.delete 'FZF_DEFAULT_OPTS'13 ENV.delete 'FZF_DEFAULT_COMMAND'4 unmodified lines18 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 end24 25 def test_environment_variables26 # Deprecated27 ENV['FZF_DEFAULT_SORT'] = '20000'28 fzf = FZF.new []29 assert_equal 20000, fzf.sort30 31- 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.get1 unmodified line37 fzf.filter38 assert_equal :fuzzy, fzf.extended39 assert_equal true, fzf.multi40 assert_equal false, fzf.color41+ assert_equal false, fzf.mouse42 end43 44 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.extended3 unmodified lines59 --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.extended382 unmodified lines450 line, offsets = fzf.convert_item item451 tokens = fzf.format line, 80, offsets452 assert_equal [], tokens453 end454+455+ 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+ end470 end471 modified:fzf+98 added-8 removed
48 unmodified lines49 end50 51 class FZF52 C = Curses53- attr_reader :rxflag, :sort, :color, :multi, :query, :filter, :extended53+ attr_reader :rxflag, :sort, :color, :mouse, :multi, :query, :filter, :extended54 55 class AtomicVar56 def initialize value57 @value = value19 unmodified lines77 @rxflag = nil78 @sort = ENV.fetch('FZF_DEFAULT_SORT', 1000).to_i79 @color = true80 @multi = false81+ @mouse = true82 @extended = nil83 @filter = nil84 85 argv =14 unmodified lines100 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.dup96 unmodified lines205 +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 mouse210 211 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 lines508 end509 510 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_colors230 unmodified lines747 1748 end749 end750 751+ def read_nb chars = 1, default = nil752+ @tty.read_nonblock(chars).ord rescue default753+ end754+755+ 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+ end773+774 def get_input actions775 @tty ||= IO.open(IO.sysopen('/dev/tty'), 'r')776 777 if pending = @pending24 unmodified lines802 end803 end804 805 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 == 27822 823+ return ord if ord.nil? || ord.is_a?(Hash)824+825 if actions.has_key?(ord)826 if str.empty?827 return ord828 else8 unmodified lines837 end838 end839 end840 841+ class MouseEvent842+ DOUBLE_CLICK_INTERVAL = 0.5843+844+ attr_reader :v845+846+ def initialize v = nil847+ @c = 0848+ @v = v849+ @t = Time.at 0850+ end851+852+ def v= v853+ @c = (@v == v && within?) ? @c + 1 : 0854+ @v = v855+ @t = Time.now856+ end857+858+ def double? v859+ @c == 1 && @v == v && within?860+ end861+862+ def within?863+ (Time.now - @t) < DOUBLE_CLICK_INTERVAL864+ end865+ end866+867 def start_loop868 got = nil869 begin870 input = @query.get.dup25 unmodified lines896 @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 lines919 actions[127] = actions[ctrl(:h)]920 actions[ctrl(:q)] = actions[ctrl(:g)] = actions[ctrl(:c)] = actions[:esc]921 922 emit(:key) { [@query.get, cursor] } unless @query.empty?923+ mouse = MouseEvent.new924 while true925 @cursor_x.set cursor926 render { print_input }927 928 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 - 1943+944+ 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)962 963 # Dispatch key event964 emit(:key) { [@query.set(input.dup), cursor] } if upd147 unmodified linesmodified:README.md+4 added
56 unmodified lines57 +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 mouse62 63 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 lines101 102 If you enable multi-select mode with `-m` option, you can select multiple items103 with TAB or Shift-TAB key.104 105+You can also use mouse. Double-click on an item to select it or shift-click to106+select multiple items. Use mouse wheel to move the cursor up and down.107+108 ### Extended-search mode109 110 With `-x` or `--extended` option, fzf will start in "extended-search mode".111 232 unmodified lines