termux-app

Форк
0
76 строк · 2.2 Кб
1
package com.termux.shared.shell;
2

3
import androidx.annotation.NonNull;
4
import androidx.annotation.Nullable;
5

6
import com.termux.shared.file.FileUtils;
7
import com.termux.terminal.TerminalBuffer;
8
import com.termux.terminal.TerminalEmulator;
9
import com.termux.terminal.TerminalSession;
10

11
import java.lang.reflect.Field;
12

13
import java.util.ArrayList;
14
import java.util.Collections;
15
import java.util.List;
16

17
public class ShellUtils {
18

19
    /** Get process id of {@link Process}. */
20
    public static int getPid(Process p) {
21
        try {
22
            Field f = p.getClass().getDeclaredField("pid");
23
            f.setAccessible(true);
24
            try {
25
                return f.getInt(p);
26
            } finally {
27
                f.setAccessible(false);
28
            }
29
        } catch (Throwable e) {
30
            return -1;
31
        }
32
    }
33

34
    /** Setup shell command arguments for the execute. */
35
    @NonNull
36
    public static String[] setupShellCommandArguments(@NonNull String executable, @Nullable String[] arguments) {
37
        List<String> result = new ArrayList<>();
38
        result.add(executable);
39
        if (arguments != null) Collections.addAll(result, arguments);
40
        return result.toArray(new String[0]);
41
    }
42

43
    /** Get basename for executable. */
44
    @Nullable
45
    public static String getExecutableBasename(@Nullable String executable) {
46
        return FileUtils.getFileBasename(executable);
47
    }
48

49

50

51
    /** Get transcript for {@link TerminalSession}. */
52
    public static String getTerminalSessionTranscriptText(TerminalSession terminalSession, boolean linesJoined, boolean trim) {
53
        if (terminalSession == null) return null;
54

55
        TerminalEmulator terminalEmulator = terminalSession.getEmulator();
56
        if (terminalEmulator == null) return null;
57

58
        TerminalBuffer terminalBuffer = terminalEmulator.getScreen();
59
        if (terminalBuffer == null) return null;
60

61
        String transcriptText;
62

63
        if (linesJoined)
64
            transcriptText = terminalBuffer.getTranscriptTextWithFullLinesJoined();
65
        else
66
            transcriptText = terminalBuffer.getTranscriptTextWithoutJoinedLines();
67

68
        if (transcriptText == null) return null;
69

70
        if (trim)
71
            transcriptText = transcriptText.trim();
72

73
        return transcriptText;
74
    }
75

76
}
77

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.