X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=resize%2Fprogress.ml;h=0ae72a387cf56b6a7d785be20fb945678ff1a22c;hb=c46bedf925cd9c6c9a9cbaee115358fd1dffcbfe;hp=a4eff0fe65406b6d4fc556c1e91790489faa6e63;hpb=ca03635a4c83afbe9b51fe846a8b3d5361462a90;p=libguestfs.git diff --git a/resize/progress.ml b/resize/progress.ml index a4eff0f..0ae72a3 100644 --- a/resize/progress.ml +++ b/resize/progress.ml @@ -22,28 +22,33 @@ open Utils module G = Guestfs -let set_up_progress_bar (g : Guestfs.guestfs) = +type progress_bar +external progress_bar_init : machine_readable:bool -> progress_bar + = "virt_resize_progress_bar_init" +external progress_bar_reset : progress_bar -> unit + = "virt_resize_progress_bar_reset" +external progress_bar_set : progress_bar -> int64 -> int64 -> unit + = "virt_resize_progress_bar_set" + +let set_up_progress_bar ?(machine_readable = false) (g : Guestfs.guestfs) = + (* Initialize the C mini library. *) + let bar = progress_bar_init ~machine_readable in + + (* Reset the progress bar before every libguestfs function. *) + let enter_callback g event evh buf array = + if event = G.EVENT_ENTER then + progress_bar_reset bar + in + + (* A progress event: move the progress bar. *) let progress_callback g event evh buf array = if event = G.EVENT_PROGRESS && Array.length array >= 4 then ( - (*let proc_nr = array.(0) - and serial = array.(1)*) let position = array.(2) and total = array.(3) in - let ratio = - if total <> 0L then Int64.to_float position /. Int64.to_float total - else 0. in - let ratio = - if ratio < 0. then 0. else if ratio > 1. then 1. else ratio in - - let dots = int_of_float (ratio *. 72.) in - - print_string "["; - for i = 0 to dots-1 do print_char '#' done; - for i = dots to 71 do print_char '-' done; - print_string "]\r"; - if ratio = 1. then print_string "\n"; - flush stdout + progress_bar_set bar position total ) in + + ignore (g#set_event_callback enter_callback [G.EVENT_ENTER]); ignore (g#set_event_callback progress_callback [G.EVENT_PROGRESS])