Add an elide function for shortening text
This commit is contained in:
@@ -40,7 +40,7 @@ from PyQt5.QtCore import QCoreApplication, QUrl
|
|||||||
from PyQt5.QtGui import QColor, QDesktopServices
|
from PyQt5.QtGui import QColor, QDesktopServices
|
||||||
|
|
||||||
from novelwriter.constants import nwConst, nwLabels, nwUnicode, trConst
|
from novelwriter.constants import nwConst, nwLabels, nwUnicode, trConst
|
||||||
from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout
|
from novelwriter.enum import nwItemClass, nwItemLayout, nwItemType
|
||||||
from novelwriter.error import logException
|
from novelwriter.error import logException
|
||||||
|
|
||||||
if TYPE_CHECKING: # pragma: no cover
|
if TYPE_CHECKING: # pragma: no cover
|
||||||
@@ -279,6 +279,13 @@ def simplified(text: str) -> str:
|
|||||||
return " ".join(str(text).strip().split())
|
return " ".join(str(text).strip().split())
|
||||||
|
|
||||||
|
|
||||||
|
def elide(text: str, length: int) -> str:
|
||||||
|
"""Elide a piece of text to a maximum length."""
|
||||||
|
if len(text) > (cut := max(4, length)):
|
||||||
|
return f"{text[:cut-4].rstrip()} ..."
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
def yesNo(value: int | bool | None) -> Literal["yes", "no"]:
|
def yesNo(value: int | bool | None) -> Literal["yes", "no"]:
|
||||||
"""Convert a boolean evaluated variable to a yes or no."""
|
"""Convert a boolean evaluated variable to a yes or no."""
|
||||||
return "yes" if value else "no"
|
return "yes" if value else "no"
|
||||||
|
|||||||
@@ -26,12 +26,14 @@ from __future__ import annotations
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from PyQt5.QtCore import pyqtSlot
|
from PyQt5.QtCore import pyqtSlot
|
||||||
from PyQt5.QtWidgets import QWidget, QGridLayout, QLabel
|
from PyQt5.QtWidgets import QGridLayout, QLabel, QWidget
|
||||||
|
|
||||||
from novelwriter import CONFIG, SHARED
|
from novelwriter import CONFIG, SHARED
|
||||||
from novelwriter.constants import trConst, nwLabels
|
from novelwriter.common import elide
|
||||||
|
from novelwriter.constants import nwLabels, trConst
|
||||||
from novelwriter.types import (
|
from novelwriter.types import (
|
||||||
QtAlignLeft, QtAlignLeftBase, QtAlignRight, QtAlignRightBase, QtAlignRightMiddle
|
QtAlignLeft, QtAlignLeftBase, QtAlignRight, QtAlignRightBase,
|
||||||
|
QtAlignRightMiddle
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -236,10 +238,6 @@ class GuiItemDetails(QWidget):
|
|||||||
# Label
|
# Label
|
||||||
# =====
|
# =====
|
||||||
|
|
||||||
label = nwItem.itemName
|
|
||||||
if len(label) > 100:
|
|
||||||
label = label[:96].rstrip()+" ..."
|
|
||||||
|
|
||||||
if nwItem.isFileType():
|
if nwItem.isFileType():
|
||||||
if nwItem.isActive:
|
if nwItem.isActive:
|
||||||
self.labelIcon.setPixmap(SHARED.theme.getPixmap("checked", (iPx, iPx)))
|
self.labelIcon.setPixmap(SHARED.theme.getPixmap("checked", (iPx, iPx)))
|
||||||
@@ -248,7 +246,7 @@ class GuiItemDetails(QWidget):
|
|||||||
else:
|
else:
|
||||||
self.labelIcon.setPixmap(SHARED.theme.getPixmap("noncheckable", (iPx, iPx)))
|
self.labelIcon.setPixmap(SHARED.theme.getPixmap("noncheckable", (iPx, iPx)))
|
||||||
|
|
||||||
self.labelData.setText(label)
|
self.labelData.setText(elide(nwItem.itemName, 100))
|
||||||
|
|
||||||
# Status
|
# Status
|
||||||
# ======
|
# ======
|
||||||
|
|||||||
@@ -27,14 +27,12 @@ from xml.etree import ElementTree as ET
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from mocked import causeOSError
|
|
||||||
from PyQt5.QtCore import QUrl
|
from PyQt5.QtCore import QUrl
|
||||||
from PyQt5.QtGui import QColor, QDesktopServices
|
from PyQt5.QtGui import QColor, QDesktopServices
|
||||||
from tools import writeFile
|
|
||||||
|
|
||||||
from novelwriter.common import (
|
from novelwriter.common import (
|
||||||
NWConfigParser, checkBool, checkFloat, checkInt, checkIntTuple, checkPath,
|
NWConfigParser, checkBool, checkFloat, checkInt, checkIntTuple, checkPath,
|
||||||
checkString, checkStringNone, checkUuid, cssCol, formatFileFilter,
|
checkString, checkStringNone, checkUuid, cssCol, elide, formatFileFilter,
|
||||||
formatInt, formatTime, formatTimeStamp, formatVersion, fuzzyTime,
|
formatInt, formatTime, formatTimeStamp, formatVersion, fuzzyTime,
|
||||||
getFileSize, hexToInt, isHandle, isItemClass, isItemLayout, isItemType,
|
getFileSize, hexToInt, isHandle, isItemClass, isItemLayout, isItemType,
|
||||||
isListInstance, isTitleTag, jsonEncode, makeFileNameSafe, minmax,
|
isListInstance, isTitleTag, jsonEncode, makeFileNameSafe, minmax,
|
||||||
@@ -42,6 +40,9 @@ from novelwriter.common import (
|
|||||||
xmlIndent, yesNo
|
xmlIndent, yesNo
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from tests.mocked import causeOSError
|
||||||
|
from tests.tools import writeFile
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.base
|
@pytest.mark.base
|
||||||
def testBaseCommon_checkStringNone():
|
def testBaseCommon_checkStringNone():
|
||||||
@@ -387,6 +388,26 @@ def testBaseCommon_simplified():
|
|||||||
# END Test testBaseCommon_simplified
|
# END Test testBaseCommon_simplified
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.base
|
||||||
|
def testBaseCommon_elide():
|
||||||
|
"""Test the elide function."""
|
||||||
|
assert elide("Hello World!", 12) == "Hello World!"
|
||||||
|
assert elide("Hello World!", 11) == "Hello W ..."
|
||||||
|
assert elide("Hello World!", 10) == "Hello ..."
|
||||||
|
assert elide("Hello World!", 9) == "Hello ..."
|
||||||
|
assert elide("Hello World!", 8) == "Hell ..."
|
||||||
|
assert elide("Hello World!", 7) == "Hel ..."
|
||||||
|
assert elide("Hello World!", 6) == "He ..."
|
||||||
|
assert elide("Hello World!", 5) == "H ..."
|
||||||
|
assert elide("Hello World!", 4) == " ..."
|
||||||
|
assert elide("Hello World!", 3) == " ..."
|
||||||
|
assert elide("Hello World!", 2) == " ..."
|
||||||
|
assert elide("Hello World!", 1) == " ..."
|
||||||
|
assert elide("Hello World!", 0) == " ..."
|
||||||
|
|
||||||
|
# END Test testBaseCommon_elide
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.base
|
@pytest.mark.base
|
||||||
def testBaseCommon_yesNo():
|
def testBaseCommon_yesNo():
|
||||||
"""Test the yesNo function."""
|
"""Test the yesNo function."""
|
||||||
|
|||||||
Reference in New Issue
Block a user