~indicator-multiload/indicator-multiload/trunk

« back to all changes in this revision

Viewing changes to src/utils.vala

  • Committer: Michael Hofmann
  • Date: 2011-05-06 21:09:48 UTC
  • Revision ID: mh21@piware.de-20110506210948-d5vomat1j0pbxcoq
Removed most of the unowned stuff, preliminary optimization. Menu text for all graphs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
            result += v;
36
36
        return result / data.length;
37
37
    }
 
38
 
 
39
    // for SI units as g_format_size_for_display uses base 2
 
40
    public string format_size(double val) {
 
41
        const string[] units = {
 
42
            // TRANSLATORS: Please leave {} as it is, it is replaced by the size
 
43
            N_("{} kB"),
 
44
            // TRANSLATORS: Please leave {} as it is, it is replaced by the size
 
45
            N_("{} MB"),
 
46
            // TRANSLATORS: Please leave {} as it is, it is replaced by the size
 
47
            N_("{} GB")
 
48
        };
 
49
        int index = -1;
 
50
        while (index + 1 < units.length && val >= 1000) {
 
51
            val /= 1000;
 
52
            ++index;
 
53
        }
 
54
        if (index < 0)
 
55
            return ngettext("%u byte", "%u bytes", (ulong)val).printf((uint)val);
 
56
        // 4 significant digits
 
57
        var pattern = _(units[index]).replace("{}",
 
58
            val < 9.9995 ? "%.3f" :
 
59
            val < 99.995 ? "%.2f" :
 
60
            val < 999.95 ? "%.1f" : "%.0f");
 
61
        return pattern.printf(val);
 
62
    }
 
63
 
 
64
    public string format_speed(double val) {
 
65
        const string[] units = {
 
66
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
 
67
            N_("{} kB/s"),
 
68
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
 
69
            N_("{} MB/s"),
 
70
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
 
71
            N_("{} GB/s"),
 
72
            // TRANSLATORS: Please leave {} as it is, it is replaced by the speed
 
73
            N_("{} TB/s")
 
74
        };
 
75
        int index = -1;
 
76
        while (index + 1 < units.length && val >= 1000) {
 
77
            val /= 1000;
 
78
            ++index;
 
79
        }
 
80
        if (index < 0)
 
81
            return ngettext("%u byte/s", "%u bytes/s", (ulong)val).printf((uint)val);
 
82
        // 4 significant digits
 
83
        var pattern = _(units[index]).replace("{}",
 
84
            val < 9.9995 ? "%.3f" :
 
85
            val < 99.995 ? "%.2f" :
 
86
            val < 999.95 ? "%.1f" : "%.0f");
 
87
        return pattern.printf(val);
 
88
    }
 
89
 
38
90
}