# Code coverage report for slap/lib/ui/Header.js

## Statements: 93.15% (68 / 73) Branches: 73.91% (34 / 46) Functions: 80% (8 / 10) Lines: 98.51% (66 / 67) Ignored: none

All files » slap/lib/ui/ » Header.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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    1381
    1
    1

    1
    1
    1

    1
    1

    1
    1

    1

    1

    1

    1
    1
    1

    1

    1

    1

    1

    1
    31
    31
    31
    31
    23

    31

    1
    7

    7
    7
    2

    7
    7

    1
    1
    1
    37

    1
    1

    1
    65

    65
    65
    65
    65
    59
    59

    59
    59

    59

    59
    59
    59
    59

    59

    59
    59
    59
    18
    18

    59

    6
    6

    65

    1
     var _ = require('lazy.js');
    var blessed = require('blessed');
    var path = require('path');

    var Slap = require('./Slap');
    var BaseElement = require('./BaseElement');
    var Button = require('./Button');

    var util = require('../util');
    var markup = require('../markup');

    function Header (opts) {
      var self = this;

      Iif (!(self instanceof blessed.Node)) return new Header(opts);

      var opts = _({
        left: 0,
        right: 0,
        height: 1,
        padding: {left: 1, right: 1},
      })
        .merge(Slap.global.options.header || {})
        .merge(opts || {})
        .toObject();
      BaseElement.call(self, _(opts)
        .merge(opts.headerPosition !== 'bottom'
          ? {top: 0,    headerPosition: 'top'}
          : {bottom: 0, headerPosition: 'bottom'})
        .toObject());

      var helpBinding = self.slap.options.bindings.help;
      helpBinding = Array.isArray(helpBinding) ? helpBinding[0] : helpBinding;
      self.helpButton = new Button(_({
          parent: self,
          content: "Help" + (helpBinding ? ": " + helpBinding : ""),
          right: self.padding.right
        })
        .merge(self.options.helpButton || {})
        .toObject());

      self.leftContent = new BaseElement(_({
          parent: self,
          tags: true,
          left: self.padding.left,
          shrink: true,
          style: self.options.style
        })
        .merge(self.options.leftContent || {})
        .toObject());

      self.rightContent = new BaseElement(_({
          parent: self,
          tags: true,
          right: self.padding.right + BaseElement.prototype.shrinkWidth.call(self.helpButton) + 1,
          shrink: true,
          style: self.options.style
        })
        .merge(self.options.rightContent || {})
        .toObject());

      self._blink(true);
    }
    Header.prototype.__proto__ = BaseElement.prototype;

    Header.prototype._blink = util.getterSetter('blink', null, function (blink) {
      var self = this;
      clearTimeout(self.data.blinkTimer);
      Eif (self.options.blinkRate) {
        self.data.blinkTimer = setTimeout(function () {
          self._blink(!blink);
        }, self.options.blinkRate);
      }
      return blink;
    });
    Header.prototype.message = util.getterSetter('message', null, function (message, styleName) {
      var self = this;

      clearTimeout(self.data.messageTimer);
      if (message) {
        self.data.messageTimer = setTimeout(function () {
          self.message(null);
        }, self.options.messageDuration);
      }

      self._blink(false);
      return message !== null ? markup(' '+message+' ', self.options.style[styleName || 'info']) : null;
    });

    Header.prototype._initHandlers = function () {
      var self = this;
      ['message', 'blink'].forEach(function (evt) {
        self.on(evt, function () { self.parent.render(); });
      });
      self.helpButton.on('press', function () { self.slap.help(); });
      return BaseElement.prototype._initHandlers.apply(self, arguments);
    };

    Header.prototype.render = function () {
      var self = this;

      var style = self.options.style;
      var slap = self.slap;
      var editor = (slap.panes[slap.data.currentPane] || {}).editor;
      if (editor) {
        var originalPath = editor.textBuf.getPath();
        var markupPath = originalPath
          ? blessed.escape(path.relative(self.slap.fileBrowser.cwd, originalPath))
          : "new file";
        Eif (editor.checkpoint !== _(editor.textBuf.history.undoStack).last() || !originalPath) {
          markupPath = markup(markupPath+"*", style.changed);
        }
        self.leftContent.setContent('\u270b ' + markupPath);

        var cursor = editor.selection.getHeadPosition();
        var originalEncoding = editor.textBuf.getEncoding();
        var markupEncoding = originalEncoding ? blessed.escape(originalEncoding) : '';
        var right = [cursor, editor.textBuf.getLineCount(), markupEncoding];

        Iif (editor.readOnly()) right.push(markup("read-only", style.warning));

        Iif (!slap.insertMode()) right.unshift(markup("OVR", style.overwrite));
        var message = self.message();
        if (message) {
          if (self._blink()) message = markup(message, style.blinkStyle);
          right.unshift(message);
        }
        self.rightContent.setContent(right.join(" "));
      } else {
        self.leftContent.setContent("\u270b");
        self.rightContent.setContent('');
      }

      return BaseElement.prototype.render.apply(self, arguments);
    };

    module.exports = Header;