Skip to content

Commit

Permalink
Added automatic detection of platform-specific open command
Browse files Browse the repository at this point in the history
  • Loading branch information
a-sr committed Jan 24, 2023
1 parent 348ad89 commit dd8495a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
9 changes: 3 additions & 6 deletions C/src/modal_models/FurutaPendulum/FurutaPendulum.lf
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
* This program specifies a build script that only code generates
* and compiles the program, as usual, but also executes the
* program and processes its output to generate and open a
* plot. You may need to change the last argument passed to
* the build script to the name of a PDF viewer installed on
* your system.
*
* plot.
* You have to have installed gnuplot and have it in your
* PATH for this script to work as expected (and also cmake).
*
Expand All @@ -23,7 +20,7 @@ target C {
timeout: 3 secs,
fast: true,
flags: "-lm",
build: "./build_run_plot.sh FurutaPendulum open"
build: "./build_run_plot.sh FurutaPendulum"
}
import PendulumController from "PendulumController.lf";
import PendulumSimulation from "PendulumSimulation.lf";
Expand All @@ -43,4 +40,4 @@ main reactor {
c.energy -> p.energy;
s.theta -> p.theta;
s.phi -> p.phi;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ target C {
timeout: 5 secs,
fast: true,
flags: "-lm",
build: "./build_run_plot.sh FurutaPendulumDisturbance open"
build: "./build_run_plot.sh FurutaPendulumDisturbance"
}
import PendulumController from "PendulumController.lf";
import PendulumSimulation from "PendulumSimulation.lf";
Expand Down
4 changes: 2 additions & 2 deletions C/src/modal_models/FurutaPendulum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ It relies on [cmake](https://cmake.org/) and [gnuplot](http://www.gnuplot.info/)
The script is not compatible to Windows systems.

The script automatically opens the resulting plot.
By default it uses the 'open' command (MacOS).
You may need to change the last argument passed to the build script to the name of a PDF viewer installed on your system (e.g. 'xdg-open' on Linux).
By default it uses the 'open' command on MacOS and 'xdg-open' on Linux.
If this command is not available on your system you have to adjust the script ('openPDF' variable).


## Summary of the Examples
Expand Down
12 changes: 9 additions & 3 deletions C/src/modal_models/FurutaPendulum/build_run_plot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
# The first argument is the name of the main reactor
# and the second argument is the viewer to render the PDF in.

# Determine platform specific command to open PDF
openPDF="open"
if [[ "$OSTYPE" =~ ^linux ]]; then
openPDF="xdg-open"
fi

# Build the generated code.
cd ${LF_SOURCE_GEN_DIRECTORY}
cmake .
Expand All @@ -22,10 +28,10 @@ ${LF_BIN_DIRECTORY}/$1
gnuplot pendulum.gnuplot

# Open the produced PDF using the specified viewer
if ! command -v $2 &> /dev/null
if ! command -v $openPDF &> /dev/null
then
echo "'$2' could not be found; please specify another PDF viewer."
echo "'$openPDF' could not be found; please specify another PDF viewer."
exit
else
$2 pendulum.pdf &
$openPDF pendulum.pdf &
fi

4 comments on commit dd8495a

@lhstrh
Copy link
Member

@lhstrh lhstrh commented on dd8495a Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You beat me to it! :-) Looks great! Maybe there is a way to do this in an app-agnostic way for Windows as well? https://stackoverflow.com/questions/6557920/how-to-open-a-pdf-in-fullscreen-view-via-command-line-on-windows

@a-sr
Copy link
Contributor Author

@a-sr a-sr commented on dd8495a Jan 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt that Windows can run an sh-script at all. Similarly, gnuplot, unless one is running a Linux shell, right?

@a-sr
Copy link
Contributor Author

@a-sr a-sr commented on dd8495a Jan 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I added the open commands for Linux systems on windows (a6fa4d7) based on googling, I haven't tested them yet.

@lhstrh
Copy link
Member

@lhstrh lhstrh commented on dd8495a Jan 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just confirming that it works on Linux.

Please sign in to comment.