Skip to content

Instantly share code, notes, and snippets.

@Hikari9
Last active April 15, 2017 08:36
Show Gist options
  • Save Hikari9/9f8f93bb612151e56f22788800e207aa to your computer and use it in GitHub Desktop.
Save Hikari9/9f8f93bb612151e56f22788800e207aa to your computer and use it in GitHub Desktop.
Save autorun for Atom Editor C++
#/bin/bash
# kill current running processes first
kill $(ps -ax | grep "$(cat test.runformat)" | awk '{print $1;}' | paste -sd " " -) 2>> /dev/null &
echo "Running $0"
# compile first if argument is supplied
if [ $# -eq 1 ]; then
# compile and run file
export fullpath=$1
export pathpath=$(basename $fullpath)
export extension="${pathpath##*.}"
export filename="$(basename $fullpath .$extension)"
echo "Compiling..." > test-preview.out
echo "$filename" > test.filename
if [ "$extension" == "cpp" ]; then
echo './a.out' > test.runformat
$($(g++ -Wall -O2 "$1" && echo '[C++98]' > test.format) || $(g++ -std=c++11 -O2 $1 && echo '[C++11]' > test.format))
exec bash $0
elif [ "$extension" == "java" ]; then
echo '[Java]' > test.format
echo "java $(cat test.filename)" > test.runformat
javac "$fullpath" 2>> test-preview.out
exec bash $0
elif [ "$extension" == "python" ]; then
echo '[Python]' > test.format
echo "python -m py_compile $(cat test.filename).py" > test.runformat
exec bash $0
else
echo '[Format unspecified]' > test.format
fi
else
# run
cat test.format > test-preview.out
echo "Running... " >> test-preview.out
export TIMEOUT=5
export MAX_LINES=100
(time ( $(timeout --foreground $TIMEOUT $(cat test.runformat) < test.in > test.out) || printf "TLE: " >> test-preview.out) ) 2>&1 | grep real | awk '{print $2;}' >> test-preview.out
a=$(wc -l < test.out)
b=$MAX_LINES
min=$([ $a -le $MAX_LINES ] && echo "$a" || echo "$b")
mb=$(ls -sh test.out | cut -d' ' -f1)
echo "----- test.out [${mb}B, $min/$a lines] -----" >> test-preview.out
cat test.out | head --lines=$MAX_LINES >> test-preview.out
fi
"**/*.cpp": "bash .autocompile.sh ${file}"
"**/*.java": "bash .autocompile.sh ${file}"
"**/*.py": "bash .autocompile.sh ${file}"
"**/test.in": "bash .autocompile.sh"
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char **args) {
int max_lines = 1000;
if (argc > 1) max_lines = atoi(args[1]);
string line;
while (getline(cin, line) && max_lines-- > 0)
cout << line << endl;
if (max_lines < 0)
cout << "..." << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment