From 855aa94f49ae2a8cc3c496365197e496bcebf7ce Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 26 Jan 2024 20:32:44 +0100 Subject: refactor: remove welcome screen --- lib/editor.ml | 67 ++++++++++++++++++++++------------------------------------- 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/lib/editor.ml b/lib/editor.ml index 0ed2229..8241c58 100644 --- a/lib/editor.ml +++ b/lib/editor.ml @@ -16,7 +16,7 @@ type editor = { mode : mode; offset : int * int; cursor : cursor; - buffer : Buffer.t option; + buffer : Buffer.t; rendered : bool; istream : Command.t Sequence.t; nstream : Command.t Sequence.t; @@ -36,12 +36,15 @@ let init (c : Config.t) : editor = mode = Normal; offset = (0, 0); cursor = (1, 1); - buffer = List.hd c.files |> Option.map ~f:Buffer.from_file; + buffer = + List.hd c.files + |> Option.map ~f:Buffer.from_file + |> Option.value ~default:Buffer.empty; rendered = true; istream = Command.i_stream; nstream = Command.n_stream; status_size = 2; - message = Some "Hello, control line!"; + message = Some "Welcome to the sandy editor!"; message_timestamp = Unix.time (); message_duration = 5.; pending_command = ""; @@ -59,20 +62,10 @@ let statusbar e = let w = e.term.size |> snd in let status = let mode = e.mode |> string_of_mode |> sequence_of_string in - let msize = Sequence.length mode - and path = - Buffer.( - e.buffer |> Option.map ~f:kind - |> Option.value ~default:No_name - |> string_of_kind |> sequence_of_string) - and br, bc = - Option.(e.buffer |> map ~f:Buffer.size |> value ~default:(0, 0)) - and cr, cc = - Option.( - e.buffer - |> map ~f:(Buffer.cursor ~rendered:false) - |> value ~default:(0, 0)) - in + let lsize = Sequence.length mode + and c = e.buffer.kind |> Buffer.string_of_kind |> sequence_of_string + and br, bc = Buffer.size e.buffer + and cr, cc = Buffer.cursor ~rendered:false e.buffer in let perc = match cr with | 0 -> "Top" @@ -83,10 +76,8 @@ let statusbar e = Printf.sprintf "%d/%d %2d/%2d [%s] " cr br cc bc perc |> sequence_of_string in - let nsize = Sequence.length nav in - spread ~l:(bold mode) ~lsize:msize ~c:path ~r:(bold nav) ~rsize:nsize - ~fill:' ' w - |> invert + let rsize = Sequence.length nav in + spread ~l:(bold mode) ~lsize ~c ~r:(bold nav) ~rsize ~fill:' ' w |> invert and control = let msg = Option.value ~default:"" e.message |> sequence_of_string and cmd = e.pending_command |> sequence_of_string in @@ -135,19 +126,18 @@ module Action = struct let dx, dy = e.offset and rs, cs = e.term.size in (* Limit cursor to buffer view *) let rs = rs - e.status_size in - match Option.map ~f:Buffer.cursor e.buffer with - | None -> { e with cursor = (1, 1); offset = (0, 0) } - | Some (cx, cy) -> - let dx' = Int.clamp_exn ~min:(cx - rs + 1) ~max:cx dx - and dy' = Int.clamp_exn ~min:(cy - cs + 1) ~max:cy dy in - { e with cursor = (cx - dx' + 1, cy - dy' + 1); offset = (dx', dy') } + let cx, cy = Buffer.cursor e.buffer in + let dx' = Int.clamp_exn ~min:(cx - rs + 1) ~max:cx dx + and dy' = Int.clamp_exn ~min:(cy - cs + 1) ~max:cy dy in + { e with cursor = (cx - dx' + 1, cy - dy' + 1); offset = (dx', dy') } in modify ~f:aux + let get_focused_buffer e = (e.buffer, e) + let set_focused_buffer b e = ((), { e with buffer = b }) + let get_mode e = (e.mode, e) let set_mode m e = ((), { e with mode = m }) - let get_focused_buffer e = (e.buffer, e) - let set_focused_buffer b e = ((), { e with buffer = Some b }) let get_terminal_size e = (e.term.size, e) let get_register r e = @@ -171,28 +161,21 @@ module Action = struct *> update_cursor let render = - let welcome (r, c) = - let open Text in - let hfill = ' ' and vfill = Sequence.empty in - "Welcome to the sandy editor!" |> String.to_list |> Sequence.of_list - |> center ~fill:hfill c |> Sequence.singleton |> center ~fill:vfill r - in let aux e = let x, y = e.offset - and ((r, c) as size) = e.term.size + and r, c = e.term.size and fill = Sequence.singleton '~' and status = statusbar e and limit = + (* debug *) Buffer.(if e.rendered then rendered_view else unrendered_view) in let ssize = e.status_size in let bufview = - Option.( - e.buffer - >>| limit x y (r - ssize) c - |> value ~default:(welcome size) - |> Text.extend ~fill r - |> Fn.flip Sequence.take (r - ssize)) + e.buffer + |> limit x y (r - ssize) c + |> Text.extend ~fill r + |> Fn.flip Sequence.take (r - ssize) in let screen = Sequence.append bufview status in Terminal.redraw screen e.cursor -- cgit v1.2.3