dartboard_resume/lib/dartboard_runner.dart
2024-09-10 15:09:15 -06:00

83 lines
2.4 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:dartboard_resume/render.dart';
import 'package:hotreloader/hotreloader.dart';
import 'package:toml/toml.dart';
StreamSubscription<FileSystemEvent>? fileStreamSub;
StreamSubscription<String>? stdinStreamSub;
Future<void> dartboardRun(HotReloader? reloader) async {
const String tomlFilePath = "resume.toml";
if (reloader != null) {
stdout.writeln('Hot reload enabled!');
}
ProcessSignal.sigint.watch().listen((_) {
stdout.writeln('SIGINT received. Exiting gracefully...');
fileStreamSub?.cancel();
stdinStreamSub?.cancel();
// Perform cleanup or other necessary actions here
reloader?.stop();
exit(0); // Exit with code 0 to indicate a successful termination
});
stdinStreamSub = getUserInputStream().listen(
(event) {
if (event == "r") {
stdout.writeln("Triggering pdf render...");
createDocument(tomlFilePath);
}
if (event == "p") {
stdout.writeln("Current toml map:");
stdout.writeln(TomlDocument.loadSync(tomlFilePath).toMap());
}
if (event == "q") {
stdout.writeln('Exiting...');
fileStreamSub?.cancel();
stdinStreamSub?.cancel();
// Perform cleanup or other necessary actions here
reloader?.stop();
exit(0); // Exit with code 0 to indicate a successful termination
}
},
);
if (FileSystemEntity.isWatchSupported) {
final fileStream = File(tomlFilePath).watch(events: FileSystemEvent.modify);
fileStreamSub = fileStream.listen((e) {
createDocument(tomlFilePath);
});
stdout.writeln('Watching for file changes.');
} else {
stdout.writeln('File watch is not supported. Exiting upon completion.');
createDocument(tomlFilePath);
}
}
Stream<String> getUserInputStream() {
stdin.lineMode = false;
stdin.echoMode = false;
stdin.echoNewlineMode = false;
return stdin.transform(const Utf8Decoder()).transform(const LineSplitter());
}
void refreshViewer() {
final result = Process.runSync('pgrep', ['llpp']);
if (result.exitCode != 0) {
stdout.writeln(
'Unable to refresh the viewer\nDartboard Resume can refresh your pdf viewer! Here are the supported programs:\n\t- llpp',
);
return;
}
// Send the HUP signal to `llpp`
Process.runSync('pkill', ['-HUP', 'llpp']);
}
void createDocument(String tomlFilePath) {
renderPdf(tomlFilePath, force: true);
refreshViewer();
}