liveOpenProgram function

LiveProgram liveOpenProgram(
  1. String file, {
  2. String? os,
  3. LiveProcesses processes = const LiveProcesses(),
})

The program that opens file in the system's image viewer on os (as Platform.operatingSystem names it).

Throws a LiveCommandException saying what to install when there's no viewer to open it with.

Implementation

LiveProgram liveOpenProgram(
  String file, {
  String? os,
  LiveProcesses processes = const LiveProcesses(),
}) {
  switch (os ?? Platform.operatingSystem) {
    case 'macos':
      return (executable: 'open', arguments: [file]);
    case 'windows':
      return (executable: 'cmd', arguments: ['/c', 'start', '', file]);
    default:
      if (processes.find('xdg-open') == null) {
        throw LiveCommandException(
          'Opening the screenshot needs xdg-open. Install xdg-utils, then '
          'try again.',
        );
      }
      // The viewer keeps running, so it's left in the background.
      return (
        executable: '/bin/sh',
        arguments: ['-c', r'xdg-open "$1" >/dev/null 2>&1 &', 'sh', file],
      );
  }
}