From 8ccef920564ce7068ccdbc07b32c8e1bc68d1025 Mon Sep 17 00:00:00 2001 From: cqc Date: Tue, 14 May 2024 21:46:03 -0500 Subject: [PATCH] nasty bug in scroll update i couldn't wrap my head around --- ogui.ml | 78 +++++++++++++++++++++++++++++++++++++++--------------- oplevel.ml | 10 ++++--- 2 files changed, 63 insertions(+), 25 deletions(-) diff --git a/ogui.ml b/ogui.ml index e382f4d..02b93ee 100644 --- a/ogui.ml +++ b/ogui.ml @@ -5,6 +5,7 @@ module Str = Re.Str type stroke = { width : float; color : Gv.Color.t } +let debugsec = ref false let stroke_none = { width = 0.; color = Gv.Color.transparent } let pp_text_row : Gv.Text.text_row F.t = @@ -456,7 +457,7 @@ module TextLayout = struct field "byte_range" (fun s -> s.byte_range) (pair ~sep:(any ",") int int); - field "format" (fun s -> s.format) pp_format; + (* field "format" (fun s -> s.format) pp_format; *) ]) let section_default = @@ -536,6 +537,9 @@ module TextLayout = struct List.fold_left (fun (l : section list) sec -> let s, e = sec.byte_range in + if !debugsec then + F.epr "with_range section cs=%d ce=%d s=%d e=%d@." cs ce + s e; l @ (if @@ -570,6 +574,10 @@ module TextLayout = struct let with_cursor (cur : cursor) ?(format = default_cursor_formatter) layout : layout = let c = with_range (cur.index, cur.index + 1) ~format layout in + if !debugsec then + F.epr "with_cursor %a@." + F.(brackets @@ list ~sep:cut pp_section) + c.sections; c let with_mark (mark : int option) (cur : int) @@ -776,27 +784,49 @@ module TextEdit = struct | Some start' -> if rows - 1 > 0 then index_rows_from s (start' + 1) (rows - 1) else Some (start' + 1) - | None -> None + | None -> None (* eof *) + + let rec rindex_rows_from (s : string) (start : int) (rows : int) : + int option = + match String.rindex_from_opt s start '\n' with + | Some start' -> + if start' - 1 <= 0 then None + else if rows - 1 > 0 then + rindex_rows_from s (start' - 1) (rows - 1) + else Some (start' + 1) + | None -> None (* eof *) let scroll_update ({ text; cursor; scroll; rows; _ } as t : t) : unit Lwt.t = - F.epr "scroll_update cursor=%d scroll=%d rows=%d" cursor.index - scroll rows; TextBuffer.fold_string text (fun s -> + let slen = String.length s in (if cursor.index < scroll then - match String.rindex_from_opt s cursor.index '\n' with + match + String.rindex_from_opt s + (min (slen - 1) (cursor.index - 1)) + '\n' + with | Some i' -> t.scroll <- i' + 1 | None -> t.scroll <- 0 else match index_rows_from s scroll rows with - | None -> (* eof *) () + | None -> () | Some eow -> ( - F.epr " eow=%d" eow; - if cursor.index > eow then - match String.index_from_opt s cursor.index '\n' with + F.epr "eow=%d@." eow; + if cursor.index >= eow then + match + rindex_rows_from s + (min (slen - 1) cursor.index) + rows + with | None -> () - | Some i' -> t.scroll <- i' + 1)); - F.epr "@.") + | Some i' -> t.scroll <- i')); + F.epr + "scroll_update slen=%d cursor=%d scroll=%d c-s=%d rows=%d@." + (String.length s) t.cursor.index t.scroll + (cursor.index - t.scroll) + rows; + debugsec := true) let cursor_update (t : t) (f : int -> int) : unit Lwt.t = col t >>= fun last_col -> @@ -877,19 +907,19 @@ module TextEdit = struct Str.search_backward (Str.regexp "^") s in let bol = sbol t.cursor.index in - if bol > 0 then ( + if bol > 0 then let prev_bol = sbol (max 0 (bol - 1)) in let prev_line_len = bol - 1 - prev_bol in - F.epr + + (*F.epr "up: index=%d bol=%d prev_bol=%d \ prev_line_len=%d @." - t.cursor.index bol prev_bol prev_line_len; - + t.cursor.index bol prev_bol prev_line_len; *) prev_bol + if t.cursor.last_col > prev_line_len then prev_line_len - else min prev_line_len t.cursor.last_col) + else min prev_line_len t.cursor.last_col else t.cursor.index) >>= cursor_set t); ] @@ -1149,22 +1179,26 @@ module Painter = struct let contents_len = String.length contents in let row_count = Gv.Text.break_lines t ~break_width:(Box2.w rect) ~max_rows - ~lines contents + ~lines ~start:te.scroll contents in Seq.fold_left - (fun ((cur, next) : p2 * int) (row : Gv.Text.text_row) -> - F.epr "text_layout row=%a@." pp_text_row row; + (fun ((cur, start) : p2 * int) (row : Gv.Text.text_row) -> + if !debugsec then + F.epr "start=%d row=%a %a @." start pp_text_row row + F.(brackets @@ list TextLayout.pp_section) + g.sections; + debugsec := false; let sections = List.filter (fun (r : TextLayout.section) -> fst r.byte_range <= row.end_index - && snd r.byte_range > next) + && snd r.byte_range > start) g.sections in List.fold_left (fun (cur' : p2) (sec : TextLayout.section) -> let start, end_ = - ( next |> max (fst sec.byte_range) |> min contents_len, + ( start |> max (fst sec.byte_range) |> min contents_len, row.end_index |> min contents_len |> min (snd sec.byte_range) ) in @@ -1199,7 +1233,7 @@ module Painter = struct sections |> fun cur'' -> (V2.(v (max (x cur) (x cur'')) (y cur'')), row.next)) - (Box2.o rect, 0) + (Box2.o rect, te.scroll) (Seq.take row_count (Array.to_seq lines)) |> fst |> Box2.(of_pts (o rect)) diff --git a/oplevel.ml b/oplevel.ml index 44b17db..2a0c8ba 100644 --- a/oplevel.ml +++ b/oplevel.ml @@ -109,9 +109,9 @@ let () = (TextBuffer.of_repo ~path:[ ".config"; "init.ml" ] ~repo:rootrepo)); - textedit + (*textedit (TextEdit.multiline ui - (TextBuffer.of_repo ~path:[ "README" ] ~repo:rootrepo)); + (TextBuffer.of_repo ~path:[ "README" ] ~repo:rootrepo)); *) ]) in @@ -153,7 +153,11 @@ let () = Gl.enable Gl.cull_face_enum; Gl.disable Gl.depth_test; - let width, height = (float win_w, float win_h) in + let width, height = + ( float win_w, + float (win_h - 50) + (* TKTK might be an artifact of exwm or something *) ) + in let box = Gg.(Box2.v V2.zero Size2.(v width height)) in Gv.begin_frame ctx ~width ~height ~device_ratio:1.; Perfgraph.render graph ctx (width -. 205.) 5.;