| 1 | ! Sniffly - Pure Fortran GUI Disk Space Analyzer |
| 2 | ! Main program entry point |
| 3 | ! |
| 4 | ! Inspired by SpaceSniffer (Windows) |
| 5 | ! Built with GTK4 and gtk-fortran |
| 6 | ! |
| 7 | ! Part of the FortranGoingOnForty project |
| 8 | ! Proving Fortran can build beautiful GUI applications! |
| 9 | |
| 10 | program sniffly_main |
| 11 | use gtk_app |
| 12 | use file_system, only: get_absolute_path, is_directory |
| 13 | use version |
| 14 | implicit none |
| 15 | |
| 16 | integer :: exit_status, nargs, iostat_val |
| 17 | character(len=512) :: arg, scan_dir, home_dir |
| 18 | character(len=:), allocatable :: abs_path |
| 19 | logical :: has_terminal |
| 20 | |
| 21 | ! Check if we have a terminal (if not, skip print statements to avoid hanging) |
| 22 | ! When launched from Finder, print statements can cause the app to hang |
| 23 | inquire(unit=6, opened=has_terminal, iostat=iostat_val) |
| 24 | if (iostat_val /= 0) has_terminal = .false. |
| 25 | |
| 26 | ! Print banner (only if terminal is available) |
| 27 | if (has_terminal) then |
| 28 | print '(A)', "======================================" |
| 29 | print '(A)', " " // SNIFFLY_NAME // " v" // SNIFFLY_VERSION |
| 30 | print '(A)', " " // SNIFFLY_DESCRIPTION |
| 31 | print '(A)', " Pure Fortran + GTK4" |
| 32 | print '(A)', "======================================" |
| 33 | print '(A)', "" |
| 34 | end if |
| 35 | |
| 36 | ! Parse command-line arguments |
| 37 | nargs = command_argument_count() |
| 38 | |
| 39 | if (nargs > 0) then |
| 40 | call get_command_argument(1, arg) |
| 41 | |
| 42 | ! Handle flags |
| 43 | if (trim(arg) == '--help' .or. trim(arg) == '-h') then |
| 44 | call print_usage() |
| 45 | stop 0 |
| 46 | else if (trim(arg) == '--version' .or. trim(arg) == '-v') then |
| 47 | print '(A)', SNIFFLY_NAME // " version " // SNIFFLY_VERSION |
| 48 | stop 0 |
| 49 | end if |
| 50 | |
| 51 | ! Validate directory path |
| 52 | if (index(trim(arg), '--') == 1) then |
| 53 | if (has_terminal) then |
| 54 | print '(A)', "ERROR: Unknown option: " // trim(arg) |
| 55 | print '(A)', "Try 'sniffly --help' for more information" |
| 56 | end if |
| 57 | stop 1 |
| 58 | end if |
| 59 | |
| 60 | ! Expand relative paths to absolute paths |
| 61 | abs_path = get_absolute_path(trim(arg)) |
| 62 | scan_dir = abs_path |
| 63 | |
| 64 | ! Check if directory exists |
| 65 | if (.not. is_directory(scan_dir)) then |
| 66 | if (has_terminal) print '(A)', "ERROR: Not a valid directory: " // trim(scan_dir) |
| 67 | stop 1 |
| 68 | end if |
| 69 | |
| 70 | if (has_terminal) print '(A,A)', "Directory to scan: ", trim(scan_dir) |
| 71 | call sniffly_set_scan_path(scan_dir) |
| 72 | else |
| 73 | ! No directory specified - use home directory |
| 74 | call get_environment_variable("HOME", home_dir) |
| 75 | if (len_trim(home_dir) == 0) then |
| 76 | home_dir = "/Users" ! Fallback for macOS |
| 77 | end if |
| 78 | scan_dir = trim(home_dir) |
| 79 | if (has_terminal) then |
| 80 | print '(A)', "No directory specified, starting with home directory" |
| 81 | print '(A,A)', "Directory to scan: ", trim(scan_dir) |
| 82 | end if |
| 83 | call sniffly_set_scan_path(scan_dir) |
| 84 | end if |
| 85 | |
| 86 | ! Run the GTK application (enters main loop) |
| 87 | exit_status = sniffly_app_run() |
| 88 | |
| 89 | ! Exit with appropriate status |
| 90 | if (exit_status /= 0) then |
| 91 | if (has_terminal) print '(A,I0)', "ERROR: Application exited with status ", exit_status |
| 92 | stop 1 |
| 93 | end if |
| 94 | |
| 95 | if (has_terminal) then |
| 96 | print '(A)', "" |
| 97 | print '(A)', "Sniffly closed successfully. Goodbye!" |
| 98 | end if |
| 99 | |
| 100 | contains |
| 101 | |
| 102 | subroutine print_usage() |
| 103 | print '(A)', "" |
| 104 | print '(A)', "Usage: sniffly [OPTIONS] [DIRECTORY]" |
| 105 | print '(A)', "" |
| 106 | print '(A)', "A fast, visual disk space analyzer built with Fortran and GTK4" |
| 107 | print '(A)', "" |
| 108 | print '(A)', "Options:" |
| 109 | print '(A)', " -h, --help Show this help message and exit" |
| 110 | print '(A)', " -v, --version Show version information and exit" |
| 111 | print '(A)', "" |
| 112 | print '(A)', "Arguments:" |
| 113 | print '(A)', " DIRECTORY Directory to analyze (default: home directory)" |
| 114 | print '(A)', "" |
| 115 | print '(A)', "Examples:" |
| 116 | print '(A)', " sniffly # Start with home directory" |
| 117 | print '(A)', " sniffly /Users/username # Analyze specific directory" |
| 118 | print '(A)', " sniffly . # Analyze current directory" |
| 119 | print '(A)', "" |
| 120 | end subroutine print_usage |
| 121 | |
| 122 | end program sniffly_main |
| 123 |