001/*
002 * Copyright (C) 2009-2017 the original author(s).
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fusesource.jansi;
017
018import java.io.ByteArrayOutputStream;
019import java.io.IOException;
020
021/**
022 * An ANSI string which reports the size of rendered text correctly (ignoring any ANSI escapes).
023 *
024 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
025 * @since 1.1
026 */
027public class AnsiString
028        implements CharSequence {
029    private final CharSequence encoded;
030
031    private final CharSequence plain;
032
033    public AnsiString(final CharSequence str) {
034        assert str != null;
035        this.encoded = str;
036        this.plain = chew(str);
037    }
038
039    private CharSequence chew(final CharSequence str) {
040        assert str != null;
041
042        ByteArrayOutputStream buff = new ByteArrayOutputStream();
043        AnsiOutputStream out = new AnsiOutputStream(buff);
044
045        try {
046            out.write(str.toString().getBytes());
047            out.flush();
048            out.close();
049        } catch (IOException e) {
050            throw new RuntimeException(e);
051        }
052
053        return new String(buff.toByteArray());
054    }
055
056    public CharSequence getEncoded() {
057        return encoded;
058    }
059
060    public CharSequence getPlain() {
061        return plain;
062    }
063
064    // FIXME: charAt() and subSequence() will make things barf, need to call toString() first to get expected results
065
066    public char charAt(final int index) {
067        return getEncoded().charAt(index);
068    }
069
070    public CharSequence subSequence(final int start, final int end) {
071        return getEncoded().subSequence(start, end);
072    }
073
074    public int length() {
075        return getPlain().length();
076    }
077
078    @Override
079    public boolean equals(final Object obj) {
080        return getEncoded().equals(obj);
081    }
082
083    @Override
084    public int hashCode() {
085        return getEncoded().hashCode();
086    }
087
088    @Override
089    public String toString() {
090        return getEncoded().toString();
091    }
092}