Added line numbers and text selection to blame

This commit is contained in:
Abdelilah El Aissaoui 2022-05-28 16:28:39 +02:00
parent df03edf396
commit 9c1133a292
3 changed files with 93 additions and 60 deletions

View file

@ -0,0 +1,18 @@
package app.extensions
fun Int.toStringWithSpaces(charactersCount: Int): String {
val numberStr = this.toString()
return if (numberStr.count() == charactersCount)
numberStr
else {
val lengthDiff = charactersCount - numberStr.count()
val numberBuilder = StringBuilder()
// Add whitespaces before the numbers
repeat(lengthDiff) {
numberBuilder.append(" ")
}
numberBuilder.append(numberStr)
numberBuilder.toString()
}
}

View file

@ -5,6 +5,8 @@ package app.ui
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.selection.DisableSelection
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
@ -21,6 +23,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import app.extensions.handMouseClickable
import app.extensions.lineAt
import app.extensions.toStringWithSpaces
import app.theme.primaryTextColor
import app.ui.components.PrimaryButton
import app.ui.components.ScrollableLazyColumn
@ -36,58 +39,88 @@ fun Blame(
) {
Column {
Header(filePath, onClose = onClose)
SelectionContainer {
ScrollableLazyColumn(
modifier = Modifier.fillMaxSize()
) {
val contents = blameResult.resultContents
val contentsSize = contents.size()
val contentSizeLengthInString = contentsSize.toString().count()
ScrollableLazyColumn(
modifier = Modifier.fillMaxSize()
) {
val contents = blameResult.resultContents
items(contents.size()) { index ->
val line = contents.lineAt(index)
val author = blameResult.getSourceAuthor(index)
val commit = blameResult.getSourceCommit(index)
items(contentsSize) { index ->
val line = contents.lineAt(index)
val author = blameResult.getSourceAuthor(index)
val commit = blameResult.getSourceCommit(index)
Row(
modifier = Modifier.fillMaxWidth().background(MaterialTheme.colors.background)
.height(IntrinsicSize.Min),
verticalAlignment = Alignment.CenterVertically,
) {
Column(
modifier = Modifier
.width(200.dp)
.fillMaxHeight()
.background(MaterialTheme.colors.surface)
.handMouseClickable { onSelectCommit(commit) },
verticalArrangement = Arrangement.Center,
Row(
modifier = Modifier.fillMaxWidth()
.background(MaterialTheme.colors.background)
.height(IntrinsicSize.Min),
verticalAlignment = Alignment.CenterVertically,
) {
DisableSelection {
LineNumber(
line = index + 1,
highestLineLength = contentSizeLengthInString
)
Column(
modifier = Modifier
.width(200.dp)
.fillMaxHeight()
.background(MaterialTheme.colors.surface)
.handMouseClickable { if (commit != null) onSelectCommit(commit) },
verticalArrangement = Arrangement.Center,
) {
Text(
text = author?.name.orEmpty(),
color = MaterialTheme.colors.primaryTextColor,
maxLines = 1,
modifier = Modifier.padding(start = 16.dp),
fontSize = 12.sp,
)
Text(
text = commit?.shortMessage ?: "Uncommited change",
color = MaterialTheme.colors.primaryTextColor,
maxLines = 1,
modifier = Modifier.padding(start = 16.dp),
fontSize = 10.sp,
)
}
}
Text(
text = author?.name.orEmpty(),
text = line + blameResult.resultContents.lineDelimiter,
color = MaterialTheme.colors.primaryTextColor,
modifier = Modifier.padding(vertical = 8.dp, horizontal = 16.dp),
fontFamily = FontFamily.Monospace,
maxLines = 1,
modifier = Modifier.padding(start = 16.dp),
fontSize = 12.sp,
)
Text(
text = commit.shortMessage,
color = MaterialTheme.colors.primaryTextColor,
maxLines = 1,
modifier = Modifier.padding(start = 16.dp),
fontSize = 10.sp,
)
}
Text(
text = line,
color = MaterialTheme.colors.primaryTextColor,
modifier = Modifier.padding(vertical = 8.dp, horizontal = 16.dp),
fontFamily = FontFamily.Monospace,
maxLines = 1,
)
}
}
}
}
}
@Composable
private fun LineNumber(line: Int, highestLineLength: Int) {
Box(
modifier = Modifier
.fillMaxHeight()
.background(MaterialTheme.colors.surface)
.padding(start = 4.dp, end = 4.dp),
contentAlignment = Alignment.Center
) {
Text(
text = line.toStringWithSpaces(highestLineLength),
color = MaterialTheme.colors.primaryTextColor,
fontFamily = FontFamily.Monospace,
)
}
}
@Composable
fun MinimizedBlame(
filePath: String,

View file

@ -28,6 +28,7 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import app.extensions.toStringWithSpaces
import app.git.DiffEntryType
import app.git.EntryContent
import app.git.StatusEntry
@ -364,12 +365,12 @@ fun DiffLine(highestLineNumberLength: Int, line: Line) {
.height(IntrinsicSize.Min)
) {
val oldLineText = if (line.lineType == LineType.REMOVED || line.lineType == LineType.CONTEXT) {
formattedLineNumber(line.displayOldLineNumber, highestLineNumberLength)
line.displayOldLineNumber.toStringWithSpaces(highestLineNumberLength)
} else
emptyLineNumber(highestLineNumberLength)
val newLineText = if (line.lineType == LineType.ADDED || line.lineType == LineType.CONTEXT) {
formattedLineNumber(line.displayNewLineNumber, highestLineNumberLength)
line.displayNewLineNumber.toStringWithSpaces(highestLineNumberLength)
} else
emptyLineNumber(highestLineNumberLength)
@ -411,24 +412,6 @@ fun LineNumber(text: String) {
)
}
fun formattedLineNumber(number: Int, charactersCount: Int): String {
val numberStr = number.toString()
return if (numberStr.count() == charactersCount)
numberStr
else {
val lengthDiff = charactersCount - numberStr.count()
val numberBuilder = StringBuilder()
// Add whitespaces before the numbers
repeat(lengthDiff) {
numberBuilder.append(" ")
}
numberBuilder.append(numberStr)
numberBuilder.toString()
}
}
fun emptyLineNumber(charactersCount: Int): String {
val numberBuilder = StringBuilder()
// Add whitespaces before the numbers
@ -437,5 +420,4 @@ fun emptyLineNumber(charactersCount: Int): String {
}
return numberBuilder.toString()
}
}