|
| 1 | + |
| 2 | +open Lwt |
| 3 | + |
| 4 | +open LTerm_style |
| 5 | +open LTerm_text |
| 6 | +open LTerm_geom |
| 7 | +open CamomileLibraryDyn.Camomile |
| 8 | +open React |
| 9 | + |
| 10 | +let make_prompt size time = |
| 11 | + let tm = Unix.localtime time in |
| 12 | + let exit_code = 10 in |
| 13 | + |
| 14 | + eval [ |
| 15 | + B_bold true; |
| 16 | + |
| 17 | + B_fg lcyan; |
| 18 | + S"─( "; |
| 19 | + B_fg lmagenta; S(Printf.sprintf "%02d:%02d" tm.Unix.tm_hour tm.Unix.tm_min); E_fg; |
| 20 | + S" )─< "; |
| 21 | + B_fg lyellow; S "$"; E_fg; |
| 22 | + S" >─"; |
| 23 | + S(Zed_utf8.make |
| 24 | + (size.cols - 23 - Zed_utf8.length "foo") |
| 25 | + (UChar.of_int 0x2500)); |
| 26 | + S"[ "; |
| 27 | + B_fg(if exit_code = 0 then lwhite else lred); S "foo"; E_fg; |
| 28 | + S" ]─"; |
| 29 | + E_fg; |
| 30 | + S"\n"; |
| 31 | + |
| 32 | + B_fg lred; S "user"; E_fg; |
| 33 | + B_fg lgreen; S"@"; E_fg; |
| 34 | + B_fg lblue; S "domain"; E_fg; |
| 35 | + B_fg lgreen; S" $ "; E_fg; |
| 36 | + |
| 37 | + E_bold; |
| 38 | + ] |
| 39 | + |
| 40 | +let commands = |
| 41 | + [ "connect" ; "add" ; "status" ] |
| 42 | + |
| 43 | +let time = |
| 44 | + let time, set_time = S.create (Unix.time ()) in |
| 45 | + (* Update the time every 60 seconds. *) |
| 46 | + ignore (Lwt_engine.on_timer 60.0 true (fun _ -> set_time (Unix.time ()))); |
| 47 | + time |
| 48 | + |
| 49 | +class read_line ~term ~history ~completions = object(self) |
| 50 | + inherit LTerm_read_line.read_line ~history () |
| 51 | + inherit [Zed_utf8.t] LTerm_read_line.term term |
| 52 | + |
| 53 | + method completion = |
| 54 | + let prefix = Zed_rope.to_string self#input_prev in |
| 55 | + let completions = List.filter (fun f -> Zed_utf8.starts_with f prefix) completions in |
| 56 | + self#set_completion 0 (List.map (fun f -> (f, " ")) completions) |
| 57 | + |
| 58 | + initializer |
| 59 | + self#set_prompt (S.l2 (fun size time -> make_prompt size time) self#size time) |
| 60 | +end |
| 61 | + |
| 62 | +let rec loop term history = |
| 63 | + let completions = commands in |
| 64 | + match_lwt |
| 65 | + try_lwt |
| 66 | + lwt command = (new read_line ~term ~history:(LTerm_history.contents history) ~completions)#run in |
| 67 | + return (Some command) |
| 68 | + with Sys.Break -> |
| 69 | + return None |
| 70 | + with |
| 71 | + | Some command -> |
| 72 | + Printf.printf "executing %s\n" command ; |
| 73 | + LTerm_history.add history command; |
| 74 | + loop |
| 75 | + term |
| 76 | + history |
| 77 | + | None -> |
| 78 | + loop term history |
| 79 | + |
| 80 | + |
| 81 | +let () = |
| 82 | + Lwt_main.run ( |
| 83 | + ignore (LTerm_inputrc.load ()); |
| 84 | + (* look for -f command line flag *) |
| 85 | + Lwt_unix.getlogin () >>= fun user -> |
| 86 | + Lwt_unix.getpwnam user >>= fun pw_ent -> |
| 87 | + let cfgdir = |
| 88 | + let home = pw_ent.Lwt_unix.pw_dir in |
| 89 | + Filename.concat home ".config" |
| 90 | + in |
| 91 | + Xmpp_callbacks.init cfgdir >>= fun (config, users) -> |
| 92 | + Printf.printf "your config %s\n%!" (Config.store_config config) ; |
| 93 | + let history = LTerm_history.create [] in |
| 94 | + Lazy.force LTerm.stdout >>= fun term -> |
| 95 | + try_lwt |
| 96 | + loop term history |
| 97 | + with |
| 98 | + LTerm_read_line.Interrupt -> |
| 99 | + ( |
| 100 | + (* dump_config cfgdir !cfg >>= fun () -> |
| 101 | + match !user_data with |
| 102 | + | None -> return () |
| 103 | + | Some x -> dump_users cfgdir x.users *) |
| 104 | + return ()) |
| 105 | + ) |
0 commit comments