Skip to content

Commit

Permalink
Use tmux-colors for colors, update readme, add color-related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mixu committed May 9, 2014
1 parent 71743c3 commit 8111590
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 23 deletions.
14 changes: 9 additions & 5 deletions bin/tmux-mem
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ var fs = require('fs'),
'help': { },
'version': { },
'ascii': { },
'format': { },
'width': { 'default': 10 }
'format': { 'default': ':currentBytes / :totalBytes [#[fg=:color]:spark#[fg=default]] :percent' },
'width': { 'default': 10 },
'tty': { 'default': process.stdout.isTTY },
'color': { 'default': true }
})
.boolean('help')
.boolean('version')
.boolean('ascii'),
.boolean('ascii')
.boolean('tty')
.boolean('color'),
argv = opts.parse(process.argv);

var fmt = false;
Expand All @@ -26,11 +30,11 @@ if(argv['help']) {
}

if(argv['ascii']) {
fmt = ':currentBytes / :totalBytes [:bar] :percent';
argv['format'] = ':currentBytes / :totalBytes [#[fg=:color]:bar#[fg=default]] :percent';
}

if(argv['format']) {
fmt = argv['format'];
}

print(fmt, argv['width']);
print(fmt, argv['width'], argv['tty'], argv['color']);
21 changes: 19 additions & 2 deletions bin/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ Usage: tmux-mem

Options:

--ascii Display ascii percentage bar ([====== ] instead of [▆])
--ascii Display ASCII percentage bar ([====== ] instead of [▆])
--width <int> The width of the ASCII bar, default: 10.
--format <str> Use a custom formatting string.
--no-color Disable colors.
--help Show help.
--version Show version.

Custom formatting:

The default formatting string is ':currentBytes / :totalBytes [:spark] :percent'.
The default formatting string is
':currentBytes / :totalBytes [#[fg=:color]:spark#[fg=default]] :percent'.

You can use these tokens in the custom formatting string:

Expand All @@ -22,4 +24,19 @@ Custom formatting:
- `:total`: the number of bytes (raw)
- `:totalBytes`: the number of bytes (with b/kb/mb/gb/tb postfix)
- `:percent`: the percentage of memory used
- `:color`: the current bar color (adaptive, based on the percentage)

Colors in the format string:

tmux uses a custom format for specifying colors, which is different from the set of codes used in the terminal. For compatibility, tmux-mem also uses the same format: #[attributes]

where attributes are a comma-separated list of 'fg=color' and 'bg=color', for example:

#[fg=yellow,bold]Yellow bold#[default] Gray

Attributes may a comma-delimited list of one or more of: bright (or bold), dim, underscore, blink, reverse, hidden, or italics.

Color may be one of: black, red, green, yellow, blue, magenta,
cyan, white, default, colour0 to colour255. Newer tmux versions also support RGB strings such as #ffffff. See `man tmux` for more info.

tmux-mem also converts these strings to the appropriate TTY color codes for the terminal.
24 changes: 11 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var os = require('os'),
bytes = require('bytes'),
spark = require('textspark'),
chalk = require('chalk');

colors = require('tmux-colors');

function bar(opts) {
var ratio = opts.current / opts.total,
Expand All @@ -24,33 +23,32 @@ function bar(opts) {
return result;
}

module.exports = function(fmt, width) {
if (!fmt) {
fmt = ':currentBytes / :totalBytes [:spark] :percent';
}

module.exports = function(fmt, width, tty, enableColor) {
var free = os.freemem(),
total = os.totalmem(),
used = total - free,
percent = (used / total) * 100,
color,
colors = [ 'red', 'yellow', 'green' ];
colorsList = [ 'red', 'yellow', 'green' ];

[ 75, 50, 25 ].some(function(threshold, i) {
color = colors[i];
color = colorsList[i];
return percent > threshold;
})
});

console.log(
bar({
colors(bar({
format: fmt,
current: used,
currentBytes: bytes(used),
total: total,
totalBytes: bytes(total),
spark: chalk[color]( spark([0, percent, 100])[1]),
spark: spark([0, percent, 100])[1],
color: color,
percent: percent.toFixed(0) + '%',
width: width || 10
}));
}), { tty: tty || process.stdout.isTTY,
color: enableColor })
);

};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"homepage": "https://github.com/mixu/tmux-mem",
"dependencies": {
"bytes": "^1.0.0",
"chalk": "^0.4.0",
"textspark": "^0.1.0",
"tmux-colors": "0.0.0",
"yargs": "^1.2.1"
}
}
22 changes: 20 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ Install Node, then use npm:
--ascii Display ASCII percentage bar ([====== ] instead of [▆])
--width <int> The width of the ASCII bar, default: 10.
--format <str> Use a custom formatting string.
--no-color Disable colors.
--help Show help.
--version Show version.

Custom formatting:

The default formatting string is ':currentBytes / :totalBytes [:spark] :percent'.
The default formatting string is
':currentBytes / :totalBytes [#[fg=:color]:spark#[fg=default]] :percent'.

You can use these tokens in the custom formatting string:

Expand All @@ -33,14 +35,30 @@ Install Node, then use npm:
- `:total`: the number of bytes (raw)
- `:totalBytes`: the number of bytes (with b/kb/mb/gb/tb postfix)
- `:percent`: the percentage of memory used
- `:color`: the current bar color (adaptive, based on the percentage)

Colors in the format string:

tmux uses a custom format for specifying colors, which is different from the set of codes used in the terminal. For compatibility, tmux-mem also uses the same format: #[attributes]

where attributes are a comma-separated list of 'fg=color' and 'bg=color', for example:

#[fg=yellow,bold]Yellow bold#[default] Gray

Attributes may a comma-delimited list of one or more of: bright (or bold), dim, underscore, blink, reverse, hidden, or italics.

Color may be one of: black, red, green, yellow, blue, magenta,
cyan, white, default, colour0 to colour255. Newer tmux versions also support RGB strings such as #ffffff. See `man tmux` for more info.

tmux-mem also converts these strings to the appropriate TTY color codes for the terminal.

## Integrating with tmux

Make sure you have enabled utf-8 in the status line, either via `set -g status-utf8 on` in `~/.tmux.conf` or by running tmux with the `-u` flag: `tmux -u`.

Add the following line to your ~/.tmux.conf file:

set -g status-right "#(/usr/local/bin/tmux-mem)"
set -g status-right "#(/usr/local/bin/tmux-mem) %H:%M %d-%b-%y"

reload the tmux config by running tmux source-file ~/.tmux.conf.

0 comments on commit 8111590

Please sign in to comment.