Fortran · 3316 bytes Raw Blame History
1 ! Binary file prompt module
2 ! Prompts user when attempting to open binary files
3
4 module binary_prompt_module
5 implicit none
6 private
7
8 public :: binary_file_prompt
9
10 contains
11
12 !> Prompt user about opening a binary file
13 !> Returns .true. to open in hex view, .false. to cancel
14 function binary_file_prompt(filename) result(open_anyway)
15 use terminal_io_module, only: terminal_write, terminal_move_cursor, terminal_clear_screen
16 use input_handler_module, only: get_key_input
17 character(len=*), intent(in) :: filename
18 logical :: open_anyway
19 character(len=512) :: prompt_text, basename
20 character(len=32) :: key_input
21 integer :: status, i, slash_pos
22
23 open_anyway = .false.
24
25 ! Extract basename from filename
26 basename = filename
27 slash_pos = 0
28 do i = len_trim(filename), 1, -1
29 if (filename(i:i) == '/') then
30 slash_pos = i
31 exit
32 end if
33 end do
34 if (slash_pos > 0 .and. slash_pos < len_trim(filename)) then
35 basename = filename(slash_pos+1:)
36 end if
37
38 ! Clear screen and show warning
39 call terminal_clear_screen()
40 call terminal_move_cursor(3, 1)
41
42 prompt_text = '═══════════════════════' // &
43 '═══════════════════════'
44 call terminal_write(trim(prompt_text))
45
46 call terminal_move_cursor(4, 1)
47 call terminal_write(' WARNING: Binary File Detected')
48
49 call terminal_move_cursor(5, 1)
50 prompt_text = '═══════════════════════' // &
51 '═══════════════════════'
52 call terminal_write(trim(prompt_text))
53
54 call terminal_move_cursor(7, 1)
55 write(prompt_text, '(A,A)') 'File: ', trim(basename)
56 call terminal_write(trim(prompt_text))
57
58 call terminal_move_cursor(9, 1)
59 call terminal_write('This file appears to be a binary file (contains null bytes).')
60
61 call terminal_move_cursor(10, 1)
62 call terminal_write('Binary files like .mod, .o, .a, executables, and images')
63
64 call terminal_move_cursor(11, 1)
65 call terminal_write('cannot be edited as text.')
66
67 call terminal_move_cursor(13, 1)
68 call terminal_write('Options:')
69
70 call terminal_move_cursor(15, 1)
71 call terminal_write('[v]iew - Open in hex viewer mode (read-only)')
72
73 call terminal_move_cursor(16, 1)
74 call terminal_write('[c]ancel - Return to editor')
75
76 call terminal_move_cursor(18, 1)
77 call terminal_write('Choice: ')
78
79 ! Get user input
80 do
81 call get_key_input(key_input, status)
82 if (status == 0) then
83 if (key_input == 'v' .or. key_input == 'V') then
84 open_anyway = .true.
85 exit
86 else if (key_input == 'c' .or. key_input == 'C' .or. key_input == 'esc') then
87 open_anyway = .false.
88 exit
89 end if
90 end if
91 end do
92 end function binary_file_prompt
93
94 end module binary_prompt_module
95