-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
52 lines (43 loc) · 1.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import React from 'react';
import PropTypes from 'prop-types';
import blacklist from 'blacklist';
import {Text} from 'ink';
const BLACKLIST_PROPS = ['percent', 'left', 'right', 'columns', 'character', 'rightPad'];
class Bar extends React.Component {
getString() {
const {percent, columns, left, right, character, rightPad} = this.props;
const screen = columns || process.stdout.columns || 80;
const space = screen - right - left;
const max = Math.min(Math.floor(space * percent), space);
const chars = character.repeat(max);
if (!rightPad) {
return chars;
}
return chars + ' '.repeat(space - max);
}
render() {
const props = blacklist(this.props, BLACKLIST_PROPS);
return (
<Text {...props}>
{this.getString()}
</Text>
);
}
}
Bar.defaultProps = {
columns: 0,
percent: 1,
left: 0,
right: 0,
character: '█',
rightPad: false
};
Bar.propTypes = {
columns: PropTypes.number,
percent: PropTypes.number,
left: PropTypes.number,
right: PropTypes.number,
character: PropTypes.string,
rightPad: PropTypes.bool
};
export default Bar;