All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aesh.utils.InfoCmpHelper Maven / Gradle / Ivy

There is a newer version: 1.17
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2014 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aesh.utils;

import org.aesh.terminal.tty.Capability;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Ståle W. Pedersen
 */
public class InfoCmpHelper {

    private static boolean initialized = false;
    private static Set bools = new HashSet<>();
    private static Map ints = new HashMap<>();
    private static Map strings = new HashMap<>();

    public static int[] getCurrentTranslatedCapabilityAsInts(String cap, int[] defaultValue) {
        if(Config.isWindows())
            return defaultValue;
        else {
            String s = getCurrentTranslatedCapability(cap, new String(defaultValue, 0, defaultValue.length));
            if (s.length() == 0)
                return defaultValue;
            return s.codePoints().toArray();
        }
    }

    public static String getCurrentTranslatedCapability(String cap, String defaultValue) {
        try {
            if (!initialized) {
                String term = System.getenv("TERM");
                if (term == null) {
                    term = "xterm-256color";
                }
                String infocmp = InfoCmp.getInfoCmp(term);
                InfoCmp.parseInfoCmp(infocmp, bools, ints, strings);
                initialized = true;
            }
            Capability capability = Capability.byName(cap);
            if (capability != null) {
                String capStr = strings.get(capability);
                if (capStr != null) {
                    StringWriter sw = new StringWriter();
                    Curses.tputs(sw, capStr);
                    return sw.toString();
                }
            }
        }
        catch (Exception e) {
            // Ignore
        }
        return defaultValue;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy