com.googlecode.lanterna.TerminalSize Java Examples

The following examples show how to use com.googlecode.lanterna.TerminalSize. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CygwinTerminal.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected TerminalSize findTerminalSize() {
    try {
        String stty = runSTTYCommand("-a");
        Matcher matcher = STTY_SIZE_PATTERN.matcher(stty);
        if(matcher.matches()) {
            return new TerminalSize(Integer.parseInt(matcher.group(2)), Integer.parseInt(matcher.group(1)));
        }
        else {
            return new TerminalSize(80, 24);
        }
    }
    catch(Throwable e) {
        return new TerminalSize(80, 24);
    }
}
 
Example #2
Source File: BorderLayout.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public TerminalSize getPreferredSize(List<Component> components) {
    EnumMap<Location, Component> layout = makeLookupMap(components);
    int preferredHeight = 
            (layout.containsKey(Location.TOP) ? layout.get(Location.TOP).getPreferredSize().getRows() : 0)
            +
            Math.max(
                layout.containsKey(Location.LEFT) ? layout.get(Location.LEFT).getPreferredSize().getRows() : 0,
                Math.max(
                    layout.containsKey(Location.CENTER) ? layout.get(Location.CENTER).getPreferredSize().getRows() : 0,
                    layout.containsKey(Location.RIGHT) ? layout.get(Location.RIGHT).getPreferredSize().getRows() : 0))
            +
            (layout.containsKey(Location.BOTTOM) ? layout.get(Location.BOTTOM).getPreferredSize().getRows() : 0);

    int preferredWidth = 
            Math.max(
                (layout.containsKey(Location.LEFT) ? layout.get(Location.LEFT).getPreferredSize().getColumns() : 0) +
                    (layout.containsKey(Location.CENTER) ? layout.get(Location.CENTER).getPreferredSize().getColumns() : 0) +
                    (layout.containsKey(Location.RIGHT) ? layout.get(Location.RIGHT).getPreferredSize().getColumns() : 0),
                Math.max(
                    layout.containsKey(Location.TOP) ? layout.get(Location.TOP).getPreferredSize().getColumns() : 0,
                    layout.containsKey(Location.BOTTOM) ? layout.get(Location.BOTTOM).getPreferredSize().getColumns() : 0));
    return new TerminalSize(preferredWidth, preferredHeight);
}
 
Example #3
Source File: LinearLayout.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void doLayout(TerminalSize area, List<Component> components) {
    // Filter out invisible components
    components = components.stream().filter(Component::isVisible).collect(Collectors.toList());

    if(direction == Direction.VERTICAL) {
        if (Boolean.getBoolean("com.googlecode.lanterna.gui2.LinearLayout.useOldNonFlexLayout")) {
            doVerticalLayout(area, components);
        }
        else {
            doFlexibleVerticalLayout(area, components);
        }
    }
    else {
        if (Boolean.getBoolean("com.googlecode.lanterna.gui2.LinearLayout.useOldNonFlexLayout")) {
            doHorizontalLayout(area, components);
        }
        else {
            doFlexibleHorizontalLayout(area, components);
        }
    }
    this.changed = false;
}
 
Example #4
Source File: Terminal24bitColorTest.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    final String string = "Hello!";
    Random random = new Random();
    Terminal terminal = new TestTerminalFactory(args).createTerminal();
    terminal.enterPrivateMode();
    terminal.clearScreen();
    TerminalSize size = terminal.getTerminalSize();

    while(true) {
        if(terminal.pollInput() != null) {
            terminal.exitPrivateMode();
            return;
        }

        terminal.setForegroundColor(new TextColor.RGB(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
        terminal.setBackgroundColor(new TextColor.RGB(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
        terminal.setCursorPosition(random.nextInt(size.getColumns() - string.length()), random.nextInt(size.getRows()));
        printString(terminal, string);

        try {
            Thread.sleep(200);
        }
        catch(InterruptedException e) {
        }
    }
}
 
Example #5
Source File: GridLayout.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
private int shrinkHeightToFitArea(TerminalSize area, int[] rowHeights) {
    int totalHeight = 0;
    for(int height: rowHeights) {
        totalHeight += height;
    }
    if(totalHeight > area.getRows()) {
        int rowOffset = 0;
        do {
            if(rowHeights[rowOffset] > 0) {
                rowHeights[rowOffset]--;
                totalHeight--;
            }
            if(++rowOffset == rowHeights.length) {
                rowOffset = 0;
            }
        }
        while(totalHeight > area.getRows());
    }
    return totalHeight;
}
 
Example #6
Source File: MultiButtonTest.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    Screen screen = new TestTerminalFactory(args).createScreen();
    screen.startScreen();
    MultiWindowTextGUI textGUI = new MultiWindowTextGUI(screen);
    textGUI.setEOFWhenNoWindows(true);
    try {
        final BasicWindow window = new BasicWindow("Button test");
        Panel contentArea = new Panel();
        contentArea.setLayoutManager(new LinearLayout(Direction.VERTICAL));
        contentArea.addComponent(new Button(""));
        contentArea.addComponent(new Button("TRE"));
        contentArea.addComponent(new Button("Button"));
        contentArea.addComponent(new Button("Another button"));
        contentArea.addComponent(new EmptySpace(new TerminalSize(5, 1)));
        //contentArea.addComponent(new Button("Here is a\nmulti-line\ntext segment that is using \\n"));
        contentArea.addComponent(new Button("OK", window::close));

        window.setComponent(contentArea);
        textGUI.addWindowAndWait(window);
    }
    finally {
        screen.stopScreen();
    }
}
 
Example #7
Source File: TerminalTta3Orchestrator.java    From pen-test-automation with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    // Setup terminal and screen layers
    TerminalSize size = new TerminalSize(80,50);  // unsure how to do this.
    Terminal terminal = new DefaultTerminalFactory().createTerminal();
    Screen screen = new TerminalScreen(terminal);
    /// ???.setPreferredSize(new TerminalSize(20, 5))

    try {
        screen.startScreen();
        TerminalTta3Orchestrator terminalTta3Orchestrator = new TerminalTta3Orchestrator(screen);
        terminalTta3Orchestrator.start();
    } finally {
        screen.stopScreen();
        terminal.close();
    }
}
 
Example #8
Source File: DynamicGridLayoutTest.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void onResetGrid(WindowBasedTextGUI textGUI, Panel gridPanel) {
    BigInteger columns = TextInputDialog.showNumberDialog(textGUI, "Reset Grid", "Reset grid to how many columns?", "4");
    if(columns == null) {
        return;
    }
    BigInteger prepopulate = TextInputDialog.showNumberDialog(
            textGUI,
            "Reset Grid",
            "Pre-populate grid with how many dummy components?",
            columns.toString());
    gridPanel.removeAllComponents();
    gridPanel.setLayoutManager(newGridLayout(columns.intValue()));
    //noinspection ConstantConditions
    for(int i = 0; i < prepopulate.intValue(); i++) {
        gridPanel.addComponent(new EmptySpace(getRandomColor(), new TerminalSize(4, 1)));
    }
}
 
Example #9
Source File: ComboBox.java    From lanterna with GNU Lesser General Public License v3.0 6 votes vote down vote up
public PopupWindow() {
    setHints(Arrays.asList(
            Hint.NO_FOCUS,
            Hint.FIXED_POSITION));
    listBox = new ActionListBox(ComboBox.this.getSize().withRows(getItemCount()));
    for(int i = 0; i < getItemCount(); i++) {
        V item = items.get(i);
        final int index = i;
        listBox.addItem(item.toString(), () -> {
            setSelectedIndex(index);
            close();
            popupWindow = null;
        });
    }
    listBox.setSelectedIndex(getSelectedIndex());
    TerminalSize dropDownListPreferedSize = listBox.getPreferredSize();
    if(dropDownNumberOfRows > 0) {
        listBox.setPreferredSize(dropDownListPreferedSize.withRows(
                Math.min(dropDownNumberOfRows, dropDownListPreferedSize.getRows())));
    }
    setComponent(listBox);
}
 
Example #10
Source File: WindowManagerTest.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected void prepareWindow(TerminalSize screenSize, Window window) {
    super.prepareWindow(screenSize, window);

    window.setDecoratedSize(window.getPreferredSize().withRelative(12, 10));
    window.setPosition(new TerminalPosition(
            screenSize.getColumns() - window.getDecoratedSize().getColumns() - 1,
            screenSize.getRows() - window.getDecoratedSize().getRows() - 1
    ));
}
 
Example #11
Source File: MessageDialog.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
MessageDialog(
        String title,
        String text,
        MessageDialogButton... buttons) {

    super(title);
    this.result = null;
    if(buttons == null || buttons.length == 0) {
        buttons = new MessageDialogButton[] { MessageDialogButton.OK };
    }

    Panel buttonPanel = new Panel();
    buttonPanel.setLayoutManager(new GridLayout(buttons.length).setHorizontalSpacing(1));
    for(final MessageDialogButton button: buttons) {
        buttonPanel.addComponent(new Button(button.toString(), () -> {
            result = button;
            close();
        }));
    }

    Panel mainPanel = new Panel();
    mainPanel.setLayoutManager(
            new GridLayout(1)
                    .setLeftMarginSize(1)
                    .setRightMarginSize(1));
    mainPanel.addComponent(new Label(text));
    mainPanel.addComponent(new EmptySpace(TerminalSize.ONE));
    buttonPanel.setLayoutData(
            GridLayout.createLayoutData(
                    GridLayout.Alignment.END,
                    GridLayout.Alignment.CENTER,
                    false,
                    false))
            .addTo(mainPanel);
    setComponent(mainPanel);
}
 
Example #12
Source File: MenuItem.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public TerminalSize getPreferredSize(MenuItem component) {
    int preferredWidth = TerminalTextUtils.getColumnWidth(component.getLabel()) + 2;
    if (component instanceof Menu && !(component.getParent() instanceof MenuBar)) {
        preferredWidth += 2;
    }
    return TerminalSize.ONE.withColumns(preferredWidth);
}
 
Example #13
Source File: NewSwingTerminalTest.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates new form NewSwingTerminalTest
 */
public NewSwingTerminalTest() {
    initComponents();

    final SwingTerminal leftTerminal = new SwingTerminal();
    final SwingTerminal rightTerminal = new SwingTerminal();
    splitPane.setLeftComponent(leftTerminal);
    splitPane.setRightComponent(rightTerminal);
    pack();

    Timer timer = new Timer(500, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            drawRandomHello(leftTerminal);
            drawRandomHello(rightTerminal);
        }

        private void drawRandomHello(IOSafeTerminal terminal) {
            TerminalSize size = terminal.getTerminalSize();
            if(size.getColumns() > 6 && size.getRows() > 1) {
                int positionX = RANDOM.nextInt(size.getColumns() - 6);
                int positionY = RANDOM.nextInt(size.getRows());

                terminal.setCursorPosition(positionX, positionY);
                terminal.setBackgroundColor(new TextColor.Indexed(RANDOM.nextInt(256)));
                terminal.setForegroundColor(new TextColor.Indexed(RANDOM.nextInt(256)));
                String hello = "Hello!";
                for(int i = 0; i < hello.length(); i++) {
                    terminal.putCharacter(hello.charAt(i));
                }
                terminal.flush();
            }
        }
    });
    timer.start();
}
 
Example #14
Source File: Issue387.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
    try {
        Screen screen = new DefaultTerminalFactory().createScreen();
        screen.startScreen();

        Window window = new BasicWindow();

        Table<String> table = new Table<>("Column");
        table.setVisibleRows(3);

        table.getTableModel().addRow("row 1");
        table.getTableModel().addRow("row 2");
        table.getTableModel().addRow("row 3");
        table.getTableModel().addRow("row 4");
        table.getTableModel().addRow("row 5");
        table.getTableModel().addRow("row 6");
        table.getTableModel().addRow("row 7");

        Panel panel = new Panel();
        panel.addComponent(new TextBox());
        panel.addComponent(new EmptySpace(new TerminalSize(15, 1)));
        panel.addComponent(table);
        panel.addComponent(new EmptySpace(new TerminalSize(15, 1)));
        panel.addComponent(new TextBox());

        window.setComponent(panel);

        MultiWindowTextGUI gui = new MultiWindowTextGUI(screen);
        gui.addWindowAndWait(window);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #15
Source File: Panel.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public TerminalSize calculatePreferredSize() {
    if(cachedPreferredSize != null && !isInvalid()) {
        return cachedPreferredSize;
    }
    return super.calculatePreferredSize();
}
 
Example #16
Source File: ProgressBar.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public TerminalSize getPreferredSize(ProgressBar component) {
    int preferredWidth = component.getPreferredWidth();
    if(preferredWidth > 0) {
        return new TerminalSize(preferredWidth, 1);
    }
    else if(component.getLabelFormat() != null && !component.getLabelFormat().trim().isEmpty()) {
        return new TerminalSize(TerminalTextUtils.getColumnWidth(String.format(component.getLabelFormat(), 100.0f)) + 2, 1);
    }
    else {
        return new TerminalSize(10, 1);
    }
}
 
Example #17
Source File: Issue216.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    Terminal terminal = new DefaultTerminalFactory().createTerminal();
    Screen screen = new TerminalScreen(terminal);
    screen.startScreen();

    // Create panel to hold components
    Panel panel = new Panel();
    panel.setLayoutManager(new GridLayout(2));

    panel.addComponent(new Label("Forename"));
    panel.addComponent(new TextBox());

    panel.addComponent(new Label("Surname"));
    panel.addComponent(new TextBox());

    panel.addComponent(new Label("Table"));
    final Table<String> table = new Table<>("Test");
    final TableModel<String> tableModel = table.getTableModel();
    tableModel.addRow("hi");
    panel.addComponent(table);

    panel.addComponent(new EmptySpace(new TerminalSize(0,0))); // Empty space underneath labels
    panel.addComponent(new Button("Submit", () -> {
        tableModel.addRow("haiiii");
        //table.invalidate();
    }));

    // Create window to hold the panel
    BasicWindow window = new BasicWindow();
    window.setComponent(panel);

    // Create gui and start gui
    MultiWindowTextGUI gui = new MultiWindowTextGUI(screen, new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE));
    gui.addWindowAndWait(window);
}
 
Example #18
Source File: VirtualTerminalTextGraphics.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public TextGraphics setCharacter(int columnIndex, int rowIndex, TextCharacter textCharacter) {
    TerminalSize size = getSize();
    if(columnIndex < 0 || columnIndex >= size.getColumns() ||
            rowIndex < 0 || rowIndex >= size.getRows()) {
        return this;
    }
    synchronized(virtualTerminal) {
        virtualTerminal.setCursorPosition(new TerminalPosition(columnIndex, rowIndex));
        virtualTerminal.putCharacter(textCharacter);
    }
    return this;
}
 
Example #19
Source File: LinearLayout.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public TerminalSize getPreferredSize(List<Component> components) {
    // Filter out invisible components
    components = components.stream().filter(Component::isVisible).collect(Collectors.toList());

    if(direction == Direction.VERTICAL) {
        return getPreferredSizeVertically(components);
    }
    else {
        return getPreferredSizeHorizontally(components);
    }
}
 
Example #20
Source File: BasicTextImage.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public BasicTextImage resize(TerminalSize newSize, TextCharacter filler) {
    if(newSize == null || filler == null) {
        throw new IllegalArgumentException("Cannot resize BasicTextImage with null " +
                (newSize == null ? "newSize" : "filler"));
    }
    if(newSize.getRows() == buffer.length &&
            (buffer.length == 0 || newSize.getColumns() == buffer[0].length)) {
        return this;
    }
    return new BasicTextImage(newSize, buffer, filler);
}
 
Example #21
Source File: CheckBox.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public TerminalSize getPreferredSize(CheckBox component) {
    int width = 3;
    if(!component.label.isEmpty()) {
        width += 1 + TerminalTextUtils.getColumnWidth(component.label);
    }
    return new TerminalSize(width, 1);
}
 
Example #22
Source File: SubTextGraphics.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public TextGraphics setCharacter(int columnIndex, int rowIndex, TextCharacter textCharacter) {
    TerminalSize writableArea = getSize();
    if(columnIndex < 0 || columnIndex >= writableArea.getColumns() ||
            rowIndex < 0 || rowIndex >= writableArea.getRows()) {
        return this;
    }
    TerminalPosition projectedPosition = project(columnIndex, rowIndex);
    underlyingTextGraphics.setCharacter(projectedPosition, textCharacter);
    return this;
}
 
Example #23
Source File: SwingTerminal.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new SwingTerminal component using custom settings and no scroll controller.
 * @param initialTerminalSize Initial size of the terminal, which will be used when calculating the preferred size
 *                            of the component. If null, it will default to 80x25. If the AWT layout manager forces
 *                            the component to a different size, the value of this parameter won't have any meaning
 * @param deviceConfiguration Device configuration to use for this SwingTerminal
 * @param fontConfiguration Font configuration to use for this SwingTerminal
 * @param colorConfiguration Color configuration to use for this SwingTerminal
 */
public SwingTerminal(
        TerminalSize initialTerminalSize,
        TerminalEmulatorDeviceConfiguration deviceConfiguration,
        SwingTerminalFontConfiguration fontConfiguration,
        TerminalEmulatorColorConfiguration colorConfiguration) {

    this(initialTerminalSize,
            deviceConfiguration,
            fontConfiguration,
            colorConfiguration,
            new TerminalScrollController.Null());
}
 
Example #24
Source File: AbstractComponent.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Default constructor
 */
public AbstractComponent() {
    size = TerminalSize.ZERO;
    position = TerminalPosition.TOP_LEFT_CORNER;
    explicitPreferredSize = null;
    layoutData = null;
    visible = true;
    invalid = true;
    parent = null;
    overrideRenderer = null;
    themeRenderer = null;
    themeRenderersTheme = null;
    defaultRenderer = null;
}
 
Example #25
Source File: IOSafeTerminalAdapter.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public TerminalSize getTerminalSize() {
    try {
        return backend.getTerminalSize();
    }
    catch(IOException e) {
        exceptionHandler.onException(e);
    }
    return null;
}
 
Example #26
Source File: AbstractScreen.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public synchronized TerminalSize doResizeIfNecessary() {
    TerminalSize pendingResize = getAndClearPendingResize();
    if(pendingResize == null) {
        return null;
    }

    backBuffer = backBuffer.resize(pendingResize, defaultCharacter);
    frontBuffer = frontBuffer.resize(pendingResize, defaultCharacter);
    return pendingResize;
}
 
Example #27
Source File: ANSITerminal.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected TerminalSize findTerminalSize() throws IOException {
    saveCursorPosition();
    setCursorPosition(5000, 5000);
    resetMemorizedCursorPosition();
    reportPosition();
    restoreCursorPosition();
    TerminalPosition terminalPosition = waitForCursorPositionReport();
    if (terminalPosition == null) {
        terminalPosition = new TerminalPosition(80,24);
    }
    return new TerminalSize(terminalPosition.getColumn(), terminalPosition.getRow());
}
 
Example #28
Source File: TerminalResizeTest.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onResized(Terminal terminal, TerminalSize newSize) {
    try {
        terminal.setCursorPosition(0, 0);
        String string = newSize.getColumns() + "x" + newSize.getRows() + "                     ";
        char[] chars = string.toCharArray();
        for(char c : chars) {
            terminal.putCharacter(c);
        }
        terminal.flush();
    }
    catch(IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #29
Source File: AWTTerminal.java    From lanterna with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new AWTTerminal component using custom settings and no scroll controller.
 * @param initialTerminalSize Initial size of the terminal, which will be used when calculating the preferred size
 *                            of the component. If null, it will default to 80x25. If the AWT layout manager forces
 *                            the component to a different size, the value of this parameter won't have any meaning
 * @param deviceConfiguration Device configuration to use for this AWTTerminal
 * @param fontConfiguration Font configuration to use for this AWTTerminal
 * @param colorConfiguration Color configuration to use for this AWTTerminal
 */
public AWTTerminal(
        TerminalSize initialTerminalSize,
        TerminalEmulatorDeviceConfiguration deviceConfiguration,
        AWTTerminalFontConfiguration fontConfiguration,
        TerminalEmulatorColorConfiguration colorConfiguration) {

    this(initialTerminalSize,
            deviceConfiguration,
            fontConfiguration,
            colorConfiguration,
            new TerminalScrollController.Null());
}
 
Example #30
Source File: AbstractWindow.java    From lanterna with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
@Deprecated
public void setSize(TerminalSize size) {
    setSize(size, true);
}