-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
279 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...ktx/src/main/java/com/fox2code/androidansi/builder/AnnotatedStringAnsiComponentBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.fox2code.androidansi.builder | ||
|
||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.text.AnnotatedString.Builder | ||
import com.fox2code.androidansi.AnsiContext | ||
import com.fox2code.androidansi.ktx.toAnsiSpanStyle | ||
|
||
class AnnotatedStringAnsiComponentBuilder : AnsiComponentBuilder<AnnotatedString>() { | ||
private val builder: Builder = Builder() | ||
private var used = false | ||
override fun notifyUse() { | ||
check(!used) { "AnnotatedStringAnsiComponentBuilder can only be used once!" } | ||
used = true | ||
} | ||
|
||
override fun appendWithSpan(buffer: String, bufferStart: Int, bufferEnd: Int, ansiContext: AnsiContext, visibleStart: Int, visibleEnd: Int) { | ||
builder.append(buffer, bufferStart, bufferEnd) | ||
builder.addStyle(ansiContext.toAnsiSpanStyle(), visibleStart, visibleEnd) | ||
} | ||
|
||
override fun build(): AnnotatedString { | ||
return builder.toAnnotatedString() | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
library-ktx/src/main/java/com/fox2code/androidansi/ktx/AnsiComposeEntensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
@file:Suppress("NOTHING_TO_INLINE") // Aliases to public API. | ||
|
||
package com.fox2code.androidansi.ktx | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.text.AnnotatedString | ||
import androidx.compose.ui.text.SpanStyle | ||
import androidx.compose.ui.text.font.FontStyle | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.style.BaselineShift | ||
import androidx.compose.ui.text.style.TextDecoration | ||
import com.fox2code.androidansi.AnsiConstants | ||
import com.fox2code.androidansi.AnsiContext | ||
import com.fox2code.androidansi.AnsiParser | ||
import com.fox2code.androidansi.builder.AnnotatedStringAnsiComponentBuilder | ||
import org.jetbrains.annotations.Contract | ||
|
||
@Contract(pure = true) | ||
fun AnsiContext.toAnsiSpanStyle(): SpanStyle { | ||
var baselineShift: BaselineShift = BaselineShift.None | ||
if (this.style and AnsiConstants.FLAG_STYLE_SUBSCRIPT != 0) { | ||
baselineShift = BaselineShift.Subscript | ||
} else if (this.style and AnsiConstants.FLAG_STYLE_SUPERSCRIPT != 0) { | ||
baselineShift = BaselineShift.Superscript | ||
} | ||
val backgroundColor: Color = when(val background: ULong = | ||
this.background.toULong() or 0xFF000000.toULong()) { | ||
this.defaultBackground.toULong() -> Color.Unspecified | ||
0xFF000000.toULong() -> Color.Black | ||
0xFF444444.toULong() -> Color.DarkGray | ||
0xFF888888.toULong() -> Color.Gray | ||
0xFFCCCCCC.toULong() -> Color.LightGray | ||
0xFFFFFFFF.toULong() -> Color.White | ||
0xFFFF0000.toULong() -> Color.Red | ||
0xFF00FF00.toULong() -> Color.Green | ||
0xFF0000FF.toULong() -> Color.Blue | ||
0xFFFFFF00.toULong() -> Color.Yellow | ||
0xFFFFFF00.toULong() -> Color.Cyan | ||
0xFFFFFF00.toULong() -> Color.Magenta | ||
else -> Color(background) | ||
} | ||
var foregroundColor: Color = when(val foreground: ULong = | ||
this.foreground.toULong() or 0xFF000000.toULong()) { | ||
this.defaultForeground.toULong() -> Color.Unspecified | ||
0xFF000000.toULong() -> Color.Black | ||
0xFF444444.toULong() -> Color.DarkGray | ||
0xFF888888.toULong() -> Color.Gray | ||
0xFFCCCCCC.toULong() -> Color.LightGray | ||
0xFFFFFFFF.toULong() -> Color.White | ||
0xFFFF0000.toULong() -> Color.Red | ||
0xFF00FF00.toULong() -> Color.Green | ||
0xFF0000FF.toULong() -> Color.Blue | ||
0xFFFFFF00.toULong() -> Color.Yellow | ||
0xFFFFFF00.toULong() -> Color.Cyan | ||
0xFFFFFF00.toULong() -> Color.Magenta | ||
else -> Color(foreground) | ||
} | ||
val textDecoration: TextDecoration = when(this.style and ( | ||
AnsiConstants.FLAG_STYLE_UNDERLINE or AnsiConstants.FLAG_STYLE_STRIKE | ||
)) { | ||
AnsiConstants.FLAG_STYLE_UNDERLINE or | ||
AnsiConstants.FLAG_STYLE_STRIKE -> | ||
TextDecoration.Underline.plus(TextDecoration.LineThrough) | ||
AnsiConstants.FLAG_STYLE_UNDERLINE -> TextDecoration.Underline | ||
AnsiConstants.FLAG_STYLE_STRIKE -> TextDecoration.LineThrough | ||
else -> TextDecoration.None | ||
} | ||
if (this.style and AnsiConstants.FLAG_STYLE_DIM != 0) { | ||
foregroundColor = foregroundColor.copy(alpha = (9F/16F)) | ||
} | ||
val fontStyle: FontStyle? = when(this.style and AnsiConstants.FLAG_STYLE_ITALIC) { | ||
AnsiConstants.FLAG_STYLE_ITALIC -> FontStyle.Italic | ||
else -> null | ||
} | ||
val fontWeight: FontWeight? = when(this.style and AnsiConstants.FLAG_STYLE_BOLD) { | ||
AnsiConstants.FLAG_STYLE_BOLD -> FontWeight.Bold | ||
else -> null | ||
} | ||
return SpanStyle(baselineShift = baselineShift, | ||
textDecoration = textDecoration, | ||
background = backgroundColor, | ||
color = foregroundColor, | ||
fontStyle = fontStyle, | ||
fontWeight = fontWeight) | ||
} | ||
|
||
@Contract(pure = true) | ||
inline fun AnsiContext.parseAsAnsiAnnotatedString(text: String, parseFlags: Int = 0): AnnotatedString { | ||
return AnsiParser.parseWithBuilder(AnnotatedStringAnsiComponentBuilder(), text, this, parseFlags) | ||
} | ||
|
||
@Contract(pure = true) | ||
inline fun String.parseAsAnsiAnnotatedString(context: AnsiContext? = null, parseFlags: Int = 0): AnnotatedString { | ||
return AnsiParser.parseWithBuilder(AnnotatedStringAnsiComponentBuilder(), this, context, parseFlags) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
library/src/main/java/com/fox2code/androidansi/builder/AnsiComponentBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.fox2code.androidansi.builder; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.fox2code.androidansi.AnsiContext; | ||
|
||
/** | ||
* This class is called when ANSI parsable text need to be transformed to formatted text. | ||
*/ | ||
public abstract class AnsiComponentBuilder<T extends CharSequence> { | ||
/** | ||
* Can be overridden if you don't want your builder to be used multiple times. | ||
*/ | ||
public void notifyUse() {} | ||
|
||
/** | ||
* @param buffer input raw ansi buffer | ||
* @param bufferStart start index of part of buffer to read | ||
* @param bufferEnd end index of part of buffer to read | ||
* @param ansiContext current ansi context call | ||
* {@link AnsiContext#toAnsiTextSpan()} to set a span. | ||
* @param visibleStart start of simulated visible index if all strings were appended correctly | ||
* @param visibleEnd end of simulated visible index if all strings were appended correctly | ||
*/ | ||
public abstract void appendWithSpan(@NonNull String buffer, int bufferStart, int bufferEnd, | ||
@NonNull AnsiContext ansiContext, int visibleStart, int visibleEnd); | ||
|
||
@NonNull | ||
public abstract T build(); | ||
} |
34 changes: 34 additions & 0 deletions
34
library/src/main/java/com/fox2code/androidansi/builder/SpannableAnsiComponentBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.fox2code.androidansi.builder; | ||
|
||
import android.text.Spannable; | ||
import android.text.SpannableStringBuilder; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import com.fox2code.androidansi.AnsiContext; | ||
|
||
public final class SpannableAnsiComponentBuilder extends AnsiComponentBuilder<Spannable> { | ||
private final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(); | ||
private boolean used = false; | ||
|
||
@Override | ||
public void notifyUse() { | ||
if (this.used) { | ||
throw new IllegalStateException("SpannableAnsiComponentBuilder can only be used once!"); | ||
} | ||
this.used = true; | ||
} | ||
|
||
@Override | ||
public void appendWithSpan(@NonNull String buffer, int bufferStart, int bufferEnd, | ||
@NonNull AnsiContext ansiContext, int visibleStart, int visibleEnd) { | ||
spannableStringBuilder.append(buffer, bufferStart, bufferEnd); | ||
spannableStringBuilder.setSpan(ansiContext.toAnsiTextSpan(), visibleStart, visibleEnd, 0); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Spannable build() { | ||
return spannableStringBuilder; | ||
} | ||
} |
Oops, something went wrong.