2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2019-06-27 19:10:34 +00:00
|
|
|
import Component from '@ember/component';
|
2022-03-08 17:28:36 +00:00
|
|
|
import { scheduleOnce, once } from '@ember/runloop';
|
2019-06-27 19:10:34 +00:00
|
|
|
import { task } from 'ember-concurrency';
|
2019-07-02 19:59:15 +00:00
|
|
|
import WindowResizable from 'nomad-ui/mixins/window-resizable';
|
2022-03-08 17:28:36 +00:00
|
|
|
import {
|
|
|
|
classNames,
|
|
|
|
tagName,
|
|
|
|
attributeBindings,
|
|
|
|
} from '@ember-decorators/component';
|
2020-06-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2019-06-27 19:10:34 +00:00
|
|
|
|
2020-06-16 19:51:52 +00:00
|
|
|
const A_KEY = 65;
|
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
@tagName('pre')
|
|
|
|
@classNames('cli-window')
|
2022-03-08 17:28:36 +00:00
|
|
|
@attributeBindings('data-test-log-cli')
|
2020-06-10 13:49:16 +00:00
|
|
|
export default class StreamingFile extends Component.extend(WindowResizable) {
|
|
|
|
'data-test-log-cli' = true;
|
2019-06-27 19:10:34 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
mode = 'streaming'; // head, tail, streaming
|
|
|
|
isStreaming = true;
|
|
|
|
logger = null;
|
2020-06-13 07:29:33 +00:00
|
|
|
follow = true;
|
2022-09-22 14:58:52 +00:00
|
|
|
shouldFillHeight = true;
|
2020-06-13 07:29:33 +00:00
|
|
|
|
|
|
|
// Internal bookkeeping to avoid multiple scroll events on one frame
|
|
|
|
requestFrame = true;
|
2019-06-27 19:10:34 +00:00
|
|
|
|
|
|
|
didReceiveAttrs() {
|
2021-12-28 14:45:20 +00:00
|
|
|
super.didReceiveAttrs();
|
2019-06-27 19:10:34 +00:00
|
|
|
if (!this.logger) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:28:36 +00:00
|
|
|
scheduleOnce('actions', this, this.performTask);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2020-06-09 21:03:28 +00:00
|
|
|
|
|
|
|
performTask() {
|
|
|
|
switch (this.mode) {
|
|
|
|
case 'head':
|
2020-06-13 07:29:33 +00:00
|
|
|
this.set('follow', false);
|
2020-06-09 21:03:28 +00:00
|
|
|
this.head.perform();
|
|
|
|
break;
|
|
|
|
case 'tail':
|
2020-06-13 07:29:33 +00:00
|
|
|
this.set('follow', true);
|
2020-06-09 21:03:28 +00:00
|
|
|
this.tail.perform();
|
|
|
|
break;
|
|
|
|
case 'streaming':
|
2020-06-13 07:29:33 +00:00
|
|
|
this.set('follow', true);
|
2020-06-09 21:03:28 +00:00
|
|
|
if (this.isStreaming) {
|
|
|
|
this.stream.perform();
|
|
|
|
} else {
|
|
|
|
this.logger.stop();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-06-27 19:10:34 +00:00
|
|
|
|
2020-06-13 07:29:33 +00:00
|
|
|
scrollHandler() {
|
|
|
|
const cli = this.element;
|
2020-06-17 21:50:55 +00:00
|
|
|
|
|
|
|
// Scroll events can fire multiple times per frame, this eliminates
|
|
|
|
// redundant computation.
|
|
|
|
if (this.requestFrame) {
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
// If the scroll position is close enough to the bottom, autoscroll to the bottom
|
2021-12-28 16:08:12 +00:00
|
|
|
this.set(
|
|
|
|
'follow',
|
|
|
|
cli.scrollHeight - cli.scrollTop - cli.clientHeight < 20
|
|
|
|
);
|
2020-06-17 21:50:55 +00:00
|
|
|
this.requestFrame = true;
|
|
|
|
});
|
|
|
|
}
|
2020-06-13 07:29:33 +00:00
|
|
|
this.requestFrame = false;
|
|
|
|
}
|
|
|
|
|
2020-06-16 19:51:52 +00:00
|
|
|
keyDownHandler(e) {
|
|
|
|
// Rebind select-all shortcut to only select the text in the
|
|
|
|
// streaming file output.
|
|
|
|
if ((e.metaKey || e.ctrlKey) && e.keyCode === A_KEY) {
|
|
|
|
e.preventDefault();
|
|
|
|
const selection = window.getSelection();
|
|
|
|
selection.removeAllRanges();
|
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNode(this.element);
|
|
|
|
selection.addRange(range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 19:10:34 +00:00
|
|
|
didInsertElement() {
|
2021-12-28 14:45:20 +00:00
|
|
|
super.didInsertElement(...arguments);
|
2022-09-22 14:58:52 +00:00
|
|
|
if (this.shouldFillHeight) {
|
|
|
|
this.fillAvailableHeight();
|
|
|
|
}
|
2020-06-13 07:29:33 +00:00
|
|
|
|
|
|
|
this.set('_scrollHandler', this.scrollHandler.bind(this));
|
|
|
|
this.element.addEventListener('scroll', this._scrollHandler);
|
2020-06-16 19:51:52 +00:00
|
|
|
|
|
|
|
this.set('_keyDownHandler', this.keyDownHandler.bind(this));
|
|
|
|
document.addEventListener('keydown', this._keyDownHandler);
|
2020-06-13 07:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
willDestroyElement() {
|
2021-12-28 14:45:20 +00:00
|
|
|
super.willDestroyElement(...arguments);
|
2020-06-13 07:29:33 +00:00
|
|
|
this.element.removeEventListener('scroll', this._scrollHandler);
|
2020-06-16 19:51:52 +00:00
|
|
|
document.removeEventListener('keydown', this._keyDownHandler);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-06-27 19:10:34 +00:00
|
|
|
|
|
|
|
windowResizeHandler() {
|
2022-09-22 14:58:52 +00:00
|
|
|
if (this.shouldFillHeight) {
|
|
|
|
once(this, this.fillAvailableHeight);
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-06-27 19:10:34 +00:00
|
|
|
|
|
|
|
fillAvailableHeight() {
|
|
|
|
// This math is arbitrary and far from bulletproof, but the UX
|
|
|
|
// of having the log window fill available height is worth the hack.
|
|
|
|
const margins = 30; // Account for padding and margin on either side of the CLI
|
|
|
|
const cliWindow = this.element;
|
2021-12-28 16:08:12 +00:00
|
|
|
cliWindow.style.height = `${
|
|
|
|
window.innerHeight - cliWindow.offsetTop - margins
|
|
|
|
}px`;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-06-27 19:10:34 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@task(function* () {
|
2019-06-27 19:10:34 +00:00
|
|
|
yield this.get('logger.gotoHead').perform();
|
2022-03-08 17:28:36 +00:00
|
|
|
scheduleOnce('afterRender', this, this.scrollToTop);
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
head;
|
2019-06-27 19:10:34 +00:00
|
|
|
|
2020-06-09 21:03:28 +00:00
|
|
|
scrollToTop() {
|
|
|
|
this.element.scrollTop = 0;
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2020-06-09 21:03:28 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@task(function* () {
|
2019-06-27 19:10:34 +00:00
|
|
|
yield this.get('logger.gotoTail').perform();
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
tail;
|
2019-06-27 19:10:34 +00:00
|
|
|
|
2020-06-13 07:29:33 +00:00
|
|
|
synchronizeScrollPosition() {
|
|
|
|
if (this.follow) {
|
|
|
|
this.element.scrollTop = this.element.scrollHeight;
|
2019-06-27 19:10:34 +00:00
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-06-27 19:10:34 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
@task(function* () {
|
2019-08-07 22:11:32 +00:00
|
|
|
// Follow the log if the scroll position is near the bottom of the cli window
|
2019-10-08 18:44:19 +00:00
|
|
|
this.logger.on('tick', this, 'scheduleScrollSynchronization');
|
2019-08-07 22:11:32 +00:00
|
|
|
|
2019-06-27 19:10:34 +00:00
|
|
|
yield this.logger.startStreaming();
|
2019-10-08 18:44:19 +00:00
|
|
|
this.logger.off('tick', this, 'scheduleScrollSynchronization');
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
stream;
|
2019-06-27 19:10:34 +00:00
|
|
|
|
2019-10-08 18:44:19 +00:00
|
|
|
scheduleScrollSynchronization() {
|
2022-03-08 17:28:36 +00:00
|
|
|
scheduleOnce('afterRender', this, this.synchronizeScrollPosition);
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
2019-10-08 18:44:19 +00:00
|
|
|
|
2019-06-27 19:10:34 +00:00
|
|
|
willDestroy() {
|
2021-12-28 14:45:20 +00:00
|
|
|
super.willDestroy(...arguments);
|
2019-06-27 19:10:34 +00:00
|
|
|
this.logger.stop();
|
2020-06-10 13:49:16 +00:00
|
|
|
}
|
|
|
|
}
|