Skip to content

Commit 94db687

Browse files
committed
update examples
1 parent 8b64f99 commit 94db687

12 files changed

+41
-33
lines changed

examples/asciiart/asciiart.ml

+11-11
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ open CamomileLibrary
6464
class asciiart img = object(self)
6565
inherit t "asciiart" as super
6666

67-
method can_focus = true
67+
method! can_focus = true
6868

6969
(* scrollable interfaces *)
7070
val vscroll = new scrollable
@@ -81,7 +81,7 @@ class asciiart img = object(self)
8181
vscroll#set_document_size self#document_size.rows;
8282
hscroll#set_document_size self#document_size.cols
8383

84-
method set_allocation r =
84+
method! set_allocation r =
8585
super#set_allocation r;
8686
let size = size_of_rect r in
8787
vscroll#set_page_size size.rows;
@@ -98,13 +98,13 @@ class asciiart img = object(self)
9898
| Some(r, c, i) when r = !avg_rows && c = !avg_cols -> i
9999
| _ -> stored_img <- Some(!avg_rows, !avg_cols, indices img); self#img
100100

101-
method draw ctx focused =
101+
method! draw ctx _focused =
102102
let { rows; cols } = LTerm_draw.size ctx in
103103
let img = self#img in
104104
for row=0 to rows-1 do
105105
for col=0 to cols-1 do
106106
LTerm_draw.draw_char ~style ctx row col @@
107-
UChar.of_char palette.[
107+
Zed_char.unsafe_of_char palette.[
108108
try img.(row + vscroll#offset).(col + hscroll#offset) with _ -> 0
109109
]
110110
done
@@ -126,29 +126,29 @@ class asciiart img = object(self)
126126

127127
(* adjust scale, which changes the document size *)
128128
method private scale_event = function
129-
| LTerm_event.Key{LTerm_key.code=LTerm_key.Char c} when c = UChar.of_char 'w' ->
129+
| LTerm_event.Key{LTerm_key.code=LTerm_key.Char c;_} when c = UChar.of_char 'w' ->
130130
avg_rows := max 1 (!avg_rows - 1);
131131
vscroll#set_document_size self#document_size.rows;
132132
self#queue_draw; true
133-
| LTerm_event.Key{LTerm_key.code=LTerm_key.Char c} when c = UChar.of_char 's' ->
133+
| LTerm_event.Key{LTerm_key.code=LTerm_key.Char c;_} when c = UChar.of_char 's' ->
134134
avg_rows := !avg_rows + 1;
135135
vscroll#set_document_size self#document_size.rows;
136136
self#queue_draw; true
137-
| LTerm_event.Key{LTerm_key.code=LTerm_key.Char c} when c = UChar.of_char 'a' ->
137+
| LTerm_event.Key{LTerm_key.code=LTerm_key.Char c;_} when c = UChar.of_char 'a' ->
138138
avg_cols := max 1 (!avg_cols - 1);
139139
hscroll#set_document_size self#document_size.cols;
140140
self#queue_draw; true
141-
| LTerm_event.Key{LTerm_key.code=LTerm_key.Char c} when c = UChar.of_char 'd' ->
141+
| LTerm_event.Key{LTerm_key.code=LTerm_key.Char c;_} when c = UChar.of_char 'd' ->
142142
avg_cols := !avg_cols + 1;
143143
hscroll#set_document_size self#document_size.cols;
144144
self#queue_draw; true
145145
| _ -> false
146146

147147
(* page up/down *)
148148
method page_event = function
149-
| LTerm_event.Key{LTerm_key.code=LTerm_key.Next_page} ->
149+
| LTerm_event.Key{LTerm_key.code=LTerm_key.Next_page;_} ->
150150
vscroll#set_offset @@ vscroll#page_next; self#queue_draw; true
151-
| LTerm_event.Key{LTerm_key.code=LTerm_key.Prev_page} ->
151+
| LTerm_event.Key{LTerm_key.code=LTerm_key.Prev_page;_} ->
152152
vscroll#set_offset @@ vscroll#page_prev; self#queue_draw; true
153153
| _ -> false
154154

@@ -202,7 +202,7 @@ let main () =
202202
top#set vbox;
203203

204204
top#on_event (function (* quit with escape key *)
205-
| LTerm_event.Key{LTerm_key.code=LTerm_key.Escape} ->
205+
| LTerm_event.Key{LTerm_key.code=LTerm_key.Escape;_} ->
206206
wakeup wakener (); false
207207
| _ -> false);
208208

examples/buttons.ml

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ let main () =
1414
let waiter, wakener = wait () in
1515

1616
let vbox = new vbox in
17-
let button = new button ~brackets:("[ "," ]") "exit" in
17+
let button = new button
18+
~brackets:("[ ", " ]")
19+
"exit退出"
20+
in
1821
let label = new label "_" in
1922
button#on_click (wakeup wakener);
2023
vbox#add button;
@@ -23,7 +26,7 @@ let main () =
2326
for i = 0 to 2 do
2427
let hbox = new hbox in
2528
let button i =
26-
let button = new button ("button" ^ string_of_int i) in
29+
let button = new button ("button按钮" ^ string_of_int i) in
2730
button#on_click (fun () -> label#set_text (string_of_int i));
2831
button
2932
in
@@ -38,7 +41,7 @@ let main () =
3841

3942
let frame = new frame in
4043
frame#set vbox;
41-
frame#set_label ~alignment:LTerm_geom.H_align_center "Button test";
44+
frame#set_label ~alignment:LTerm_geom.H_align_center "Button test按钮测试";
4245

4346
Lazy.force LTerm.stdout >>= fun term ->
4447
LTerm.enable_mouse term >>= fun () ->

examples/checkbuttons.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ let main () =
1414
let waiter, wakener = wait () in
1515

1616
let vbox = new vbox in
17-
let checked_label = (new label "") in
18-
let create_button n = new checkbutton ("button" ^ (string_of_int n)) false in
17+
let checked_label = new label "" in
18+
let create_button n = new checkbutton ("button按钮" ^ (string_of_int n)) false in
1919
let checkbuttons = Array.init 9 create_button in
2020
let callback () =
2121
let new_label = ref "" in
@@ -26,7 +26,7 @@ let main () =
2626
done;
2727
checked_label#set_text !new_label
2828
in
29-
let button = new button "exit" in
29+
let button = new button "exit退出" in
3030
button#on_click (wakeup wakener);
3131
vbox#add ~expand:false button;
3232
vbox#add ~expand:false (new hline);

examples/clock.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let main () =
2222

2323
let vbox = new vbox in
2424
let clock = new label (get_time ()) in
25-
let button = new button "exit" in
25+
let button = new button "exit退出" in
2626
vbox#add clock;
2727
vbox#add button;
2828

examples/editor.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ let main () =
3232
[ LTerm_edit.Custom (fun () -> wakeup wakener ()) ];
3333

3434
Zed_edit.insert editor#context
35-
(Zed_rope.of_string "\
35+
(Zed_rope.of_string @@ Zed_string_UTF8.to_t_exn "\
3636
This is a simple edition widget.
3737
3838
Type C-x C-c to exit.

examples/hello.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ let main () =
1616
(* Create the UI. *)
1717
let vbox = new LTerm_widget.vbox in
1818
vbox#add (new LTerm_widget.label "Hello, world!");
19-
vbox#add (new LTerm_widget.label "Press escape to exit.");
19+
vbox#add (new LTerm_widget.label "你好,世界!");
20+
vbox#add (new LTerm_widget.label "ハロー・ワールド");
21+
vbox#add (new LTerm_widget.label "안녕, 세계!");
2022
vbox#on_event (function
2123
| LTerm_event.Key { LTerm_key.code = LTerm_key.Escape; _ } -> wakeup wakener (); true
2224
| _ -> false);

examples/move.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ let draw ui matrix coord =
3939
let size = LTerm_ui.size ui in
4040
let ctx = LTerm_draw.context matrix size in
4141
LTerm_draw.clear ctx;
42-
LTerm_draw.draw_frame_labelled ctx { row1 = 0; col1 = 0; row2 = size.rows; col2 = size.cols } ~alignment:H_align_center "Use arrow keys to move text" LTerm_draw.Light;
42+
LTerm_draw.draw_frame_labelled ctx { row1 = 0; col1 = 0; row2 = size.rows; col2 = size.cols } ~alignment:H_align_center (Zed_string_UTF8.to_t_exn "Use arrow keys to move text 文字") LTerm_draw.Light;
4343
if size.rows > 2 && size.cols > 2 then begin
4444
let ctx = LTerm_draw.sub ctx { row1 = 1; col1 = 1; row2 = size.rows - 1; col2 = size.cols - 1 } in
45-
LTerm_draw.draw_styled ctx coord.row coord.col (eval [B_fg LTerm_style.lblue; S"Move me"; E_fg])
45+
LTerm_draw.draw_styled ctx coord.row coord.col (eval [B_fg LTerm_style.lblue; S "Move m̀é 囧"; E_fg])
4646
end
4747

4848
let main () =

examples/read_password.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let ( >>= ) = Lwt.( >>= )
1515

1616
class read_password term = object(self)
1717
inherit LTerm_read_line.read_password () as super
18-
inherit [Zed_utf8.t] LTerm_read_line.term term
18+
inherit [Zed_string.t] LTerm_read_line.term term
1919

2020
method! send_action = function
2121
| LTerm_read_line.Break ->
@@ -25,7 +25,7 @@ class read_password term = object(self)
2525
super#send_action action
2626

2727
initializer
28-
self#set_prompt (S.const (LTerm_text.of_string "Type a password: "))
28+
self#set_prompt (S.const (LTerm_text.of_utf8 "Type a password: "))
2929
end
3030

3131
let main () =
@@ -35,6 +35,6 @@ let main () =
3535
>>= fun term ->
3636
(new read_password term)#run
3737
>>= fun password ->
38-
Lwt_io.printlf "You typed %S" password
38+
Lwt_io.printlf "You typed %S" (Zed_string_UTF8.of_t password)
3939

4040
let () = Lwt_main.run (main ())

examples/repl.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ let make_output state out =
4848

4949
class read_line ~term ~history ~state = object(self)
5050
inherit LTerm_read_line.read_line ~history ()
51-
inherit [Zed_utf8.t] LTerm_read_line.term term
51+
inherit [Zed_string.t] LTerm_read_line.term term
5252

5353
method! show_box = false
5454

@@ -62,13 +62,14 @@ end
6262

6363
let rec loop term history state =
6464
Lwt.catch (fun () ->
65-
let rl = new read_line ~term ~history:(LTerm_history.contents history) ~state in
65+
let rl = new read_line ~term ~history:(LTerm_history.zed_contents history) ~state in
6666
rl#run >|= fun command -> Some command)
6767
(function
6868
| Sys.Break -> return None
6969
| exn -> Lwt.fail exn)
7070
>>= function
7171
| Some command ->
72+
let command= Zed_string_UTF8.of_t command in
7273
let state, out = Interpreter.eval state command in
7374
LTerm.fprintls term (make_output state out)
7475
>>= fun () ->

examples/scroll.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class scrollable_nums (scroll : scrollable) = object
2222
let { rows; _ } = LTerm_draw.size ctx in
2323

2424
for row=0 to rows-1 do
25-
LTerm_draw.draw_string ctx row 0 (string_of_int (row + scroll#offset))
25+
LTerm_draw.draw_string ctx row 0 (Zed_string_UTF8.to_t_exn (string_of_int (row + scroll#offset)))
2626
done
2727

2828
end

examples/scroll_debug.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class scroll_label scroll = object
2020
method! draw ctx _focused =
2121
LTerm_draw.fill_style ctx style;
2222
LTerm_draw.draw_string_aligned ctx 0 H_align_center ~style
23-
(Printf.sprintf "%i/%i" scroll#offset scroll#range)
23+
(Zed_string_UTF8.to_t_exn (Printf.sprintf "%i/%i" scroll#offset scroll#range))
2424

2525
end
2626

examples/shell.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ let get_binaries () =
114114
String_set.empty
115115
(get_paths ())
116116
>|= String_set.elements
117+
>|= List.map Zed_string_UTF8.to_t_exn
117118

118119
(* +-----------------------------------------------------------------+
119120
| Customization of the read-line engine |
@@ -128,12 +129,12 @@ let time =
128129

129130
class read_line ~term ~history ~exit_code ~binaries = object(self)
130131
inherit LTerm_read_line.read_line ~history ()
131-
inherit [Zed_utf8.t] LTerm_read_line.term term
132+
inherit [Zed_string.t] LTerm_read_line.term term
132133

133134
method! completion =
134135
let prefix = Zed_rope.to_string self#input_prev in
135-
let binaries = List.filter (fun file -> Zed_utf8.starts_with file prefix) binaries in
136-
self#set_completion 0 (List.map (fun file -> (file, " ")) binaries)
136+
let binaries = List.filter (fun file -> Zed_string.starts_with ~prefix file) binaries in
137+
self#set_completion 0 (List.map (fun file -> (file, Zed_string_UTF8.to_t_exn " ")) binaries)
137138

138139
initializer
139140
self#set_prompt (S.l2 (fun size time -> make_prompt size exit_code time) self#size time)
@@ -147,14 +148,15 @@ let rec loop term history exit_code =
147148
get_binaries ()
148149
>>= fun binaries ->
149150
Lwt.catch (fun () ->
150-
(new read_line ~term ~history:(LTerm_history.contents history)
151+
(new read_line ~term ~history:(LTerm_history.zed_contents history)
151152
~exit_code ~binaries)#run
152153
>|= fun command -> Some command)
153154
(function
154155
| Sys.Break -> return None
155156
| exn -> Lwt.fail exn)
156157
>>= function
157158
| Some command ->
159+
let command= Zed_string_UTF8.of_t command in
158160
Lwt.catch (fun () -> Lwt_process.exec (Lwt_process.shell command))
159161
(function
160162
| Unix.Unix_error (Unix.ENOENT, _, _) ->

0 commit comments

Comments
 (0)