Skip to content

Commit 4a0abc8

Browse files
authored
Merge pull request #2 from DerGoogler/master
Add AnsiTextView and create simple setup in readme
2 parents ec6441e + 7c0013b commit 4a0abc8

File tree

5 files changed

+107
-3
lines changed

5 files changed

+107
-3
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,51 @@ Example: `38;2;164;198;57` set foreground color to rgb(164, 198, 57)
3939

4040
2 -> R;G;B
4141
5 -> X (Only support from 0 to 15, see: [Wikipedia ANSI Page](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors))
42+
43+
44+
# Setup
45+
## Legacy
46+
```java
47+
TextView textView = findViewById(R.id.ansiView);
48+
AnsiParser.setAnsiText(textView, // It's "AndroidANSI!" but with color & style
49+
"\\e[1;38;2;164;198;57mAndroid\\e[0;35mAN\u001B[2mSI\u001B[0;73m!",
50+
AnsiParser.FLAG_PARSE_DISABLE_SUBSCRIPT); // Also disable superscript
51+
```
52+
53+
## TextView
54+
**Layout**
55+
```xml
56+
<com.fox2code.androidansi.AnsiTextView
57+
android:layout_width="wrap_content"
58+
android:layout_height="wrap_content"
59+
android:id="@+id/ansiView" />
60+
```
61+
**Activity**
62+
```java
63+
AnsiTextView textView = findViewById(R.id.ansiView);
64+
textView.setAnsiText("\\e[1;38;2;164;198;57mAndroid\\e[0;35mAN\u001B[2mSI\u001B[0;73m!",
65+
AnsiParser.FLAG_PARSE_DISABLE_SUBSCRIPT);
66+
```
67+
68+
## TextView w/o Java (Currently not working)
69+
```xml
70+
<?xml version="1.0" encoding="utf-8"?>
71+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
72+
xmlns:app="http://schemas.android.com/apk/res-auto"
73+
xmlns:tools="http://schemas.android.com/tools"
74+
android:layout_width="match_parent"
75+
android:layout_height="match_parent"
76+
tools:context=".MainActivity">
77+
78+
<com.fox2code.androidansi.AnsiTextView
79+
android:layout_width="wrap_content"
80+
android:layout_height="wrap_content"
81+
app:setAnsiText="\e[38;5;82mHello \e[38;5;198mWorld"
82+
android:id="@+id/ansiView"
83+
app:layout_constraintBottom_toBottomOf="parent"
84+
app:layout_constraintEnd_toEndOf="parent"
85+
app:layout_constraintStart_toStartOf="parent"
86+
app:layout_constraintTop_toTopOf="parent" />
87+
88+
</androidx.constraintlayout.widget.ConstraintLayout>
89+
```

androidansiapp/src/main/java/com/fox2code/androidansiapp/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.widget.TextView;
77

88
import com.fox2code.androidansi.AnsiParser;
9+
import com.fox2code.androidansi.AnsiTextView;
910

1011
public class MainActivity extends AppCompatActivity {
1112

androidansiapp/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<TextView
9+
<com.fox2code.androidansi.AnsiTextView
10+
android:id="@+id/ansiView"
1011
android:layout_width="wrap_content"
1112
android:layout_height="wrap_content"
12-
android:text="Hello World!"
13-
android:id="@+id/ansiView"
1413
app:layout_constraintBottom_toBottomOf="parent"
1514
app:layout_constraintEnd_toEndOf="parent"
1615
app:layout_constraintStart_toStartOf="parent"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fox2code.androidansi;
2+
3+
import static com.fox2code.androidansi.AnsiParser.parseAsSpannable;
4+
5+
import android.annotation.SuppressLint;
6+
import android.content.Context;
7+
import android.content.res.TypedArray;
8+
import android.util.AttributeSet;
9+
import android.widget.TextView;
10+
11+
import androidx.annotation.NonNull;
12+
13+
public class AnsiTextView extends TextView{
14+
15+
public final int FLAG_PARSE_DISABLE_COLORS = AnsiParser.FLAG_PARSE_DISABLE_COLORS;
16+
public final int FLAG_PARSE_DISABLE_ATTRIBUTES = AnsiParser.FLAG_PARSE_DISABLE_ATTRIBUTES;
17+
public final int FLAG_PARSE_DISABLE_EXTRAS_COLORS = AnsiParser.FLAG_PARSE_DISABLE_EXTRAS_COLORS;
18+
public final int FLAG_PARSE_DISABLE_SUBSCRIPT = AnsiParser.FLAG_PARSE_DISABLE_SUBSCRIPT;
19+
20+
public AnsiTextView(Context context) {
21+
super(context);
22+
}
23+
24+
public AnsiTextView(Context context, AttributeSet attrs) {
25+
super(context, attrs);
26+
readAttr(context,attrs);
27+
}
28+
29+
public AnsiTextView(Context context, AttributeSet attrs, int defStyle) {
30+
super(context, attrs, defStyle);
31+
readAttr(context,attrs);
32+
}
33+
34+
public void setAnsiText(@NonNull String ansiText) {
35+
super.setText(parseAsSpannable(ansiText));
36+
}
37+
38+
public void setAnsiText(@NonNull String ansiText, int parseFlags) {
39+
super.setText(parseAsSpannable(ansiText, parseFlags));
40+
}
41+
42+
private void readAttr(Context context, AttributeSet attrs) {
43+
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AnsiTextView);
44+
@NonNull String text = a.getString(R.styleable.AnsiTextView_setAnsiText) ;
45+
if (text != null) {
46+
super.setText(parseAsSpannable(text));
47+
}
48+
a.recycle();
49+
}
50+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<declare-styleable name="AnsiTextView">
4+
<attr name="setAnsiText" format="string" />
5+
</declare-styleable>
6+
</resources>

0 commit comments

Comments
 (0)