Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit ea3dae5983262d1e1703cbe1638431e853f0e68c @mixu committed May 9, 2014
Showing with 254 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +40 −0 bin/tmux-cpu
  3. +42 −0 bin/usage.txt
  4. +71 −0 index.js
  5. +35 −0 package.json
  6. +65 −0 readme.md
@@ -0,0 +1 @@
+node_modules/
@@ -0,0 +1,40 @@
+#!/usr/bin/env node
+var fs = require('fs'),
+ print = require('../index.js'),
+ opts = require('yargs')
+ .options({
+ 'help': { },
+ 'version': { },
+ 'ascii': { },
+ 'format': { 'default': '#[fg=:color][:spark] :load :percent #[default][:spark5] :load5 :percent5 #[fg=black,bold][:spark15] :load15 :percent15' },
+ 'width': { 'default': 5 },
+ 'tty': { 'default': process.stdout.isTTY },
+ 'color': { 'default': true }
+ })
+ .boolean('help')
+ .boolean('version')
+ .boolean('ascii')
+ .boolean('tty')
+ .boolean('color'),
+ argv = opts.parse(process.argv);
+
+var fmt = false;
+
+if(argv['version'] || argv['v'] ) {
+ console.log(require('../package.json').version);
+ process.exit();
+}
+
+if(argv['help']) {
+ return fs.createReadStream(__dirname + '/usage.txt').pipe(process.stdout).on('close', function () { process.exit(1) });
+}
+
+if(argv['ascii']) {
+ argv['format'] = '#[fg=:color][:bar] :load :percent #[default][:bar5] :load5 :percent5 #[fg=black,bold][:bar15] :load15 :percent15';
+}
+
+if(argv['format']) {
+ fmt = argv['format'];
+}
+
+print(fmt, argv['width'], argv['tty'], argv['color']);
@@ -0,0 +1,42 @@
+Usage: tmux-cpu
+
+Options:
+
+ --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
+ '#[fg=:color][:spark] :load :percent #[default][:spark5] :load5 :percent5 #[fg=black,bold][:spark15] :load15 :percent15'.
+
+ You can use these tokens in the custom formatting string:
+
+ - `:load`: Load average for the past minute.
+ - `:load5`: Load average for the past 5 minutes.
+ - `:load15`: Load average for the past 15 minutes.
+ - `:percent`, `:percent5`, `:percent15`: CPU usage % for the past minute.
+ - `:bar`, `:bar5`, `:bar15`: ASCII progress bar, CPU %.
+ - `:spark`, `:spark5`, `:spark15`: the utf-8 spark line graphic
+ - `:color`, `:color5`, `:color15`: a color (red, yellow, green) representing the percentage used (adaptive based on the percentage)
+
+Load percentages are calculated simply by dividing load average by the number of cpus.
+
+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-cpu 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-cpu also converts these strings to the appropriate TTY color codes for the terminal.
@@ -0,0 +1,71 @@
+var os = require('os'),
+ bytes = require('bytes'),
+ spark = require('textspark'),
+ colors = require('tmux-colors');
+
+function escapeRegExp(string) {
+ return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
+}
+
+function bar(opts) {
+ var ratio = opts.current / opts.total,
+ width = opts.width || 10,
+ complete = Array(Math.round(width * ratio)).join('='),
+ incomplete = Array(width - complete.length).join(' ');
+
+ return complete + incomplete;
+}
+
+function replace(result, opts) {
+ Object.keys(opts).sort(function(a, b) {
+ // sort so that we match the longest strings first
+ return b.length - a.length;
+ }).forEach(function(key) {
+ if (key != 'format') {
+ result = result.replace(new RegExp(escapeRegExp(':' + key), 'g'), opts[key]);
+ }
+ });
+ return result;
+}
+
+function active(percent){
+ var color = 'green',
+ colorsList = [ 'red', 'yellow', 'green' ];
+ [ 75, 50, 25 ].some(function(threshold, i) {
+ color = colorsList[i];
+ return percent > threshold;
+ });
+ return color;
+}
+
+module.exports = function(fmt, width, tty, enableColor) {
+ var avg = os.loadavg(),
+ cpus = Object.keys(os.cpus()).length,
+ percentage = avg.map(function(i) {
+ return ((i / cpus) * 100).toFixed(0);
+ });
+
+ avg = avg.map(function(i) {
+ return i.toFixed(2);
+ });
+
+ var opts = {
+ load: avg[0],
+ load5: avg[1],
+ load15: avg[2],
+ percent: percentage[0] + '%',
+ percent5: percentage[1] + '%',
+ percent15: percentage[2] + '%',
+ color: active(percentage[0]),
+ color5: active(percentage[1]),
+ color15: active(percentage[2]),
+ bar: bar({ current: percentage[0], total: 100, width: width }),
+ bar5: bar({ current: percentage[1], total: 100, width: width }),
+ bar15: bar({ current: percentage[2], total: 100, width: width }),
+ spark: spark([0, percentage[0], 100])[1],
+ spark5: spark([0, percentage[1], 100])[1],
+ spark15: spark([0, percentage[2], 100])[1]
+ };
+
+ console.log(colors(replace(fmt, opts), { tty: tty, color: enableColor}));
+};
@@ -0,0 +1,35 @@
+{
+ "name": "tmux-cpu",
+ "version": "0.0.0",
+ "description": "Display memory usage in your tmux status bar or in the terminal",
+ "main": "index.js",
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "bin": {
+ "tmux-cpu": "./bin/tmux-cpu"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/mixu/tmux-cpu.git"
+ },
+ "keywords": [
+ "tmux",
+ "memory",
+ "ram",
+ "ascii",
+ "status"
+ ],
+ "author": "Mikito Takada <mikito.takada@gmail.com> (http://mixu.net/)",
+ "license": "BSD",
+ "bugs": {
+ "url": "https://github.com/mixu/tmux-cpu/issues"
+ },
+ "homepage": "https://github.com/mixu/tmux-cpu",
+ "dependencies": {
+ "bytes": "1.0.x",
+ "textspark": "0.1.x",
+ "yargs": "1.2.x",
+ "tmux-colors": "0.0.1"
+ }
+}
@@ -0,0 +1,65 @@
+# tmux-cpu
+
+Display CPU usage in your tmux status bar or in the terminal.
+
+## Installation
+
+Install Node, then use npm:
+
+ sudo npm install -g tmux-cpu
+
+## Usage
+
+ Usage: tmux-cpu
+
+ Options:
+
+ --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
+ '#[fg=:color][:spark] :load :percent #[default][:spark5] :load5 :percent5 #[fg=black,bold][:spark15] :load15 :percent15'.
+
+ You can use these tokens in the custom formatting string:
+
+ - `:load`: Load average for the past minute.
+ - `:load5`: Load average for the past 5 minutes.
+ - `:load15`: Load average for the past 15 minutes.
+ - `:percent`, `:percent5`, `:percent15`: CPU usage % for the past minute.
+ - `:bar`, `:bar5`, `:bar15`: ASCII progress bar, CPU %.
+ - `:spark`, `:spark5`, `:spark15`: the utf-8 spark line graphic
+ - `:color`, `:color5`, `:color15`: a color (red, yellow, green) representing the percentage used (adaptive based on the percentage)
+
+ Load percentages are calculated simply by dividing load average by the number of cpus.
+
+ 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-cpu 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-cpu 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-cpu)"
+
+reload the tmux config by running tmux source-file ~/.tmux.conf.
+

0 comments on commit ea3dae5

Please sign in to comment.