5
* Copyright 2016 Tony George <teejee2008@gmail.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25
namespace TeeJee.Misc {
27
/* Various utility functions */
31
using TeeJee.FileSystem;
32
using TeeJee.ProcessHelper;
34
// color format -------------------
36
public static Gdk.RGBA hex_to_rgba (string hex_color){
38
/* Converts the color in hex to RGBA */
40
string hex = hex_color.strip().down();
41
if (hex.has_prefix("#") == false){
45
Gdk.RGBA color = Gdk.RGBA();
46
if(color.parse(hex) == false){
47
color.parse("#000000");
54
public static string rgba_to_hex (Gdk.RGBA color, bool alpha = false, bool prefix_hash = true){
56
/* Converts the color in RGBA to hex */
61
hex = "%02x%02x%02x%02x".printf((uint)(Math.round(color.red*255)),
62
(uint)(Math.round(color.green*255)),
63
(uint)(Math.round(color.blue*255)),
64
(uint)(Math.round(color.alpha*255)))
68
hex = "%02x%02x%02x".printf((uint)(Math.round(color.red*255)),
69
(uint)(Math.round(color.green*255)),
70
(uint)(Math.round(color.blue*255)))
81
// localization --------------------
83
public void set_numeric_locale(string type){
84
Intl.setlocale(GLib.LocaleCategory.NUMERIC, type);
85
Intl.setlocale(GLib.LocaleCategory.COLLATE, type);
86
Intl.setlocale(GLib.LocaleCategory.TIME, type);
89
// timestamp ----------------
91
public string timestamp (){
93
/* Returns a formatted timestamp string */
95
Time t = Time.local (time_t ());
96
return t.format ("%H:%M:%S");
99
public string timestamp_numeric (){
101
/* Returns a numeric timestamp string */
103
return "%ld".printf((long) time_t ());
106
public string timestamp_for_path (){
108
/* Returns a formatted timestamp string */
110
Time t = Time.local (time_t ());
111
return t.format ("%Y-%d-%m_%H-%M-%S");
114
// string formatting -------------------------------------------------
116
public string format_duration (long millis){
118
/* Converts time in milliseconds to format '00:00:00.0' */
120
double time = millis / 1000.0; // time in seconds
122
double hr = Math.floor(time / (60.0 * 60));
123
time = time - (hr * 60 * 60);
124
double min = Math.floor(time / 60.0);
125
time = time - (min * 60);
126
double sec = Math.floor(time);
128
return "%02.0lf:%02.0lf:%02.0lf".printf (hr, min, sec);
131
public double parse_time (string time){
133
/* Converts time in format '00:00:00.0' to milliseconds */
135
string[] arr = time.split (":");
137
if (arr.length >= 3){
138
millis += double.parse(arr[0]) * 60 * 60;
139
millis += double.parse(arr[1]) * 60;
140
millis += double.parse(arr[2]);
145
public string string_replace(string str, string search, string replacement, int count = -1){
146
string[] arr = str.split(search);
150
foreach(string part in arr){
160
new_txt += replacement;
171
public string escape_html(string html){
173
.replace("&","&")
174
.replace("\"",""")
175
//.replace(" "," ") //pango markup throws an error with
181
public string unescape_html(string html){
183
.replace("&","&")
184
.replace(""","\"")
185
//.replace(" "," ") //pango markup throws an error with
191
public string uri_encode(string path, bool encode_forward_slash){
192
string uri = Uri.escape_string(path);
193
if (!encode_forward_slash){
194
uri = uri.replace("%2F","/");
199
public string uri_decode(string path){
200
return Uri.unescape_string(path);
203
public DateTime datetime_from_string (string date_time_string){
205
/* Converts date time string to DateTime
211
* 'yyyy-MM-dd HH:mm:ss'
214
string[] arr = date_time_string.replace(":"," ").replace("-"," ").strip().split(" ");
216
int year = (arr.length >= 3) ? int.parse(arr[0]) : 0;
217
int month = (arr.length >= 3) ? int.parse(arr[1]) : 0;
218
int day = (arr.length >= 3) ? int.parse(arr[2]) : 0;
219
int hour = (arr.length >= 4) ? int.parse(arr[3]) : 0;
220
int min = (arr.length >= 5) ? int.parse(arr[4]) : 0;
221
int sec = (arr.length >= 6) ? int.parse(arr[5]) : 0;
223
return new DateTime.utc(year,month,day,hour,min,sec);
226
public string break_string_by_word(string input_text){
229
foreach(string part in input_text.split(" ")){
231
if (line.length > 50){
232
text += line.strip() + "\n";
236
if (line.length > 0){
239
if (text.has_suffix("\n")){
240
text = text[0:text.length-1].strip();
245
public string[] array_concat(string[] a, string[] b){
247
foreach(string str in a){ c += str; }
248
foreach(string str in b){ c += str; }
252
public string random_string(int length = 8, string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"){
255
for(int i=0;i<length;i++){
256
int random_index = Random.int_range(0,charset.length);
257
string ch = charset.get_char(charset.index_of_nth_char(random_index)).to_string();
264
private string pad_numbers_in_string(
265
string input, int max_length = 3, char pad_char = '0'){
267
string sequence = "";
269
bool seq_started = false;
273
for (int i = 0; input.get_next_char(ref i, out c);) {
274
character = c.to_string();
277
sequence += character;
282
if ((max_length - sequence.length) > 0){
283
sequence = string.nfill(max_length - sequence.length, pad_char) + sequence;
294
//append remaining characters in sequence
295
if (sequence.length > 0){
296
if ((max_length - sequence.length) > 0){
297
sequence = string.nfill(max_length - sequence.length, pad_char) + sequence;