# vim: set filetype=ruby isk=@,48-57,_,192-255:
#!/usr/bin/env bash
#
#     ____      ____
#    / __/___  / __/
#   / /_/_  / / /_
#  / __/ / /_/ __/
# /_/   /___/_/    Fuzzy finder for your shell
#
# URL:         https://github.com/junegunn/fzf
# Author:      Junegunn Choi
# License:     MIT
# Last update: October 29, 2013
#
# Copyright (c) 2013 Junegunn Choi
#
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

exec /usr/bin/env ruby -x "$0" $* 3>&1 1>&2 2>&3
#!ruby
# encoding: utf-8

def usage x
  puts %[usage: fzf [options]

  -s, --sort=MAX   Maximum number of matched items to sort. Default: 500.
  +s, --no-sort    Do not sort the result. Keep the sequence unchanged.
  +i               Case-sensitive match]
  exit x
end

usage 0 unless (%w[--help -h] & ARGV).empty?
@rxflag = ARGV.delete('+i') ? 0 : Regexp::IGNORECASE
@sort   = (ARGV.delete('+s') || ARGV.delete('--no-sort')) ? nil : 500
rest    = ARGV.join ' '
if sort = rest.match(/(-s|--sort=?) ?([0-9]+)/)
  usage 1 unless @sort
  @sort = sort[2].to_i
  rest  = rest.delete sort[0]
end
usage 1 unless rest.empty?

require 'thread'
require 'curses'

@mtx      = Mutex.new
@smtx     = Mutex.new
@cv       = ConditionVariable.new
@lists    = []
@new      = []
@query    = ''
@matches  = []
@count    = 0
@cursor_x = 0
@vcursor  = 0
@events   = {}

