A review of the shown files and the diff card: the sheet opens the gate, the diff is bounded first
CI / check (push) Canceled after 0s
CI / check (push) Canceled after 0s
The assistant's sheet is a session's window and no activity, so the gate a reply's picture now waits at never opened for it: the session says when it shows and hides, and the lock still keeps the gate shut. An edit's diff is worked out off the main thread, one file after another, and each text is cut to 2000 lines and 200000 characters before anything is split or compared; a change that was cut says so. Lines are compared without the carriage return before their newline, and a change of line endings is said once. Invisible, direction-changing and line-breaking characters in a line or a path are written as their code point. The call's arguments stay on the card when the diffs say nothing. "Open with" takes the type from the copy's first bytes, else from its name's extension, and no longer from the instance's word. Tests: the too-long case now shares lines out of order and can fail; the device file fixtures are checked by field; the gate with the session; the bounds, the line endings, the escaping, the arguments and the type. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
25 files changed
+554
-87
No files matched your search
@@ -2,15 +2,21 @@ package eu.llembas.android.api
|
||||
|
||||
// An edit's diff on its approval card, worked out on the phone from the two texts the card
|
||||
// carries (`contract/event_ask_edit.json`). Lines only: what is drawn is text with a sign, and
|
||||
// every step is bounded, because both texts are a model's and a file's words of any size.
|
||||
// every step is bounded before it is taken, because both texts are a model's and a file's words
|
||||
// of any size.
|
||||
|
||||
/** What a line of a diff is: in both texts, only in the new one, only in the old one, or the mark of lines left out between two changes. */
|
||||
enum class DiffSign { Same, Added, Removed, Gap }
|
||||
|
||||
data class DiffLine(val sign: DiffSign, val text: String)
|
||||
|
||||
/** One file's change as the card draws it: the changed lines with a little around them, and how many were added and removed. */
|
||||
data class DiffView(val lines: List<DiffLine>, val added: Int, val removed: Int) {
|
||||
/**
|
||||
* One file's change as the card draws it: the changed lines with a little around them, and how
|
||||
* many were added and removed. [endings] says a change of line endings (`"LF" to "CRLF"`), which
|
||||
* is said once instead of as every line going and coming. [cut]: the texts were longer than what
|
||||
* a phone compares, and only their beginning was.
|
||||
*/
|
||||
data class DiffView(val lines: List<DiffLine>, val added: Int, val removed: Int, val endings: Pair<String, String>? = null, val cut: Boolean = false) {
|
||||
/** The lines drawn: the first [DIFF_FOLD] until the person opens it, and never more than [DIFF_MOST]. */
|
||||
fun shown(open: Boolean): List<DiffLine> = lines.take(if (open) DIFF_MOST else DIFF_FOLD)
|
||||
|
||||
@@ -19,6 +25,9 @@ data class DiffView(val lines: List<DiffLine>, val added: Int, val removed: Int)
|
||||
|
||||
/** The lines not drawn even when open. */
|
||||
val beyond: Int get() = (lines.size - DIFF_MOST).coerceAtLeast(0)
|
||||
|
||||
/** Whether the block says anything of the change at all; a card whose diffs say nothing keeps its arguments in view. */
|
||||
val says: Boolean get() = lines.isNotEmpty() || endings != null || cut
|
||||
}
|
||||
|
||||
/** How many lines of a file's diff the card shows before it is opened. */
|
||||
@@ -33,11 +42,40 @@ const val DIFF_CONTEXT = 3
|
||||
/** Past this many pairs of lines the two middles are not compared: the old one is shown going and the new one coming. */
|
||||
private const val DIFF_CELLS = 1_000_000L
|
||||
|
||||
/** A text's lines; the newline that ends the last one starts no empty line. */
|
||||
internal fun textLines(text: String): List<String> {
|
||||
if (text.isEmpty()) return emptyList()
|
||||
val lines = text.split('\n')
|
||||
return if (lines.last().isEmpty()) lines.dropLast(1) else lines
|
||||
/** The most lines of one text that are compared, and the most characters: what is past either is left out before any work is done. */
|
||||
const val DIFF_TEXT_LINES = 2_000
|
||||
const val DIFF_TEXT_CHARS = 200_000
|
||||
|
||||
/** A text's lines as they are compared, and whether the text went on past what is. */
|
||||
data class TextLines(val lines: List<String>, val cut: Boolean)
|
||||
|
||||
/**
|
||||
* The lines of [text], at most [mostLines] of them from at most its first [mostChars]
|
||||
* characters: nothing past either bound is read, split or kept. The newline that ends the last
|
||||
* line starts no empty one, and a line is compared without the `\r` before its newline, so a
|
||||
* file whose line endings change is not a file whose every line changes.
|
||||
*/
|
||||
fun textLines(text: String, mostLines: Int = DIFF_TEXT_LINES, mostChars: Int = DIFF_TEXT_CHARS): TextLines {
|
||||
val end = minOf(text.length, mostChars)
|
||||
val lines = ArrayList<String>()
|
||||
var from = 0
|
||||
while (from < end && lines.size < mostLines) {
|
||||
val newline = text.indexOf('\n', from).let { if (it < 0 || it >= end) -1 else it }
|
||||
val to = if (newline < 0) end else newline
|
||||
lines += text.substring(from, if (to > from && text[to - 1] == '\r') to - 1 else to)
|
||||
from = if (newline < 0) end else newline + 1
|
||||
}
|
||||
return TextLines(lines, cut = from < text.length)
|
||||
}
|
||||
|
||||
/** How [text]'s lines end, where it has any: `CRLF` when its first newline follows a `\r`, else `LF`; null for a text of one line. */
|
||||
internal fun lineEnding(text: String): String? {
|
||||
val newline = text.indexOf('\n')
|
||||
return when {
|
||||
newline < 0 -> null
|
||||
newline > 0 && text[newline - 1] == '\r' -> "CRLF"
|
||||
else -> "LF"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,8 +145,55 @@ fun hunks(lines: List<DiffLine>, context: Int = DIFF_CONTEXT): List<DiffLine> {
|
||||
return out
|
||||
}
|
||||
|
||||
/** This file's change for the card: a file that is made is all added, one that goes is all removed. */
|
||||
/**
|
||||
* This file's change for the card: a file that is made is all added, one that goes is all
|
||||
* removed. Bounded before it starts ([textLines]) and while it runs ([diffLines]); it is still
|
||||
* work for a background thread, never for a frame.
|
||||
*/
|
||||
fun FileDiff.view(): DiffView {
|
||||
val all = diffLines(textLines(oldText.orEmpty()), textLines(if (deleted) "" else newText))
|
||||
return DiffView(hunks(all), added = all.count { it.sign == DiffSign.Added }, removed = all.count { it.sign == DiffSign.Removed })
|
||||
val before = oldText.orEmpty()
|
||||
val after = if (deleted) "" else newText
|
||||
val old = textLines(before)
|
||||
val new = textLines(after)
|
||||
val all = diffLines(old.lines, new.lines)
|
||||
val from = lineEnding(before)
|
||||
val to = lineEnding(after)
|
||||
return DiffView(
|
||||
hunks(all),
|
||||
added = all.count { it.sign == DiffSign.Added },
|
||||
removed = all.count { it.sign == DiffSign.Removed },
|
||||
endings = if (from != null && to != null && from != to) from to to else null,
|
||||
cut = old.cut || new.cut,
|
||||
)
|
||||
}
|
||||
|
||||
/** Whether an approval's item shows its `detail` (the call's arguments): always without diffs, and with them when none of them says anything. [views] is null while they are worked out. */
|
||||
fun showsDetail(item: CardItem, views: List<DiffView>?): Boolean = item.diffs.isEmpty() || (views != null && views.none { it.says })
|
||||
|
||||
// Characters a person cannot see, or that would move or break a line: on a card where somebody
|
||||
// decides, each is written as its code point.
|
||||
private fun unseen(c: Char): Boolean = when (c.code) {
|
||||
in 0x00..0x1F, in 0x7F..0x9F -> true
|
||||
0x00AD, 0x061C, 0x180E, 0xFEFF -> true
|
||||
in 0x200B..0x200F, in 0x2028..0x202E, in 0x2060..0x206F -> true
|
||||
in 0xFFF9..0xFFFB -> true
|
||||
else -> false
|
||||
}
|
||||
|
||||
/**
|
||||
* [text] with every invisible, direction-changing or line-breaking character written as
|
||||
* `<U+202E>`: a bidi override, a zero-width space, a line separator, a control. A tab is four
|
||||
* spaces. What is drawn reads as what is written.
|
||||
*/
|
||||
fun visibly(text: String): String {
|
||||
if (text.none { it == '\t' || unseen(it) }) return text
|
||||
return buildString(text.length + 16) {
|
||||
for (c in text) {
|
||||
when {
|
||||
c == '\t' -> append(" ")
|
||||
unseen(c) -> append("<U+").append(c.code.toString(16).uppercase().padStart(4, '0')).append('>')
|
||||
else -> append(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -365,3 +365,52 @@ class RangeReader(private val length: Long, private val block: Int = BLOCK, priv
|
||||
const val BLOCK = 512 * 1024
|
||||
}
|
||||
}
|
||||
|
||||
/** How much of a file's beginning says what it is. */
|
||||
const val SNIFF_BYTES = 16
|
||||
|
||||
private fun ByteArray.begins(at: Int, vararg bytes: Int): Boolean =
|
||||
size >= at + bytes.size && bytes.indices.all { (this[at + it].toInt() and 0xFF) == bytes[it] }
|
||||
|
||||
private fun ByteArray.says(at: Int, word: String): Boolean = size >= at + word.length && word.indices.all { this[at + it].toInt() == word[it].code }
|
||||
|
||||
/**
|
||||
* What a file is by its first bytes ([head], [SNIFF_BYTES] of them are enough), for the kinds a
|
||||
* chat hands out: pictures, sound, video, a PDF, a 3D model, the archives. Null for anything
|
||||
* else -- text has no mark, and a guess is worse than none.
|
||||
*/
|
||||
fun sniffType(head: ByteArray): String? = when {
|
||||
head.begins(0, 0x89, 0x50, 0x4E, 0x47) -> "image/png"
|
||||
head.begins(0, 0xFF, 0xD8, 0xFF) -> "image/jpeg"
|
||||
head.says(0, "GIF8") -> "image/gif"
|
||||
head.says(0, "RIFF") && head.says(8, "WEBP") -> "image/webp"
|
||||
head.says(0, "RIFF") && head.says(8, "WAVE") -> "audio/wav"
|
||||
head.says(0, "BM") -> "image/bmp"
|
||||
head.says(0, "%PDF-") -> "application/pdf"
|
||||
head.says(0, "OggS") -> "audio/ogg"
|
||||
head.says(0, "fLaC") -> "audio/flac"
|
||||
head.says(0, "ID3") || head.begins(0, 0xFF, 0xFB) || head.begins(0, 0xFF, 0xF3) -> "audio/mpeg"
|
||||
head.says(4, "ftyp") -> "video/mp4"
|
||||
head.begins(0, 0x1A, 0x45, 0xDF, 0xA3) -> "video/webm"
|
||||
head.says(0, "glTF") -> "model/gltf-binary"
|
||||
head.says(0, "PK\u0003\u0004") -> "application/zip"
|
||||
head.begins(0, 0x1F, 0x8B) -> "application/gzip"
|
||||
head.says(0, "7z") && head.begins(2, 0xBC, 0xAF) -> "application/x-7z-compressed"
|
||||
else -> null
|
||||
}
|
||||
|
||||
/**
|
||||
* The type a copy is offered to another app as ("Open with"): what its bytes say, else what its
|
||||
* name's extension says ([named], the platform's table), else anything. A zip is the one mark
|
||||
* that many kinds of document carry inside, so there a known extension is the better word. The
|
||||
* type the instance said is not asked: it is not the phone's own reading.
|
||||
*/
|
||||
fun openType(head: ByteArray, named: String?): String {
|
||||
val sniffed = sniffType(head)
|
||||
val byName = named?.ifBlank { null }
|
||||
return when {
|
||||
sniffed == null -> byName ?: "*/*"
|
||||
sniffed == "application/zip" && byName != null -> byName
|
||||
else -> sniffed
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user