summary refs log tree commit diff stats
path: root/ranger.py
blob: 0046f37105ced29ccdd926f132d506c31954e752 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/python -O
# This file is part of ranger, the console file manager.  (coding: utf-8)
# License: GNU GPL version 3, see the file "AUTHORS" for details.

# =====================
# This embedded bash script can be executed by sourcing this file.
# It will cd to ranger's last location after you exit it.
# The first argument specifies the command to run ranger, the
# default is simply "ranger". (Not this file itself!)
# The other arguments are passed to ranger.
"""":
tempfile="$(mktemp -t tmp.XXXXXX)"
ranger="${1:-ranger}"
test -z "$1" || shift
"$ranger" --choosedir="$tempfile" "${@:-$(pwd)}"
returnvalue=$?
test -f "$tempfile" &&
if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
    cd "$(cat "$tempfile")"
fi
rm -f -- "$tempfile"
return $returnvalue
"""

from __future__ import (absolute_import, division, print_function)

import sys
from os.path import exists, abspath

# Need to find out whether or not the flag --clean was used ASAP,
# because --clean is supposed to disable bytecode compilation
ARGV = sys.argv[1:sys.argv.index('--')] if '--' in sys.argv else sys.argv[1:]
sys.dont_write_bytecode = '-c' in ARGV or '--clean' in ARGV

# Don't import ./ranger when running an installed binary at /usr/.../ranger
if __file__[:4] == '/usr' and exists('ranger') and abspath('.') in sys.path:
    sys.path.remove(abspath('.'))

# Start ranger
import ranger  # NOQA pylint: disable=import-self,wrong-import-position
sys.exit(ranger.main())  # pylint: disable=no-member
# # flags are a string or array containing: # * a = run all # * d or e = detach # * t = run in a terminal # * w = wait for <enter> after execution # * c = run from ./ranger.rb <filename> (not usable from inside ranger) # * capital letter inverts ## accessors {{{ attr_accessor( *%w[ all detach wait new_term console files handlers paths mode exec multi ]) attr_reader( :flags ) def flags=(x) assert x, Array, String if x.is_a? Array @flagstring = x.join('') @flags = x elsif x.is_a? String @flagstring = x @flags = x.split(//) end parse_flags return x end ## }}} def initialize(files, mode=nil, flags=nil, preferred_app=nil) @mode = mode.to_i if files.is_a? Array @files = files.dup else @files = [files.dup] end self.flags = flags || '' if @flags.include?( "A" ) and ( cf = Fm.currentfile ).is_a? Directory::Entry @files = [cf] end @files.reject! {|file| file.handler == nil or !file.exists? } @handlers = @files.map {|file| file.handler} @paths = @files.map {|file| file.path} @handler = preferred_app || @handlers.first @multi = (@files.size > 1 and @handlers.uniq.size == 1) if @handler @exec = Application.send(@handler, self) else @exec = nil end end def has_flag? x if x.is_a? Regexp @flagstring =~ x elsif x.is_a? String @flags.include? x else false end end def parse_flags @all = @detach = @new_term = @wait = false ## Positive flags if has_flag? 'a' @all = true end if has_flag? /[de]/ @detach = true end if has_flag? 't' @new_term = true @detach = true end if has_flag? 'w' @wait = true end if has_flag? 'c' @console = true end ## Negative flags if has_flag? 'A' @all = false end if has_flag? /[DE]/ @detach = false end if has_flag? 'T' @new_term = false end if has_flag? 'W' @wait = false end if has_flag? 'C' @console = false end end def no_mode?() @mode == 0 end def no_flags?() @flagstring.empty? end def default_flags=(x) if @flagstring.empty? self.flags = x end return x end def base_flags=(x) newflags = (x.is_a? Array) ? x : x.split(//) for flag in newflags unless @flags.include? flag.upcase or @flags.include? flag.downcase @flags << flag end end self.flags = @flags return x end ## set the mode and return self. def with_mode(n) @mode = n self end ## wrapper {{{ ## escape all files for direct use in the shell. ## if the _multi_ attribute is true, this is a shortcut for ## rc.paths.map {|x| ~x}.join(' ') ## otherwise: ## ~(rc.paths.first) def ~ if @multi @paths.map {|x| ~x}.join(' ') else ~@paths.first end end alias to_s ~ ## escape one (the first) file for direct use in the shell. ## this is a shortcut for: ## ~(rc.paths.first) def one ~@paths.first end ## shortcut for _files.size_ def size() @files.size end ## shortcut for _files.first.path_ def first() @files.first end ## shortcut for _files.first.name_ def name() @files.first.name end ## }}} end