summary refs log blame commit diff stats
path: root/examples/plugin_ipc.py
blob: c6fc60c6b607d76f7bd2b597b3fe34759c3a35c7 (plain) (tree)
1
2
3
4
5
6
7
8
9








                                                                      
                                                                  
 

                 

                                    

 



                        

                                                        





                                    
 




                                                    
                                                        







                                         
                                              



                              


                         
                                
# Tested with ranger 1.7.0 through ranger 1.7.*
#
# This plugin creates a FIFO in /tmp/ranger-ipc.<PID> to which any
# other program may write. Lines written to this file are evaluated by
# ranger as the ranger :commands.
#
# Example:
#   $ echo tab_new ~/images > /tmp/ranger-ipc.1234

from __future__ import (absolute_import, division, print_function)

import ranger.api


HOOK_INIT_OLD = ranger.api.hook_init


def hook_init(fm):
    try:
        # Create a FIFO.
        import os
        ipc_fifo = "/tmp/ranger-ipc." + str(os.getpid())
        os.mkfifo(ipc_fifo)

        # Start the reader thread.
        try:
            import thread
        except ImportError:
            import _thread as thread

        def ipc_reader(filepath):
            while True:
                with open(filepath, 'r') as fifo:
                    line = fifo.read()
                    fm.execute_console(line.strip())
        thread.start_new_thread(ipc_reader, (ipc_fifo,))

        # Remove the FIFO on ranger exit.
        def ipc_cleanup(filepath):
            try:
                os.unlink(filepath)
            except IOError:
                pass
        import atexit
        atexit.register(ipc_cleanup, ipc_fifo)
    except IOError:
        # IPC support disabled
        pass
    finally:
        HOOK_INIT_OLD(fm)


ranger.api.hook_init = hook_init
span>[colours] bkgnd=default titlebar=magenta titlebar.text=white titlebar.brackets=white titlebar.unencrypted=black titlebar.encrypted=cyan titlebar.untrusted=yellow titlebar.trusted=cyan titlebar.online=white titlebar.offline=black titlebar.away=white titlebar.chat=white titlebar.dnd=black titlebar.xa=white statusbar=default statusbar.text=white statusbar.time=white statusbar.brackets=red statusbar.active=cyan statusbar.new=white main.text=blue main.text.me=white main.text.them=bold_white main.splash=red main.help.header=bold_blue main.time=green input.text=yellow subscribed=bold_magenta unsubscribed=bold_black otr.started.trusted=cyan otr.started.untrusted=yellow otr.ended=blue otr.trusted=cyan otr.untrusted=yellow online=red away=cyan chat=green dnd=megenta xa=cyan offline=green incoming=yellow mention=green trigger=green typing=magenta gone=yellow error=red roominfo=white roommention=bold_green roommention.term=bold_green roomtrigger=bold_green roomtrigger.term=bold_green me=white them=white roster.header=bold_cyan roster.chat=green roster.online=red roster.away=cyan roster.xa=cyan roster.dnd=magenta roster.offline=green roster.chat.active=green roster.online.active=red roster.away.active=cyan roster.xa.active=cyan roster.dnd.active=magenta roster.offline.active=green roster.chat.unread=bold_green roster.online.unread=bold_red roster.away.unread=bold_cyan roster.xa.unread=bold_cyan roster.dnd.unread=bold_magenta roster.offline.unread=bold_green roster.room=red roster.room.unread=bold_red roster.room.mention=bold_red roster.room.trigger=bold_red occupants.header=bold_cyan receipt.sent=red