summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/terminal/terminal.vue
blob: 0ee4107f9abe68acca0034e8c4e226bdfc569fe2 (plain)
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
<script>
import { mapState } from 'vuex';
import { GlLoadingIcon } from '@gitlab/ui';
import { __ } from '~/locale';
import GLTerminal from '~/terminal/terminal';
import TerminalControls from './terminal_controls.vue';
import { RUNNING, STOPPING } from '../../stores/modules/terminal/constants';
import { isStartingStatus } from '../../stores/modules/terminal/utils';

export default {
  components: {
    GlLoadingIcon,
    TerminalControls,
  },
  props: {
    terminalPath: {
      type: String,
      required: false,
      default: '',
    },
    status: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      glterminal: null,
      canScrollUp: false,
      canScrollDown: false,
    };
  },
  computed: {
    ...mapState(['panelResizing']),
    loadingText() {
      if (isStartingStatus(this.status)) {
        return __('Starting...');
      } else if (this.status === STOPPING) {
        return __('Stopping...');
      }

      return '';
    },
  },
  watch: {
    panelResizing() {
      if (!this.panelResizing && this.glterminal) {
        this.glterminal.fit();
      }
    },
    status() {
      this.refresh();
    },
    terminalPath() {
      this.refresh();
    },
  },
  beforeDestroy() {
    this.destroyTerminal();
  },
  methods: {
    refresh() {
      if (this.status === RUNNING && this.terminalPath) {
        this.createTerminal();
      } else if (this.status === STOPPING) {
        this.stopTerminal();
      }
    },
    createTerminal() {
      this.destroyTerminal();
      this.glterminal = new GLTerminal(this.$refs.terminal);
      this.glterminal.addScrollListener(({ canScrollUp, canScrollDown }) => {
        this.canScrollUp = canScrollUp;
        this.canScrollDown = canScrollDown;
      });
    },
    destroyTerminal() {
      if (this.glterminal) {
        this.glterminal.dispose();
        this.glterminal = null;
      }
    },
    stopTerminal() {
      if (this.glterminal) {
        this.glterminal.disable();
      }
    },
  },
};
</script>

<template>
  <div class="d-flex flex-column flex-fill min-height-0 pr-3">
    <div class="top-bar d-flex border-left-0 align-items-center">
      <div v-if="loadingText" data-qa-selector="loading_container">
        <gl-loading-icon :inline="true" />
        <span>{{ loadingText }}</span>
      </div>
      <terminal-controls
        v-if="glterminal"
        class="ml-auto"
        :can-scroll-up="canScrollUp"
        :can-scroll-down="canScrollDown"
        @scroll-up="glterminal.scrollToTop()"
        @scroll-down="glterminal.scrollToBottom()"
      />
    </div>
    <div class="terminal-wrapper d-flex flex-fill min-height-0">
      <div
        ref="terminal"
        class="ide-terminal-trace flex-fill min-height-0 w-100"
        :data-project-path="terminalPath"
        data-qa-selector="terminal_screen"
      ></div>
    </div>
  </div>
</template>