Skip to content

Instantly share code, notes, and snippets.

@luqmaan
Last active December 14, 2015 16:48
Show Gist options
  • Save luqmaan/5117196 to your computer and use it in GitHub Desktop.
Save luqmaan/5117196 to your computer and use it in GitHub Desktop.
LiveReload for the terminal. Performs a shell command whenever a matching file changes. Just specify the command to run and the file extensions to monitor.
#!/usr/bin/env python
""" Example:
Enter command to run when a matched file changes:
g++ hashing.cpp - o main & & ./main
Enter space seperated list of file extensions to monitor:
.cpp .h
"""
import os
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
cmd = ""
exts = ""
class ChangeHandler(FileSystemEventHandler):
def on_modified(self, event):
global cmd, exts
for e in exts:
if (event.src_path[-len(e):] == e):
print "======================================"
print event
print "======================================"
os.system(cmd)
def main():
if __name__ == "__main__":
global cmd, exts
cmd = raw_input("Enter command to run when a matched file changes: ")
exts = raw_input("Enter space seperated list of file extensions to monitor: ")
exts = exts.split(' ')
event_handler = ChangeHandler()
path = "."
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment