diff --git a/bigtext.py b/bigtext.py index 36cc01a..0c7d5fa 100644 --- a/bigtext.py +++ b/bigtext.py @@ -12,6 +12,7 @@ from PyQt6.QtGui import QMouseEvent from PyQt6.QtWidgets import * from ScaledScrollBar import ScaledScrollBar +from conversion import humanbytes from highlight import Highlight from highlight_regex import HighlightRegex from highlight_selection import HighlightSelection @@ -227,6 +228,20 @@ class InnerBigText(QWidget): if self.selection_highlight.start_byte != self.selection_highlight.end_byte: start = min(self.selection_highlight.start_byte, self.selection_highlight.end_byte) end = max(self.selection_highlight.start_byte, self.selection_highlight.end_byte) + if end - start > (1024 ** 2) * 5: + you_sure = QMessageBox( + QMessageBox.Icon.Warning, + self.tr("copy to clipboard"), + self.tr( + "You are about to copy %s to the clipboard. Are you sure you want to do that?" % humanbytes( + end - start))) + you_sure.setStandardButtons(QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No) + you_sure.setDefaultButton(QMessageBox.StandardButton.No) + result = you_sure.exec() + if result != QMessageBox.StandardButton.Yes: + # abort + return + selected_text = self.model.read_range(start, end) cb = QApplication.clipboard() cb.setText(selected_text) diff --git a/conversion.py b/conversion.py new file mode 100644 index 0000000..ff8009b --- /dev/null +++ b/conversion.py @@ -0,0 +1,30 @@ +import unittest + + +def humanbytes(bytes: int) -> str: + """non-localized conversion of bytes to human readable strings""" + powers = {0: 'bytes', 1: 'KB', 2: 'MB', 3: 'GB', 4: 'TB', 5: 'PB', 6: 'EB'} + power = 1 + result = "%d bytes" % bytes + while bytes >= 1024 ** power and power in powers: + result = "%.3f" % (bytes / (1024 ** power)) + result = result.rstrip("0") + result = result.rstrip(".") + result = result + " " + powers[power] + power = power + 1 + return result + + +class TestLogFileModel(unittest.TestCase): + + def test_humanbytes(self): + inputs = { + 0: "0 bytes", + 1023: "1023 bytes", + 1024: "1 KB", + 1048575: "1023.999 KB", + 1048576: "1 MB", + } + for input in inputs.keys(): + actual = humanbytes(input) + self.assertEqual(inputs[input], actual)