xonsh/docs/tutorial_events.rst

55 lines
1.8 KiB
ReStructuredText
Raw Normal View History

2016-08-24 21:46:51 -04:00
.. _tutorial_events:
************************************
Tutorial: Events
************************************
What's the best way to keep informed in xonsh? Subscribe to an event!
Overview
========
2016-08-27 23:20:44 -04:00
Simply, events are a way for various pieces of xonsh to tell each other what's going on. They're
fired when something of note happens, eg the current directory changes or just before a command is
executed.
2016-08-24 21:46:51 -04:00
2016-08-27 23:29:23 -04:00
While xonsh has its own event system, it is not dissimilar to other event systems. If you do know
events, this should be easy to understand. If not, then this document is extra for you.
2016-08-24 21:46:51 -04:00
Show me the code!
=================
Fine, fine!
This will add a line to a file every time the current directory changes (due to ``cd``, ``pushd``,
2016-08-27 23:20:44 -04:00
or several other commands)::
2016-08-24 21:46:51 -04:00
@events.on_chdir
2017-01-27 21:32:25 -05:00
def add_to_file(olddir, newdir, **kw):
2016-08-24 21:46:51 -04:00
with open(g`~/.dirhist`[0], 'a') as dh:
print(newdir, file=dh)
2016-08-27 23:20:44 -04:00
The exact arguments passed and returns expected vary from event to event; see the
`event list <events.html>`_ for the details.
2016-08-24 21:46:51 -04:00
2017-01-27 21:32:25 -05:00
Note that the event system is keyword only. Event handlers must match argument names and must have a
``**kw`` as protection against future changes.
2016-08-27 23:20:44 -04:00
Can I use this, too?
====================
Yes! It's even easy! In your xontrib, you just have to do something like::
events.doc('myxontrib_on_spam', """
myxontrib_on_spam(can: Spam) -> bool?
Fired in case of spam. Return ``True`` if it's been eaten.
""")
This will enable users to call ``help(events.myxontrib_on_spam)`` and get useful output.
2016-08-27 22:38:12 -04:00
Further Reading
===============
For a complete list of available events, see `the events reference <events.html>`_.
2016-08-27 22:38:12 -04:00
2016-08-27 23:20:44 -04:00
If you want to know more about the gory details of what makes events tick, see
`Advanced Events <advanced_events.html>`_.