string.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ########################################################################
  2. # Searx-Qt - Lightweight desktop application for Searx.
  3. # Copyright (C) 2020-2022 CYBERDEViL
  4. #
  5. # This file is part of Searx-Qt.
  6. #
  7. # Searx-Qt is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # Searx-Qt is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. #
  20. ########################################################################
  21. from searxqt.translations import _
  22. def boolToStr(state):
  23. return _("Yes") if state else _("No")
  24. def listToStr(list_):
  25. return ", ".join(list_)
  26. def formatFileSize(fileSize):
  27. """
  28. @param fileSize: File size in bytes to format to string.
  29. @type fileSize: uint
  30. """
  31. sizeStr = ""
  32. KiB = 1024
  33. MiB = KiB ** 2
  34. GiB = KiB ** 3
  35. TiB = KiB ** 4
  36. if fileSize > TiB: # TiB
  37. sizeStr = f"{fileSize / TiB:.2f} TB"
  38. elif fileSize > GiB: # GiB
  39. sizeStr = f"{fileSize / GiB:.2f} GB"
  40. elif fileSize > MiB: # MiB
  41. sizeStr = f"{fileSize / MiB:.2f} MB"
  42. elif fileSize > KiB: # KiB
  43. sizeStr = f"{fileSize / KiB:.2f} KB"
  44. else:
  45. sizeStr = f"{fileSize} Bytes"
  46. return sizeStr
  47. def formatFileCount(fileCount):
  48. """
  49. @param fileCount: File count to format to string.
  50. @type fileCount: uint
  51. """
  52. if fileCount == 1:
  53. return "1 " + _("file")
  54. return f"{fileCount} {_('files')}"