Rewrite virt-resize in OCaml.
[libguestfs.git] / resize / progress.ml
1 (* virt-resize
2  * Copyright (C) 2010-2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 open Printf
20
21 open Utils
22
23 module G = Guestfs
24
25 let set_up_progress_bar (g : Guestfs.guestfs) =
26   let progress_callback g event evh buf array =
27     if event = G.EVENT_PROGRESS && Array.length array >= 4 then (
28       (*let proc_nr = array.(0)
29       and serial = array.(1)*)
30       let position = array.(2)
31       and total = array.(3) in
32
33       let ratio =
34         if total <> 0L then Int64.to_float position /. Int64.to_float total
35         else 0. in
36       let ratio =
37         if ratio < 0. then 0. else if ratio > 1. then 1. else ratio in
38
39       let dots = int_of_float (ratio *. 72.) in
40
41       print_string "[";
42       for i = 0 to dots-1 do print_char '#' done;
43       for i = dots to 71 do print_char '-' done;
44       print_string "]\r";
45       if ratio = 1. then print_string "\n";
46       flush stdout
47     )
48   in
49   ignore (g#set_event_callback progress_callback [G.EVENT_PROGRESS])