| 1 | ! Save prompt module |
| 2 | ! Prompts user to save/backup files on quit |
| 3 | |
| 4 | module save_prompt_module |
| 5 | implicit none |
| 6 | private |
| 7 | |
| 8 | public :: save_prompt, save_prompt_result_t |
| 9 | |
| 10 | type :: save_prompt_result_t |
| 11 | character :: action = 'c' ! 's' = save, 'd' = discard (backup), 'a' = all, 'c' = cancel |
| 12 | end type save_prompt_result_t |
| 13 | |
| 14 | contains |
| 15 | |
| 16 | !> Prompt user to save a file |
| 17 | !> Returns 's' to save, 'd' to discard (backup), 'a' to save all, 'c' to cancel quit |
| 18 | subroutine save_prompt(filename, result, current_index, total_count) |
| 19 | use terminal_io_module, only: terminal_write, terminal_move_cursor, terminal_clear_screen |
| 20 | use input_handler_module, only: get_key_input |
| 21 | character(len=*), intent(in) :: filename |
| 22 | type(save_prompt_result_t), intent(out) :: result |
| 23 | integer, intent(in), optional :: current_index, total_count |
| 24 | character(len=512) :: prompt_text, basename |
| 25 | character(len=32) :: key_input |
| 26 | integer :: status, i, slash_pos |
| 27 | logical :: show_all_option |
| 28 | |
| 29 | ! Extract basename from filename |
| 30 | basename = filename |
| 31 | slash_pos = 0 |
| 32 | do i = len_trim(filename), 1, -1 |
| 33 | if (filename(i:i) == '/') then |
| 34 | slash_pos = i |
| 35 | exit |
| 36 | end if |
| 37 | end do |
| 38 | if (slash_pos > 0 .and. slash_pos < len_trim(filename)) then |
| 39 | basename = filename(slash_pos+1:) |
| 40 | end if |
| 41 | |
| 42 | ! Determine if we should show the [a]ll option |
| 43 | show_all_option = .false. |
| 44 | if (present(current_index) .and. present(total_count)) then |
| 45 | if (current_index < total_count) then |
| 46 | show_all_option = .true. |
| 47 | end if |
| 48 | end if |
| 49 | |
| 50 | ! Clear screen and show prompt |
| 51 | call terminal_clear_screen() |
| 52 | call terminal_move_cursor(3, 1) |
| 53 | |
| 54 | ! Build prompt with progress if available |
| 55 | if (present(current_index) .and. present(total_count)) then |
| 56 | write(prompt_text, '(A,I0,A,I0,A,A)') 'Unsaved changes [', current_index, & |
| 57 | ' of ', total_count, ']: ', trim(basename) |
| 58 | else |
| 59 | write(prompt_text, '(A,A)') 'Unsaved changes: ', trim(basename) |
| 60 | end if |
| 61 | call terminal_write(trim(prompt_text)) |
| 62 | |
| 63 | call terminal_move_cursor(5, 1) |
| 64 | call terminal_write('[s]ave - Save changes') |
| 65 | |
| 66 | call terminal_move_cursor(6, 1) |
| 67 | call terminal_write('[d]iscard - Don''t save (backup will be created for recovery)') |
| 68 | |
| 69 | if (show_all_option) then |
| 70 | call terminal_move_cursor(7, 1) |
| 71 | call terminal_write('[a]ll - Save all remaining unsaved files') |
| 72 | |
| 73 | call terminal_move_cursor(8, 1) |
| 74 | call terminal_write('[c]ancel - Cancel quit and return to editing') |
| 75 | |
| 76 | call terminal_move_cursor(10, 1) |
| 77 | else |
| 78 | call terminal_move_cursor(7, 1) |
| 79 | call terminal_write('[c]ancel - Cancel quit and return to editing') |
| 80 | |
| 81 | call terminal_move_cursor(9, 1) |
| 82 | end if |
| 83 | call terminal_write('Choice: ') |
| 84 | |
| 85 | ! Get user input |
| 86 | result%action = 'c' ! Default to cancel |
| 87 | do |
| 88 | call get_key_input(key_input, status) |
| 89 | if (status == 0) then |
| 90 | if (key_input == 's' .or. key_input == 'S') then |
| 91 | result%action = 's' |
| 92 | exit |
| 93 | else if (key_input == 'd' .or. key_input == 'D') then |
| 94 | result%action = 'd' |
| 95 | exit |
| 96 | else if (show_all_option .and. (key_input == 'a' .or. key_input == 'A')) then |
| 97 | result%action = 'a' |
| 98 | exit |
| 99 | else if (key_input == 'c' .or. key_input == 'C') then |
| 100 | result%action = 'c' |
| 101 | exit |
| 102 | else if (key_input == 'ESCAPE') then |
| 103 | result%action = 'c' ! ESC = cancel |
| 104 | exit |
| 105 | end if |
| 106 | end if |
| 107 | end do |
| 108 | end subroutine save_prompt |
| 109 | |
| 110 | end module save_prompt_module |
| 111 |