dartboard_resume/lib/dartboard_runner.dart

92 lines
2.7 KiB
Dart
Raw Normal View History

2024-09-10 15:09:15 -06:00
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:dartboard_resume/render.dart';
import 'package:dartboard_resume/utils.dart';
2024-09-10 15:09:15 -06:00
import 'package:hotreloader/hotreloader.dart';
import 'package:toml/toml.dart';
StreamSubscription<FileSystemEvent>? fileStreamSub;
StreamSubscription<String>? stdinStreamSub;
Future<void> dartboardRun(HotReloader? reloader, List<String> arguments) async {
final result = getTomlFromArgs(arguments);
if (!result.success) {
exitRunner(reloader);
}
final resumeTomlFile = result.file!;
2024-09-10 15:09:15 -06:00
if (reloader != null) {
stdout.writeln('Hot reload enabled!');
}
ProcessSignal.sigint.watch().listen((_) {
stdout.writeln('SIGINT received. Exiting gracefully...');
exitRunner(reloader);
2024-09-10 15:09:15 -06:00
});
stdinStreamSub = getUserInputStream().listen(
(event) {
if (event == "r") {
stdout.writeln("Triggering pdf render...");
createDocument(resumeTomlFile.path);
2024-09-10 15:09:15 -06:00
}
if (event == "p") {
stdout.writeln("Current toml map:");
stdout.writeln(TomlDocument.loadSync(resumeTomlFile.path).toMap());
2024-09-10 15:09:15 -06:00
}
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 = resumeTomlFile.watch(events: FileSystemEvent.modify);
2024-09-10 15:09:15 -06:00
fileStreamSub = fileStream.listen((e) {
createDocument(resumeTomlFile.path);
2024-09-10 15:09:15 -06:00
});
stdout.writeln('Watching for file changes.');
} else {
stdout.writeln('File watch is not supported. Exiting upon completion.');
createDocument(resumeTomlFile.path);
2024-09-10 15:09:15 -06:00
}
}
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();
}
void exitRunner(HotReloader? reloader) {
fileStreamSub?.cancel();
stdinStreamSub?.cancel();
// Perform cleanup or other necessary actions here
reloader?.stop();
exit(0); // Exit with code 0 to indicate a successful termination
}