fortrangoingonforty/fortty / abacd9a

Browse files

Integrate configuration system in main program

Load configuration at startup from ~/.config/fortty/fortty.toml:
- Apply color palette and default colors to cell module
- Use window dimensions from config
- Pass font path and size to renderer
- Use font metrics for cell dimensions instead of hardcoded values

Add config modules to CMakeLists.txt build.
Includes response handling in main loop for DA1/DSR replies.
Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
abacd9aa9e0618275a55343f493231438e2b709c
Parents
d768056
Tree
4fa580e

2 changed files

StatusFile+-
M CMakeLists.txt 2 0
M src/fortty.f90 77 32
CMakeLists.txtmodified
@@ -55,6 +55,8 @@ set(FORTRAN_SOURCES
5555
     src/text/atlas.f90
5656
     src/text/renderer.f90
5757
     src/terminal/cell.f90
58
+    src/config/toml_parser.f90
59
+    src/config/config.f90
5860
     src/terminal/screen.f90
5961
     src/terminal/cursor.f90
6062
     src/terminal/scrollback.f90
src/fortty.f90modified
@@ -7,7 +7,8 @@ program fortty
77
   use terminal_mod
88
   use parser_mod
99
   use screen_mod
10
-  use cell_mod
10
+  use cell_mod, only: cell_t, set_palette_color, set_default_colors
11
+  use config_mod
1112
   implicit none
1213
 
1314
   type(window_t) :: win
@@ -17,40 +18,56 @@ program fortty
1718
   type(parser_t) :: parser
1819
   type(screen_t), pointer :: scr
1920
   type(cell_t) :: cell
21
+  type(config_t) :: cfg
2022
   integer :: win_width, win_height
2123
   integer :: prev_width, prev_height
2224
   integer :: term_rows, term_cols
2325
   integer :: new_rows, new_cols
2426
   character(len=256) :: font_path, fallback_path
2527
   character(len=4096) :: pty_buffer
28
+  character(len=256) :: response_buf
2629
   integer :: nbytes, i, row, col, scroll_offset, sb_offset, screen_row
27
-  real :: x, y, r, g, b
30
+  integer :: response_len
31
+  real :: x, y, r, g, b, bg_r, bg_g, bg_b
2832
   type(cell_t), allocatable :: sb_line(:)
29
-  integer, parameter :: CELL_WIDTH = 10   ! Approximate char width
30
-  integer, parameter :: CELL_HEIGHT = 18  ! Approximate line height
33
+  integer :: cell_width, cell_height  ! From font metrics
3134
 
32
-  ! Window dimensions
33
-  win_width = 800
34
-  win_height = 600
35
+  ! Load configuration (uses defaults if no config file found)
36
+  cfg = config_load('')
37
+
38
+  ! Apply color palette from config
39
+  call set_default_colors(cfg%fg_color, cfg%bg_color)
40
+  do i = 0, 15
41
+    call set_palette_color(i, cfg%palette(i))
42
+  end do
43
+
44
+  ! Window dimensions from config
45
+  win_width = cfg%window_width
46
+  win_height = cfg%window_height
3547
 
3648
   ! Create window with OpenGL context
3749
   win = window_create(win_width, win_height, "fortty")
3850
 
39
-  ! Font path - use fontconfig for portable discovery, with fallbacks
40
-  font_path = font_find_monospace()
41
-  if (len_trim(font_path) == 0) then
42
-    ! Fontconfig not available or failed - try common system locations
43
-    font_path = "/usr/share/fonts/TTF/DejaVuSansMono.ttf"
51
+  ! Font path - use config if specified, otherwise fontconfig, then fallbacks
52
+  if (len_trim(cfg%font_path) > 0) then
53
+    font_path = cfg%font_path
54
+    print *, "Using configured font: ", trim(font_path)
4455
   else
45
-    print *, "Using system monospace font: ", trim(font_path)
56
+    font_path = font_find_monospace()
57
+    if (len_trim(font_path) == 0) then
58
+      ! Fontconfig not available or failed - try common system locations
59
+      font_path = "/usr/share/fonts/TTF/DejaVuSansMono.ttf"
60
+    else
61
+      print *, "Using system monospace font: ", trim(font_path)
62
+    end if
4663
   end if
4764
 
48
-  ! Create renderer with font
49
-  ren = renderer_create(trim(font_path), 16)
65
+  ! Create renderer with font (using font size from config)
66
+  ren = renderer_create(trim(font_path), cfg%font_size)
5067
   if (.not. ren%initialized) then
5168
     print *, "Warning: Could not load font, trying alternate path..."
5269
     font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"
53
-    ren = renderer_create(trim(font_path), 16)
70
+    ren = renderer_create(trim(font_path), cfg%font_size)
5471
   end if
5572
 
5673
   if (.not. ren%initialized) then
@@ -65,11 +82,21 @@ program fortty
6582
   ren%atlas%font => ren%font
6683
 
6784
   ! Load fallback font for missing glyphs (icons, symbols, etc.)
68
-  ! First try fontconfig to find a font with Nerd Font icons (e.g., eza uses these)
69
-  ! Try Nerd Font-specific devicons first (U+E5FF), then common icons (U+F07B)
70
-  fallback_path = font_find_for_codepoint(int(z'E5FF'))  ! Nerd Font devicon
71
-  if (len_trim(fallback_path) > 0) then
72
-    call renderer_load_fallback_font(ren, trim(fallback_path))
85
+  ! Use config fallback if specified, otherwise auto-detect via fontconfig
86
+  if (len_trim(cfg%font_fallback) > 0) then
87
+    call renderer_load_fallback_font(ren, trim(cfg%font_fallback))
88
+    if (ren%font%has_fallback) then
89
+      print *, "Using configured fallback font: ", trim(cfg%font_fallback)
90
+    end if
91
+  end if
92
+
93
+  ! If no configured fallback or it failed, try fontconfig auto-detection
94
+  if (.not. ren%font%has_fallback) then
95
+    ! Try Nerd Font-specific devicons first (U+E5FF), then common icons (U+F07B)
96
+    fallback_path = font_find_for_codepoint(int(z'E5FF'))  ! Nerd Font devicon
97
+    if (len_trim(fallback_path) > 0) then
98
+      call renderer_load_fallback_font(ren, trim(fallback_path))
99
+    end if
73100
   end if
74101
   if (.not. ren%font%has_fallback) then
75102
     fallback_path = font_find_for_codepoint(int(z'E0A0'))  ! Powerline branch symbol
@@ -112,9 +139,16 @@ program fortty
112139
   ! Set up projection matrix
113140
   call renderer_set_projection(ren, win_width, win_height)
114141
 
142
+  ! Get cell dimensions from font metrics
143
+  cell_width = ren%font%cell_width
144
+  cell_height = ren%font%cell_height
145
+  if (cell_width < 1) cell_width = 10  ! Fallback
146
+  if (cell_height < 1) cell_height = 18  ! Fallback
147
+  print *, "Font cell size:", cell_width, "x", cell_height
148
+
115149
   ! Calculate terminal dimensions based on font metrics
116
-  term_cols = win_width / CELL_WIDTH
117
-  term_rows = win_height / CELL_HEIGHT
150
+  term_cols = win_width / cell_width
151
+  term_rows = win_height / cell_height
118152
   prev_width = win_width
119153
   prev_height = win_height
120154
 
@@ -149,8 +183,8 @@ program fortty
149183
       call renderer_set_projection(ren, win_width, win_height)
150184
 
151185
       ! Calculate new terminal size and notify PTY and terminal
152
-      new_cols = win_width / CELL_WIDTH
153
-      new_rows = win_height / CELL_HEIGHT
186
+      new_cols = win_width / cell_width
187
+      new_rows = win_height / cell_height
154188
       if (new_cols /= term_cols .or. new_rows /= term_rows) then
155189
         term_cols = new_cols
156190
         term_rows = new_rows
@@ -168,8 +202,19 @@ program fortty
168202
       end do
169203
     end if
170204
 
171
-    ! Clear screen with dark gray background
172
-    call glClearColor(0.1, 0.1, 0.12, 1.0)
205
+    ! Check for terminal responses (DA1, DSR, etc.) and send to PTY
206
+    if (terminal_has_response(term)) then
207
+      call terminal_get_response(term, response_buf, response_len)
208
+      if (response_len > 0) then
209
+        call pty_write(pty, response_buf, response_len)
210
+      end if
211
+    end if
212
+
213
+    ! Clear screen with background color from config
214
+    bg_r = real(cfg%bg_color%r) / 255.0
215
+    bg_g = real(cfg%bg_color%g) / 255.0
216
+    bg_b = real(cfg%bg_color%b) / 255.0
217
+    call glClearColor(bg_r, bg_g, bg_b, 1.0)
173218
     call glClear(GL_COLOR_BUFFER_BIT)
174219
 
175220
     ! Render terminal buffer
@@ -187,7 +232,7 @@ program fortty
187232
     end if
188233
 
189234
     do row = 1, scr%rows
190
-      y = real(row) * CELL_HEIGHT
235
+      y = real(row) * cell_height
191236
 
192237
       ! Determine if this row shows scrollback or screen content
193238
       sb_offset = scroll_offset - row + 1
@@ -198,7 +243,7 @@ program fortty
198243
         do col = 1, min(term_cols, scr%cols)
199244
           cell = sb_line(col)
200245
           if (cell%codepoint /= 32) then
201
-            x = real(col - 1) * CELL_WIDTH
246
+            x = real(col - 1) * cell_width
202247
             r = real(cell%fg%r) / 255.0
203248
             g = real(cell%fg%g) / 255.0
204249
             b = real(cell%fg%b) / 255.0
@@ -212,7 +257,7 @@ program fortty
212257
           do col = 1, scr%cols
213258
             cell = screen_get_cell(scr, screen_row, col)
214259
             if (cell%codepoint /= 32) then
215
-              x = real(col - 1) * CELL_WIDTH
260
+              x = real(col - 1) * cell_width
216261
               r = real(cell%fg%r) / 255.0
217262
               g = real(cell%fg%g) / 255.0
218263
               b = real(cell%fg%b) / 255.0
@@ -225,8 +270,8 @@ program fortty
225270
 
226271
     ! Draw cursor if visible (only when not scrolled back)
227272
     if (term%cursor%visible .and. scroll_offset == 0) then
228
-      x = real(term%cursor%col - 1) * CELL_WIDTH
229
-      y = real(term%cursor%row) * CELL_HEIGHT
273
+      x = real(term%cursor%col - 1) * cell_width
274
+      y = real(term%cursor%row) * cell_height
230275
       ! Draw cursor as underscore character for visibility
231276
       call renderer_draw_char(ren, x, y, 95, 0.7, 0.7, 0.7, 1.0)  ! '_'
232277
     end if