~indicator-multiload/indicator-multiload/trunk

« back to all changes in this revision

Viewing changes to src/utils.vala

  • Committer: mh21 at mh21
  • Date: 2014-06-14 18:42:15 UTC
  • Revision ID: mh21@mh21.de-20140614184215-to6u9g06ynm3e50j
Provide bitrate function to show speed in bit/s

Show diffs side-by-side

added added

removed removed

Lines of Context:
93
93
        return pattern.printf(val);
94
94
    }
95
95
 
 
96
    public string format_bitrate(double val) {
 
97
        const string[] units = {
 
98
            // TRANSLATORS: Please leave {} as it is, it is replaced by the bitrate
 
99
            N_("{} kbit/s"),
 
100
            // TRANSLATORS: Please leave {} as it is, it is replaced by the bitrate
 
101
            N_("{} Mbit/s"),
 
102
            // TRANSLATORS: Please leave {} as it is, it is replaced by the bitrate
 
103
            N_("{} Gbit/s"),
 
104
            // TRANSLATORS: Please leave {} as it is, it is replaced by the bitrate
 
105
            N_("{} Tbit/s")
 
106
        };
 
107
        int index = -1;
 
108
        while (index + 1 < units.length && (val >= 1000 || index < 0)) {
 
109
            val /= 1000;
 
110
            ++index;
 
111
        }
 
112
        if (index < 0)
 
113
            // TRANSLATORS: Please leave %u as it is, it is replaced by the bit rate
 
114
            return ngettext("%u bit/s", "%u bit/s",
 
115
                    (ulong)val).printf((uint)val);
 
116
        // 4 significant digits
 
117
        var pattern = _(units[index]).replace("{}",
 
118
            val <   9.95 ? "%.1f" :
 
119
            val <  99.5  ? "%.0f" :
 
120
            val < 999.5  ? "%.0f" : "%.0f");
 
121
        return pattern.printf(val);
 
122
    }
 
123
 
96
124
    public string format_frequency(double val) {
97
125
        const string[] units = {
98
126
            // TRANSLATORS: Please leave {} as it is, it is replaced by the frequency