case RUBY_PLATFORM
when /darwin/
  module UConv
    CHOSUNG   = 0x1100
    JUNGSUNG  = 0x1161
    JONGSUNG  = 0x11A7
    CHOSUNGS  = 19
    JUNGSUNGS = 21
    JONGSUNGS = 28
    JJCOUNT   = JUNGSUNGS * JONGSUNGS
    NFC_BEGIN = 0xAC00
    NFC_END   = NFC_BEGIN + CHOSUNGS * JUNGSUNGS * JONGSUNGS

    def self.nfd str
      ret = ''
      str.split(//).each do |c|
        cp = c.ord
        if cp >= NFC_BEGIN && cp < NFC_END
          idx  = cp - NFC_BEGIN
          cho  = CHOSUNG  + idx / JJCOUNT
          jung = JUNGSUNG + (idx % JJCOUNT) / JONGSUNGS
          jong = JONGSUNG + idx % JONGSUNGS
          ret << cho << jung
          ret << jong if jong != JONGSUNG
        else
          ret << c
        end
      end
      ret
    end

    def self.nfc str, offset
      ret = ''
      omap = []
      pend = []
      str.split(//).each_with_index do |c, idx|
        cp = c.ord
        omap << ret.length
        unless pend.empty?
          if cp >= JUNGSUNG && cp < JUNGSUNG + JUNGSUNGS
            pend << cp - JUNGSUNG
            next
          elsif cp >= JONGSUNG && cp < JONGSUNG + JONGSUNGS
            pend << cp - JONGSUNG
            next
          else
            omap[-1] = omap[-1] + 1
            ret << [NFC_BEGIN + pend[0] * JJCOUNT +
                                (pend[1] || 0) * JONGSUNGS +
                                (pend[2] || 0)].pack('U*')
            pend.clear
          end
        end
        if cp >= CHOSUNG && cp < CHOSUNG + CHOSUNGS
          pend << cp - CHOSUNG
        else
          ret << c
        end
      end
      return [ret, offset.map { |o| omap[o] || (omap.last + 1) }]
    end
  end

  def convert_query q
    UConv.nfd(q).split(//)
  end

  def convert_item item
    UConv.nfc(*item)
  end
else
  def convert_query q
    q.split(//)
  end

  def convert_item item
    item
  end
end

def emit event
  @mtx.synchronize do
    @events[event] = yield
    @cv.broadcast
  end
end

C = Curses
def max_items; C.lines - 2; end
def cursor_y;  C.lines - 1; end
def cprint str, col
  C.attron(col) do
    C.addstr str
  end if str
end

def print_input
  C.setpos cursor_y, 0
  C.clrtoeol
  cprint '> ', color(:blue, true)
  cprint @query, color(:normal, true)
end

def print_info progress = true, msg = nil
  @fan ||= '-\|/-\|/'.split(//)
  C.setpos cursor_y - 1, 0
  C.clrtoeol
  prefix =
    if fan = @fan.shift
      @fan.push fan
      cprint fan, color(:fan, true)
      ' '
    else
      '  '
    end
  C.attron color(:info, false) do
    progress &&= "#{prefix}#{@matches.length}/#{@count}"
    C.addstr progress if progress
    C.addstr msg if msg
  end
end

def refresh
  C.setpos cursor_y, 2 + ulen(@query[0, @cursor_x])
  C.refresh
end

def ctrl char
  char.to_s.ord - 'a'.ord + 1
end

if RUBY_VERSION.split('.').map { |e| e.rjust(3, '0') }.join > '001009'
  def ulen str
    @urx ||= Regexp.new '\p{Han}|\p{Katakana}|\p{Hiragana}|\p{Hangul}'
    str.gsub(@urx, '  ').length
  end
else
  def ulen str
    str.length
  end

  class String
    def ord
      self.unpack('c').first
    end
  end

  class Fixnum
    def ord
      self
    end
  end
end

C.init_screen
C.start_color
dfg, dbg =
  if C.respond_to?(:use_default_colors)
    C.use_default_colors
    [-1, -1]
  else
    [C::COLOR_WHITE, C::COLOR_BLACK]
  end
C.raw
C.noecho
if C.can_change_color?
  fg = ENV.fetch('FZF_FG', 252).to_i
  bg = ENV.fetch('FZF_BG', 236).to_i
  C.init_pair 1, 110,    dbg
  C.init_pair 2, dfg,    dbg
  C.init_pair 3, 108,    dbg
  C.init_pair 4, fg + 2, bg
  C.init_pair 5, 151,    bg
  C.init_pair 6, 148,    dbg
  C.init_pair 7, 144,    dbg
  C.init_pair 8, 161,    bg
else
  C.init_pair 1, C::COLOR_BLUE,   dbg
  C.init_pair 2, C::COLOR_WHITE,  dbg
  C.init_pair 3, C::COLOR_GREEN,  dbg
  C.init_pair 4, C::COLOR_YELLOW, dbg
  C.init_pair 5, C::COLOR_GREEN,  dbg
  C.init_pair 6, C::COLOR_GREEN,  dbg
  C.init_pair 7, C::COLOR_WHITE,  dbg
  C.init_pair 8, C::COLOR_RED,    dbg
end

def color sym, bold = false
  C.color_pair([:blue, :normal, :match, :chosen,
                :match!, :fan, :info, :red].index(sym) + 1) |
    (bold ? C::A_BOLD : 0)
end

@read =
  if $stdin.tty?
    if !`which find`.empty?
      IO.popen("find * -path '*/\\.*' -prune -o -type f -print -o -type l -print 2> /dev/null")
    else
      exit 1
    end
  else
    $stdin
  end

reader = Thread.new {
  while line = @read.gets
    emit(:new) { @new << line.chomp }
  end
  emit(:loaded) { true }
  @smtx.synchronize { @fan = [] }
}

main     = Thread.current
searcher = Thread.new {
  events  = {}
  fcache  = {}
  matches = []
  mcount  = 0      # match count
  plcount = 0      # prev list count
  q       = ''
  vcursor = 0
  zz      = [0, 0]

  begin
    while true
      wait_for_completion = nil
      @mtx.synchronize do
        while true
          events.merge! @events
          wait_for_completion = !@sort && !events[:loaded]

          if @events.empty? # No new events
            @cv.wait @mtx
            next
          end
          @events.clear
          break
        end

        if !wait_for_completion && events[:new]
          @lists << [@new, {}]
          @count += @new.length
          @new    = []
          fcache  = {}
        end
      end#mtx

      if wait_for_completion
        @smtx.synchronize do
          print_info false, " +#{@new.length}"
          print_input
          refresh
          sleep 0.1
        end
        next
      end

      new_search = events[:key] || events[:new]
      user_input = events[:key] || events[:vcursor]

      if new_search && !@lists.empty?
        events.delete :new
        q = events.delete(:key) || q
        regexp = q.empty? ? nil :
          Regexp.new(convert_query(q).inject('') { |sum, e|
            e = Regexp.escape e
            sum << "#{e}[^#{e}]*?"
          }, @rxflag)

        matches = fcache[q] ||=
          begin
            @smtx.synchronize do
              print_info true, ' ..'
              print_input
              refresh
            end unless q.empty?

            found = []
            skip  = false
            @lists.each do |pair|
              @mtx.synchronize { skip = @events[:key] }
              break if skip

              list, cache = pair
              found.concat(cache[q] ||= begin
                prefix, suffix = @query[0, @cursor_x], @query[@cursor_x..-1] || ''
                prefix_cache = suffix_cache = nil

                (prefix.length - 1).downto(1) do |len|
                  break if prefix_cache = cache[prefix[0, len]]
                end

                0.upto(suffix.length - 1) do |idx|
                  break if suffix_cache = cache[suffix[idx..-1]]
                end unless suffix.empty?

                partial_cache = [prefix_cache, suffix_cache].compact.sort_by { |e| e.length }.first
                (partial_cache ? partial_cache.map { |e| e.first } : list).map { |line|
                  if regexp
                    # Ignore errors: e.g. invalid byte sequence in UTF-8
                    md = line.match(regexp) rescue nil
                    md ? [line, md.offset(0)] : nil
                  else
                    [line, zz]
                  end
                }.compact
              end)
            end
            next if skip
            @sort ? found : found.reverse
          end

        mcount = matches.length
        if @sort && mcount <= @sort
          matches.replace matches.sort_by { |pair|
            line, offset = pair
            [offset.last - offset.first, line.length, line]
          }
        end
      end#new_search

      # This small delay reduces the number of partial lists
      sleep 0.2 unless user_input

      if events.delete(:vcursor) || new_search
        @mtx.synchronize do
          plcount  = [@matches.length, max_items].min
          @matches = matches
          vcursor  = @vcursor = [0, [@vcursor, mcount - 1, max_items - 1].min].max
        end
      end

      # Output
      @smtx.synchronize do
        item_length = [mcount, max_items].min
        if item_length < plcount
          plcount.downto(item_length) do |idx|
            C.setpos cursor_y - idx - 2, 0
            C.clrtoeol
          end
        end

        maxc = C.cols - 5
        matches[0, max_items].each_with_index do |item, idx|
          next if !new_search && !((vcursor-1)..(vcursor+1)).include?(idx)

          line, offset = convert_item item
          row = cursor_y - idx - 2
          chosen = idx == vcursor

          if line.length > maxc
            line = line[0, maxc] + '..'
          end

          basic = chosen ? color(:chosen, true) : color(:normal)

          C.setpos row, 0
          C.clrtoeol
          cprint chosen ? '>' : ' ', color(:red, true)
          cprint ' ', color(chosen ? :chosen : :normal)

          C.attron basic

          b, e = offset
          e = [e, maxc].min
          if b < maxc && b < e
            C.addstr line[0, b]
            cprint   line[b...e], color(chosen ? :match! : :match, chosen)
            C.attron basic
            C.addstr line[e..-1] || ''
          else
            C.addstr line
          end
          C.attroff basic
        end

        print_info if !@lists.empty? || events[:loaded]
        print_input
        refresh
      end
    end
  rescue Exception => e
    main.raise e
  end
}

got = nil
begin
  tty     = IO.open(IO.sysopen('/dev/tty'), 'r')
  input   = ''
  cursor  = 0
  actions = {
    :nop     => proc {},
    ctrl(:c) => proc { exit 1 },
    ctrl(:d) => proc { exit 1 if input.empty? },
    ctrl(:m) => proc {
      @mtx.synchronize do
        got = @matches.fetch(@vcursor, [])[0]
      end
      exit 0
    },
    ctrl(:u) => proc { input = input[cursor..-1]; cursor = 0 },
    ctrl(:a) => proc { cursor = 0 },
    ctrl(:e) => proc { cursor = input.length },
    ctrl(:j) => proc { emit(:vcursor) { @vcursor -= 1 } },
    ctrl(:k) => proc { emit(:vcursor) { @vcursor += 1 } },
    ctrl(:w) => proc {
      ridx   = (input[0...cursor - 1].rindex(/\S\s/) || -2) + 2
      input  = input[0...ridx] + input[cursor..-1]
      cursor = ridx
    },
    127 => proc { input[cursor -= 1] = '' if cursor > 0 },
    :left => proc { cursor = [0, cursor - 1].max },
    :right => proc { cursor = [input.length, cursor + 1].min },
  }
  actions[ctrl(:b)] = actions[:left]
  actions[ctrl(:f)] = actions[:right]
  actions[ctrl(:h)] = actions[127]
  actions[ctrl(:n)] = actions[ctrl(:j)]
  actions[ctrl(:p)] = actions[ctrl(:k)]

  while true
    ord = tty.getc.ord
    if ord == 27
      ord = tty.getc.ord
      if ord == 91
        ord = case tty.getc.ord
              when 68 then :left
              when 67 then :right
              when 66 then ctrl(:j)
              when 65 then ctrl(:k)
              else :nop
              end
      end
    end

    actions.fetch(ord, proc { |ord|
      char = [ord].pack('U*')
      if char =~ /[[:print:]]/
        input.insert cursor, char
        cursor += 1
      end
    }).call(ord)

    # Dispatch key event
    emit(:key) { @query = input.dup }

    # Update user input
    @smtx.synchronize do
      @cursor_x = cursor
      print_input
      refresh
    end
  end
ensure
  C.close_screen
  $stderr.puts got if got
end

