• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1032268278

10 Oct 2023 04:28PM UTC coverage: 82.723% (-17.2%) from 99.964%
1032268278

Pull #4096

gitlab-ci

mzedel
chore: made offline settings saving less aggressive

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4096: MEN-6781

4340 of 6277 branches covered (0.0%)

1 of 2 new or added lines in 1 file covered. (50.0%)

1719 existing lines in 164 files now uncovered.

8307 of 10042 relevant lines covered (82.72%)

201.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.09
/src/js/components/devices/troubleshoot/terminal.js
1
// Copyright 2020 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14
import React, { useCallback, useEffect, useMemo, useState } from 'react';
15

16
import { FitAddon } from 'xterm-addon-fit';
17
import { WebLinksAddon } from 'xterm-addon-web-links';
18

19
import { TIMEOUTS } from '../../../constants/appConstants';
20
import { DEVICE_MESSAGE_TYPES as MessageTypes } from '../../../constants/deviceConstants';
21
import useWindowSize from '../../../utils/resizehook';
22
import XTerm from '../../common/xterm';
23

24
const fitAddon = new FitAddon();
13✔
25

26
export const options = {
13✔
27
  cursorBlink: 'block',
28
  macOptionIsMeta: true
29
};
30

31
// only matching absolute paths, so: /here/there - but not ../here or ./here or here/there
32
const unixPathRegex = new RegExp('(\\/([^\\0\\s!$`&*()\\[\\]+\'":;\\\\])+)');
13✔
33

34
export const Terminal = ({ onDownloadClick, sendMessage, setSnackbar, socketInitialized, textInput, xtermRef, ...xtermProps }) => {
13✔
35
  const [dimensions, setDimensions] = useState({});
7✔
36
  const [isVisible, setIsVisible] = useState(false);
7✔
37
  const size = useWindowSize();
7✔
38

39
  const observer = useMemo(() => new IntersectionObserver(([entry]) => setIsVisible(entry.isIntersecting)), []);
7✔
40
  const addons = useMemo(() => [fitAddon], []);
7✔
41

42
  const tryFit = useCallback(() => {
7✔
43
    try {
1✔
44
      fitAddon.fit();
1✔
45
    } catch {
UNCOV
46
      setSnackbar('Fit not possible, terminal not yet visible', TIMEOUTS.fiveSeconds);
×
47
    }
48
  }, [setSnackbar]);
49

50
  useEffect(() => {
7✔
51
    if (!socketInitialized) {
5✔
52
      return;
4✔
53
    }
54
    observer.observe(xtermRef.current.terminalRef.current);
1✔
55
    // Remove the observer as soon as the component is unmounted
56
    return () => {
1✔
57
      observer.disconnect();
1✔
58
    };
59
  }, [observer, sendMessage, socketInitialized, xtermRef]);
60

61
  useEffect(() => {
7✔
62
    if (!socketInitialized || !xtermRef.current?.terminal?.current) {
5✔
63
      return;
4✔
64
    }
65
    xtermRef.current.terminal.current.reset();
1✔
66
    const webLinksAddon = new WebLinksAddon((e, link) => onDownloadClick(link), { urlRegex: unixPathRegex }, true);
1✔
67
    xtermRef.current.terminal.current.loadAddon(webLinksAddon);
1✔
68
    tryFit();
1✔
69
    const { rows = 40, cols = 80 } = fitAddon.proposeDimensions() || {};
1✔
70
    sendMessage({ typ: MessageTypes.Resize, props: { terminal_height: rows, terminal_width: cols } });
1✔
71
    setDimensions({ rows, cols });
1✔
72
    xtermRef.current.terminal.current.focus();
1✔
73
    // eslint-disable-next-line react-hooks/exhaustive-deps
74
  }, [onDownloadClick, socketInitialized, tryFit, sendMessage]);
75

76
  useEffect(() => {
7✔
77
    if (!socketInitialized || !xtermRef.current.terminal || !isVisible) {
6!
78
      return;
6✔
79
    }
UNCOV
80
    fitAddon.fit();
×
UNCOV
81
    const newDimensions = fitAddon.proposeDimensions();
×
UNCOV
82
    if (newDimensions.rows != dimensions.rows || newDimensions.cols != dimensions.cols) {
×
83
      //
UNCOV
84
      sendMessage({
×
85
        typ: MessageTypes.Resize,
86
        props: { terminal_height: newDimensions.rows, terminal_width: newDimensions.cols }
87
      });
UNCOV
88
      setDimensions(newDimensions);
×
89
    }
90
  }, [size, isVisible, socketInitialized, xtermRef, dimensions.rows, dimensions.cols, sendMessage]);
91

92
  useEffect(() => {
7✔
93
    if (!socketInitialized || !xtermRef.current.terminal || !textInput) {
5!
94
      return;
5✔
95
    }
UNCOV
96
    xtermRef.current.terminal.current.paste(textInput);
×
97
  }, [socketInitialized, textInput, xtermRef]);
98

99
  const onData = useCallback(data => sendMessage({ typ: MessageTypes.Shell, body: data }), [sendMessage]);
7✔
100

101
  return <XTerm xtermRef={xtermRef} addons={addons} options={options} onData={onData} {...xtermProps} />;
7✔
102
};
103

104
export default Terminal;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc