Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 66 additions & 36 deletions gitfourchette/filelists/filelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,43 +75,68 @@ def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIn
text = painter.fontMetrics().elidedText(fullText, option.textElideMode, textRect.width())

# Split path into directory and filename for better readability
dirPortion = None
filePortion = None

if '/' in fullText:
slashesInFull = fullText.count('/')
slashesInElided = text.count('/')

if slashesInFull > slashesInElided:
# A slash was elided - gray everything up to the ellipsis
ellipsisPos = text.find('\u2026')
dirPortion = text[:ellipsisPos + 1]
filePortion = text[ellipsisPos + 1:]
elif slashesInElided > 0:
# No slash elided - gray up to the last slash
lastSlash = text.rfind('/')
dirPortion = text[:lastSlash + 1]
filePortion = text[lastSlash + 1:]

if dirPortion is not None:
textColor = QPalette.ColorRole.WindowText if not isSelected else QPalette.ColorRole.HighlightedText
dirColor = QPalette.ColorRole.PlaceholderText if not isSelected else textColor

# Draw directory with muted color
mutedColor = option.palette.color(colorGroup, dirColor)
if isSelected:
mutedColor.setAlphaF(.7)
painter.setPen(mutedColor)
painter.drawText(textRect, option.displayAlignment, dirPortion)

# Draw filename with normal color
painter.setPen(option.palette.color(colorGroup, textColor))
dirWidth = painter.fontMetrics().horizontalAdvance(dirPortion)
fileRect = QRect(textRect)
fileRect.setLeft(textRect.left() + dirWidth)
painter.drawText(fileRect, option.displayAlignment, filePortion)
firstPortion = None
secondPortion = None
firstColor = QPalette.ColorRole.PlaceholderText
secondColor = QPalette.ColorRole.WindowText

# Determine split based on style
isFileNameFirst = settings.prefs.pathDisplayStyle == PathDisplayStyle.FileNameFirst

if isFileNameFirst:
try:
firstPortion, secondPortion = text.split('\0')
except ValueError:
firstPortion, secondPortion = text, ""

firstColor = QPalette.ColorRole.WindowText
secondColor = QPalette.ColorRole.PlaceholderText

else:
painter.drawText(textRect, option.displayAlignment, text)
if '/' in fullText:
slashesInFull = fullText.count('/')
slashesInElided = text.count('/')

if slashesInFull > slashesInElided:
# A slash was elided - gray everything up to the ellipsis
ellipsisPos = text.find('\u2026')
firstPortion = text[:ellipsisPos + 1]
secondPortion = text[ellipsisPos + 1:]
elif slashesInElided > 0:
# No slash elided - gray up to the last slash
lastSlash = text.rfind('/')
firstPortion = text[:lastSlash + 1]
secondPortion = text[lastSlash + 1:]

if firstPortion is None:
firstPortion = ""
secondPortion = text

firstColor = QPalette.ColorRole.PlaceholderText
secondColor = QPalette.ColorRole.WindowText

# Draw the parts
if firstPortion:
fg1 = option.palette.color(colorGroup, firstColor if not isSelected else QPalette.ColorRole.HighlightedText)
if firstColor == QPalette.ColorRole.PlaceholderText and isSelected:
fg1 = QColor(option.palette.color(colorGroup, QPalette.ColorRole.HighlightedText))
fg1.setAlphaF(0.7)

painter.setPen(fg1)
painter.drawText(textRect, option.displayAlignment, firstPortion)

# Prepare rect for second part
part1Width = painter.fontMetrics().horizontalAdvance(firstPortion)
textRect.setLeft(textRect.left() + part1Width)

if secondPortion:
fg2 = option.palette.color(colorGroup, secondColor if not isSelected else QPalette.ColorRole.HighlightedText)
if secondColor == QPalette.ColorRole.PlaceholderText and isSelected:
fg2 = QColor(option.palette.color(colorGroup, QPalette.ColorRole.HighlightedText))
fg2.setAlphaF(0.7)

painter.setPen(fg2)
painter.drawText(textRect, option.displayAlignment, secondPortion)

# Highlight search term
if searchTerm and searchTerm in fullText.lower():
Expand All @@ -122,8 +147,11 @@ def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIn
else:
needleLen = len(searchTerm)

# Use original textRect (before we advanced it for the second part)
textRect.setLeft(textRect.left() - part1Width if firstPortion else 0)
SearchBar.highlightNeedle(painter, textRect, text, needlePos, needleLen)


painter.restore()


Expand Down Expand Up @@ -195,6 +223,8 @@ def __init__(self, repoModel: RepoModel, parent: QWidget, navContext: NavContext

def refreshPrefs(self):
self.setVerticalScrollMode(settings.prefs.listViewScrollMode)
nameFirst = settings.prefs.pathDisplayStyle == PathDisplayStyle.FileNameFirst
self.setTextElideMode(Qt.TextElideMode.ElideRight if nameFirst else Qt.TextElideMode.ElideMiddle)

@property
def repo(self) -> Repo:
Expand Down
6 changes: 6 additions & 0 deletions gitfourchette/toolbox/pathutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PathDisplayStyle(enum.IntEnum):
FullPaths = 1
AbbreviateDirs = 2
FileNameOnly = 3
FileNameFirst = 4


def compactPath(path: str) -> str:
Expand All @@ -33,6 +34,11 @@ def abbreviatePath(path: str, style: PathDisplayStyle = PathDisplayStyle.FullPat
else:
splitLong[i] = splitLong[i][0]
return '/'.join(splitLong)
elif style == PathDisplayStyle.FileNameFirst:
split = path.rsplit('/', 1)
if len(split) == 1:
return path
return split[-1] + ' \0' + split[0]
elif style == PathDisplayStyle.FileNameOnly:
return path.rsplit('/', 1)[-1]
else:
Expand Down
1 change: 1 addition & 0 deletions gitfourchette/trtables.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def _init_enums():
PathDisplayStyle.FullPaths : _("Full paths"),
PathDisplayStyle.AbbreviateDirs : _("Abbreviate directories"),
PathDisplayStyle.FileNameOnly : _("Show filename only"),
PathDisplayStyle.FileNameFirst : _("Filename first"),
},

AuthorDisplayStyle: {
Expand Down
3 changes: 3 additions & 0 deletions test/test_filelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ def testFileListChangePathDisplayStyle(tempDir, mainWindow):
triggerContextMenuAction(rw.committedFiles.viewport(), "path display style/full")
assert ["c/c2-2.txt"] == qlvGetRowData(rw.committedFiles)

triggerContextMenuAction(rw.committedFiles.viewport(), "path display style/name first")
assert ["c2-2.txt \0c"] == qlvGetRowData(rw.committedFiles)


def testFileListShowInFolder(tempDir, mainWindow):
wd = unpackRepo(tempDir)
Expand Down