diff options
author | Federico Igne <undyamon@disroot.org> | 2024-01-26 20:32:44 +0100 |
---|---|---|
committer | Federico Igne <undyamon@disroot.org> | 2024-01-26 20:32:44 +0100 |
commit | 855aa94f49ae2a8cc3c496365197e496bcebf7ce (patch) | |
tree | 5dd05a60f55d1eff1f9b2ffaefa2651f4143460a | |
parent | af0cd851fec5038faff5a8133cadad6c65568374 (diff) | |
download | sandy-855aa94f49ae2a8cc3c496365197e496bcebf7ce.tar.gz sandy-855aa94f49ae2a8cc3c496365197e496bcebf7ce.zip |
refactor: remove welcome screen
-rw-r--r-- | lib/editor.ml | 67 |
1 files 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 = { | |||
16 | mode : mode; | 16 | mode : mode; |
17 | offset : int * int; | 17 | offset : int * int; |
18 | cursor : cursor; | 18 | cursor : cursor; |
19 | buffer : Buffer.t option; | 19 | buffer : Buffer.t; |
20 | rendered : bool; | 20 | rendered : bool; |
21 | istream : Command.t Sequence.t; | 21 | istream : Command.t Sequence.t; |
22 | nstream : Command.t Sequence.t; | 22 | nstream : Command.t Sequence.t; |
@@ -36,12 +36,15 @@ let init (c : Config.t) : editor = | |||
36 | mode = Normal; | 36 | mode = Normal; |
37 | offset = (0, 0); | 37 | offset = (0, 0); |
38 | cursor = (1, 1); | 38 | cursor = (1, 1); |
39 | buffer = List.hd c.files |> Option.map ~f:Buffer.from_file; | 39 | buffer = |
40 | List.hd c.files | ||
41 | |> Option.map ~f:Buffer.from_file | ||
42 | |> Option.value ~default:Buffer.empty; | ||
40 | rendered = true; | 43 | rendered = true; |
41 | istream = Command.i_stream; | 44 | istream = Command.i_stream; |
42 | nstream = Command.n_stream; | 45 | nstream = Command.n_stream; |
43 | status_size = 2; | 46 | status_size = 2; |
44 | message = Some "Hello, control line!"; | 47 | message = Some "Welcome to the sandy editor!"; |
45 | message_timestamp = Unix.time (); | 48 | message_timestamp = Unix.time (); |
46 | message_duration = 5.; | 49 | message_duration = 5.; |
47 | pending_command = ""; | 50 | pending_command = ""; |
@@ -59,20 +62,10 @@ let statusbar e = | |||
59 | let w = e.term.size |> snd in | 62 | let w = e.term.size |> snd in |
60 | let status = | 63 | let status = |
61 | let mode = e.mode |> string_of_mode |> sequence_of_string in | 64 | let mode = e.mode |> string_of_mode |> sequence_of_string in |
62 | let msize = Sequence.length mode | 65 | let lsize = Sequence.length mode |
63 | and path = | 66 | and c = e.buffer.kind |> Buffer.string_of_kind |> sequence_of_string |
64 | Buffer.( | 67 | and br, bc = Buffer.size e.buffer |
65 | e.buffer |> Option.map ~f:kind | 68 | and cr, cc = Buffer.cursor ~rendered:false e.buffer in |
66 | |> Option.value ~default:No_name | ||
67 | |> string_of_kind |> sequence_of_string) | ||
68 | and br, bc = | ||
69 | Option.(e.buffer |> map ~f:Buffer.size |> value ~default:(0, 0)) | ||
70 | and cr, cc = | ||
71 | Option.( | ||
72 | e.buffer | ||
73 | |> map ~f:(Buffer.cursor ~rendered:false) | ||
74 | |> value ~default:(0, 0)) | ||
75 | in | ||
76 | let perc = | 69 | let perc = |
77 | match cr with | 70 | match cr with |
78 | | 0 -> "Top" | 71 | | 0 -> "Top" |
@@ -83,10 +76,8 @@ let statusbar e = | |||
83 | Printf.sprintf "%d/%d %2d/%2d [%s] " cr br cc bc perc | 76 | Printf.sprintf "%d/%d %2d/%2d [%s] " cr br cc bc perc |
84 | |> sequence_of_string | 77 | |> sequence_of_string |
85 | in | 78 | in |
86 | let nsize = Sequence.length nav in | 79 | let rsize = Sequence.length nav in |
87 | spread ~l:(bold mode) ~lsize:msize ~c:path ~r:(bold nav) ~rsize:nsize | 80 | spread ~l:(bold mode) ~lsize ~c ~r:(bold nav) ~rsize ~fill:' ' w |> invert |
88 | ~fill:' ' w | ||
89 | |> invert | ||
90 | and control = | 81 | and control = |
91 | let msg = Option.value ~default:"" e.message |> sequence_of_string | 82 | let msg = Option.value ~default:"" e.message |> sequence_of_string |
92 | and cmd = e.pending_command |> sequence_of_string in | 83 | and cmd = e.pending_command |> sequence_of_string in |
@@ -135,19 +126,18 @@ module Action = struct | |||
135 | let dx, dy = e.offset and rs, cs = e.term.size in | 126 | let dx, dy = e.offset and rs, cs = e.term.size in |
136 | (* Limit cursor to buffer view *) | 127 | (* Limit cursor to buffer view *) |
137 | let rs = rs - e.status_size in | 128 | let rs = rs - e.status_size in |
138 | match Option.map ~f:Buffer.cursor e.buffer with | 129 | let cx, cy = Buffer.cursor e.buffer in |
139 | | None -> { e with cursor = (1, 1); offset = (0, 0) } | 130 | let dx' = Int.clamp_exn ~min:(cx - rs + 1) ~max:cx dx |
140 | | Some (cx, cy) -> | 131 | and dy' = Int.clamp_exn ~min:(cy - cs + 1) ~max:cy dy in |
141 | let dx' = Int.clamp_exn ~min:(cx - rs + 1) ~max:cx dx | 132 | { e with cursor = (cx - dx' + 1, cy - dy' + 1); offset = (dx', dy') } |
142 | and dy' = Int.clamp_exn ~min:(cy - cs + 1) ~max:cy dy in | ||
143 | { e with cursor = (cx - dx' + 1, cy - dy' + 1); offset = (dx', dy') } | ||
144 | in | 133 | in |
145 | modify ~f:aux | 134 | modify ~f:aux |
146 | 135 | ||
136 | let get_focused_buffer e = (e.buffer, e) | ||
137 | let set_focused_buffer b e = ((), { e with buffer = b }) | ||
138 | |||
147 | let get_mode e = (e.mode, e) | 139 | let get_mode e = (e.mode, e) |
148 | let set_mode m e = ((), { e with mode = m }) | 140 | let set_mode m e = ((), { e with mode = m }) |
149 | let get_focused_buffer e = (e.buffer, e) | ||
150 | let set_focused_buffer b e = ((), { e with buffer = Some b }) | ||
151 | let get_terminal_size e = (e.term.size, e) | 141 | let get_terminal_size e = (e.term.size, e) |
152 | 142 | ||
153 | let get_register r e = | 143 | let get_register r e = |
@@ -171,28 +161,21 @@ module Action = struct | |||
171 | *> update_cursor | 161 | *> update_cursor |
172 | 162 | ||
173 | let render = | 163 | let render = |
174 | let welcome (r, c) = | ||
175 | let open Text in | ||
176 | let hfill = ' ' and vfill = Sequence.empty in | ||
177 | "Welcome to the sandy editor!" |> String.to_list |> Sequence.of_list | ||
178 | |> center ~fill:hfill c |> Sequence.singleton |> center ~fill:vfill r | ||
179 | in | ||
180 | let aux e = | 164 | let aux e = |
181 | let x, y = e.offset | 165 | let x, y = e.offset |
182 | and ((r, c) as size) = e.term.size | 166 | and r, c = e.term.size |
183 | and fill = Sequence.singleton '~' | 167 | and fill = Sequence.singleton '~' |
184 | and status = statusbar e | 168 | and status = statusbar e |
185 | and limit = | 169 | and limit = |
170 | (* debug *) | ||
186 | Buffer.(if e.rendered then rendered_view else unrendered_view) | 171 | Buffer.(if e.rendered then rendered_view else unrendered_view) |
187 | in | 172 | in |
188 | let ssize = e.status_size in | 173 | let ssize = e.status_size in |
189 | let bufview = | 174 | let bufview = |
190 | Option.( | 175 | e.buffer |
191 | e.buffer | 176 | |> limit x y (r - ssize) c |
192 | >>| limit x y (r - ssize) c | 177 | |> Text.extend ~fill r |
193 | |> value ~default:(welcome size) | 178 | |> Fn.flip Sequence.take (r - ssize) |
194 | |> Text.extend ~fill r | ||
195 | |> Fn.flip Sequence.take (r - ssize)) | ||
196 | in | 179 | in |
197 | let screen = Sequence.append bufview status in | 180 | let screen = Sequence.append bufview status in |
198 | Terminal.redraw screen e.cursor | 181 | Terminal.redraw screen e.cursor |