# The ampersand is special syntax for leo to execute the command# in the background. The "flock ... 9>" part is because when leo# exits, the background process doesn't get cleaned up and this# flock "incantation" will prevent a second instance from launching# The flock utility is Linux only.# The 'entr' utility (http://entrproject.org) will watch the given list# of filenames and run the commands whenever changedg.execute_shell_commands(f"""&(flock -n 9 || exit 1ls {c.fileName()} | entr -s 'git diff . | catgit commit -m "$(date +%Y-%m-%d) autocommit" .') 9> {c.fileName()}.entr.lock""")
I'm currently using leo as my main note-taking application and I find it very useful to have auto-saving, so I enabled the auto-save module.I felt a little nervous about using auto-save and decided I wanted a git commit on every save so I would have a history in case anything goes wrong.
#!/bin/bash
cd ~/leo-editor
git pull
cd /working/MEGA/leo-files
./rerun2 ./push &
python3 ~/leo-editor/launchLeo.py $1 $2 $3
#!/usr/bin/env bash
# Events that occur within this time from an initial one are ignored
ignore_secs=0.25
clear='false'
verbose='false'
function usage {
echo "Rerun a given command every time filesystem changes are detected."
echo ""
echo "Usage: $(basename $0) [OPTIONS] COMMAND"
echo ""
echo " -c, --clear Clear the screen before each execution of COMMAND."
echo " -v, --verbose Print the name of the files that changed to cause"
echo " each execution of COMMAND."
echo " -h, --help Display this help and exit."
echo ""
echo "Run the given COMMAND, and then every time filesystem changes are"
echo "detected in or below the current directory, run COMMAND again."
echo "Changes within $ignore_secs seconds are grouped into one."
echo ""
echo "This is useful for running commands to regenerate visual output every"
echo "time you hit [save] in your editor. For example, re-run tests, or"
echo "refresh markdown or graphviz rendering."
echo ""
echo "COMMAND can only be a simple command, ie. \"executable arg arg...\"."
echo "For compound commands, use:"
echo ""
echo " rerun bash -c \"ls -l | grep ^d\""
echo ""
echo "Using this it's pretty easy to rig up ad-hoc GUI apps on the fly."
echo "For example, every time you save a .dot file from the comfort of"
echo "your favourite editor, rerun can execute GraphViz to render it to"
echo "SVG, and refresh whatever GUI program you use to view that SVG."
echo ""
echo "COMMAND can't be a shell alias, and I don't understand why not."
}
while [ $# -gt 0 ]; do
case "$1" in
-c|--clear) clear='true';;
-v|--verbose) verbose='true' ;;
-h|--help) usage; exit;;
*) break;;
esac
shift
done
function execute() {
if [ $clear = "true" ]; then
clear
fi
if [ $verbose = "true" ]; then
if [ -n "$changes" ]; then
echo -e "Changed: $(echo -e $changes | cut -d' ' -f2 | sort -u | tr '\n' ' ')"
changes=""
fi
echo "$@"
fi
"$@"
}
execute "$@"
ignore_until=$(date +%s.%N)
inotifywait --quiet --recursive --monitor --format "%e %w%f" \
--event modify --event move --event create --event delete \
--exclude '__pycache__' --exclude '.cache' \
. | while read changed
do
changes="$changes\n$changed"
if [ $(echo "$(date +%s.%N) > $ignore_until" | bc) -eq 1 ] ; then
ignore_until=$(echo "$(date +%s.%N) + $ignore_secs" | bc)
( sleep $ignore_secs ; execute "$@" ) &
fi
done
#!/bin/bash
fossil add .
fossil commit . -m "Autocommit"
from collections import Counter
counter = Counter()
def activate():
c.user_dict['my_callback'] = my_callback
g.registerHandler('save2', my_callback)
def deactivate():
h = c.user_dict.pop('my_callback', None)
if h:
g.unregisterHandler('save2', h)
def my_callback(tag, kw):
c = kw.get('c')
counter[c.mFileName] += 1
g.es('git commit -a -m "version %d"'%counter[c.mFileName]) # do whatever
activate() # or deactivate()
def reactivate(tag, handler):
k = '_activation_%s_handler'%tag
# this is just to prevent collisions
# you can use whatever string as a key
old_handler = c.user_dict.pop(k, None)
if old_handler:
g.unregisterHandler(tag, old_handler)
g.registerHandler(tag, handler)
c.user_dict[k] = handler
reactivate('save2', my_callback)
--
You received this message because you are subscribed to the Google Groups "leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leo-editor+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leo-editor/630dd216-994f-4a7a-805a-e1b20ed2ae34%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to leo-e...@googlegroups.com.
#!/bin/bash
cd ~/leo-editor
git pull
cd /working/MEGA/leo-files
./rerun2 ./push &
python3 ~/leo-editor/launchLeo.py $1 $2 $3
Since rerun2 is still part of the console session it closes when I close Leo which looks after having it hang around clutttering up the joint.
This might be a little tighter than the method you use.
Here is where I'm confused. You are launching rerun2 in the background, so it will outlast your bash script for sure. Are you saying you close your console when leo exits and that causes rerun to exit? I run a console with many tabs open. In one tab I launch leo. If I leo exits, I just launch again within the same console instance. With that use case I expect to end up with multiple instances of rerun2.Brian
disown the subprocess or use nohup to have the process continue beyond the closing of the terminal.
HTH,
Chris
YMMV, but I had a bad experience with watching files when using Leo. Leo often writes files in two phases and it happened to me more than once that process watching on files take an empty file or not completely written because of this. So, I had to add some latency to watcher.
If you precede git command with the '&' g.execute_shell_commands will not wait for command to finish
g.execute_shell_commands('&echo My pid is $$')
[sh] <defunct>
--
You received this message because you are subscribed to the Google Groups "leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leo-editor+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leo-editor/d825e886-1465-4dbd-a407-d71af4e93763%40googlegroups.com.