ask user before creating a clipboard larger than 5 MB

This commit is contained in:
2021-10-29 15:11:29 +02:00
parent fbd378cbf4
commit 86b70f43ac
2 changed files with 45 additions and 0 deletions

View File

@@ -12,6 +12,7 @@ from PyQt6.QtGui import QMouseEvent
from PyQt6.QtWidgets import * from PyQt6.QtWidgets import *
from ScaledScrollBar import ScaledScrollBar from ScaledScrollBar import ScaledScrollBar
from conversion import humanbytes
from highlight import Highlight from highlight import Highlight
from highlight_regex import HighlightRegex from highlight_regex import HighlightRegex
from highlight_selection import HighlightSelection from highlight_selection import HighlightSelection
@@ -227,6 +228,20 @@ class InnerBigText(QWidget):
if self.selection_highlight.start_byte != self.selection_highlight.end_byte: if self.selection_highlight.start_byte != self.selection_highlight.end_byte:
start = min(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) 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 <b>%s</b> 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) selected_text = self.model.read_range(start, end)
cb = QApplication.clipboard() cb = QApplication.clipboard()
cb.setText(selected_text) cb.setText(selected_text)

30
conversion.py Normal file
View File

@@ -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)