Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'streaming mode' which draws animated graphs from stdin #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions tests/streaming.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
while true; do

cat <<EOF
digraph {
A -> B;
}

EOF
printf '\0'
sleep 1

cat <<EOF
digraph {
A -> B;
B -> C;
}

EOF
printf '\0'
sleep 1

cat <<EOF
digraph {
A -> B;
B -> C;
B -> D;
}

EOF
printf '\0'
sleep 1

cat <<EOF
digraph {
A -> B;
B -> C;
B -> D;
C -> E;
}

EOF
printf '\0'
sleep 1

cat <<EOF
digraph {
A -> B;
B -> C;
B -> D;
C -> E;
D -> E;
}

EOF
printf '\0'
sleep 1
done
30 changes: 30 additions & 0 deletions xdot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import argparse
import sys
from threading import Thread

from .ui.window import DotWindow, Gtk

Expand Down Expand Up @@ -61,6 +62,10 @@ def main():
'--hide-toolbar',
action='store_true', dest='hide_toolbar',
help='Hides the toolbar on start.')
parser.add_argument(
'--streaming-mode',
action='store_true', dest='streaming_mode',
help='Updates canvas when a new graph is read from stdin')

options = parser.parse_args()
inputfile = options.inputfile
Expand Down Expand Up @@ -89,7 +94,32 @@ def main():
import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

def read():
istream = sys.stdin.detach()
buf = bytearray()
first = True
# if there's a more efficient way to do this than to scan
# stdin character by character, I'm all for it. This just gets
# us off the ground.
#
# what we're doing is looking for the null byte which
while True:
byte = istream.read(1)
if not byte:
pass # Gtk.main_quit()
elif byte == b'\0':
win.set_dotcode(bytes(buf), preserve_viewport=not first)
buf = bytearray()
first = False
else:
buf += byte

if options.streaming_mode:
reader = Thread(target=read, daemon=True)
reader.start()

Gtk.main()


if __name__ == '__main__':
main()
14 changes: 8 additions & 6 deletions xdot/ui/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,15 +706,17 @@ def textentry_activate(self, widget, entry):
def set_filter(self, filter):
self.dotwidget.set_filter(filter)

def set_dotcode(self, dotcode, filename=None):
if self.dotwidget.set_dotcode(dotcode, filename):
def set_dotcode(self, dotcode, filename=None, preserve_viewport=False):
if self.dotwidget.set_dotcode(dotcode, filename, center=not preserve_viewport):
self.update_title(filename)
self.dotwidget.zoom_to_fit()
if not preserve_viewport:
self.dotwidget.zoom_to_fit()

def set_xdotcode(self, xdotcode, filename=None):
if self.dotwidget.set_xdotcode(xdotcode):
def set_xdotcode(self, xdotcode, filename=None, preserve_viewport=False):
if self.dotwidget.set_xdotcode(xdotcode, center=not preserve_viewport):
self.update_title(filename)
self.dotwidget.zoom_to_fit()
if not preserve_viewport:
self.dotwidget.zoom_to_fit()

def update_title(self, filename=None):
if filename is None:
Expand Down