diff options
author | hut <hut@lavabit.com> | 2009-07-24 02:10:05 +0200 |
---|---|---|
committer | hut <hut@lavabit.com> | 2009-07-24 02:10:05 +0200 |
commit | cbda44e7ca9d957dd48b231b7d463e89d5081053 (patch) | |
tree | 06ce04b5eaa8ac0c02a186cf2c9bdfdee2155ada /code/search.rb | |
parent | 3cd5b440d7d7e4714726fa3a68b9724d1bc6d0bc (diff) | |
download | ranger-cbda44e7ca9d957dd48b231b7d463e89d5081053.tar.gz |
implemented #13 (special keys for quick navigation)
Diffstat (limited to 'code/search.rb')
-rw-r--r-- | code/search.rb | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/code/search.rb b/code/search.rb new file mode 100644 index 00000000..2952f59a --- /dev/null +++ b/code/search.rb @@ -0,0 +1,141 @@ +module Fm + def goto(arg) + if arg.is_a? Directory::Entry + @pwd.pointed_file = arg.path + + elsif arg.is_a? String + @pwd.pointed_file = arg + + elsif arg.is_a? Numeric + @pwd.pos = arg + + else + lograise ArgumentError.new + end + end + + FIND_PROPERTIES = { + 's' => :size, + 'm' => :mtime, + 'c' => :ctime, + } + FIND_KEY_REGEXP = /f([#{ FIND_PROPERTIES.keys.join("") }])/ + + def quicksearch(n) + log Option.search_method + case Option.search_method + when *FIND_PROPERTIES.values + quicksearch_by_property(n, Option.search_method) + when :handler + quicksearch_by_handler(n) + when :regexp + quicksearch_by_regexp(n) + else + raise "Wrong search method!" + end rescue lograise + end + + def quicksearch_by_property(n, property) + sorted = @pwd.files.sort do |a, b| + b.send(property) <=> a.send(property) + end + + if @search_reset + @search_reset = false + else + sorted.wrap(sorted.index(currentfile) + n) + end + + goto(sorted.first) + end + + def quicksearch_by_handler(n) + sorted = @pwd.files.sort do |a, b| + a.handler.to_s <=> b.handler.to_s + end + + goto(sorted.first) + end + + def quicksearch_by_regexp(n) + begin + rx = Regexp.new(@search_string, Regexp::IGNORECASE) + rescue + return false + end + + ary = @pwd.files.dup + ary.wrap(@pwd.pos) + if n < 0 + ary.wrap(1) + ary.reverse! + end + ary.wrap(n.abs) + + for file in ary + if file.basename =~ rx + return goto(file) + end + end + end + + def search_reset! + @search_reset = true + end + + def search_reset(array) + if @search_reset + @search_reset = false + sorted.wrap(sorted.index(currentfile) + n) + end + end + + def search(str, offset=0, backwards=false) + begin + rx = Regexp.new(str, Regexp::IGNORECASE) + rescue + return false + end + + ary = @pwd.files_raw.dup + ary.wrap(@pwd.pos + offset) + + ary.reverse! if backwards + + for f in ary + g = File.basename(f) + if g =~ rx + @pwd.pointed_file = f + break + end + end + end + + def hints(str) + begin + rx = Regexp.new(str, Regexp::IGNORECASE) + rescue + return false + end + + ary = @pwd.files_raw.dup + ary.wrap(@pwd.pos) + + n = 0 + pointed = false + for f in ary + g = File.basename(f) + if g =~ rx + unless pointed + log "point at #{f}" + @pwd.pointed_file = f + pointed = true + end + n += 1 + end + end + + return n + end + +end |