Passed
Push — master ( a49b74...edc40c )
by Pavel
02:03
created

formatLine.ts ➔ formatName   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
export default function formatLine(name: string, total_seconds: number, percent: number) {
2
  return [
3
    cutStr(formatName(name), 10).padEnd(12),
4
    convertSeconds(total_seconds).padEnd(11),
5
    generateBarChart(percent, 21),
6
    String(percent.toFixed(1)).padStart(5) + '%',
7
  ].join(' ');
8
}
9
10
function convertSeconds(seconds: number) {
11
  const days = Math.floor(seconds / (24 * 60 * 60));
12
  seconds -= days * (24 * 60 * 60);
13
  const hours = Math.floor(seconds / (60 * 60));
14
  seconds -= hours * (60 * 60);
15
  const minutes = Math.floor(seconds / (60));
16
  seconds -= minutes * (60);
17
18
  seconds = Math.round(seconds);
19
  const result = '🕓 ';
20
21
  if (days > 0) {
22
    return result + days + 'd ' + hours + 'h';
23
  }
24
  if (hours > 0) {
25
    return result + hours + 'h ' + minutes + 'm';
26
  }
27
  if (minutes > 0) {
28
    return result + minutes + 'm ' + seconds + 's';
29
  }
30
  return result + seconds + 's';
31
}
32
33
function cutStr(str: string, len: number) {
34
  return str.length > len ? str.substring(0, len - 3) + '...' : str;
35
}
36
37
function formatName(name: string) {
38
  if (name === 'Blade Template') {
39
    return 'Blade';
40
  }
41
  return name;
42
}
43
44
function generateBarChart(percent: number, size: number) {
45
  const syms = '░▏▎▍▌▋▊▉█';
46
47
  const frac = Math.floor((size * 8 * percent) / 100);
48
  const barsFull = Math.floor(frac / 8);
49
  if (barsFull >= size) {
50
    return syms.substring(8, 9).repeat(size);
51
  }
52
  const semi = frac % 8;
53
54
  return [syms.substring(8, 9).repeat(barsFull), syms.substring(semi, semi + 1)]
55
    .join('').padEnd(size, syms.substring(0, 1));
56
}