browsers.nim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements a simple proc for opening URLs with the user's
  10. ## default browser.
  11. ##
  12. ## Unstable API.
  13. import std/private/since
  14. import strutils
  15. when defined(windows):
  16. import winlean
  17. from os import absolutePath
  18. else:
  19. import os
  20. when not defined(osx):
  21. import osproc
  22. const osOpenCmd* =
  23. when defined(macos) or defined(macosx) or defined(windows): "open" else: "xdg-open" ## \
  24. ## Alias for the operating system specific *"open"* command,
  25. ## `"open"` on OSX, MacOS and Windows, `"xdg-open"` on Linux, BSD, etc.
  26. proc prepare(s: string): string =
  27. if s.contains("://"):
  28. result = s
  29. else:
  30. result = "file://" & absolutePath(s)
  31. proc openDefaultBrowserImpl(url: string) =
  32. when defined(windows):
  33. var o = newWideCString(osOpenCmd)
  34. var u = newWideCString(prepare url)
  35. discard shellExecuteW(0'i32, o, u, nil, nil, SW_SHOWNORMAL)
  36. elif defined(macosx):
  37. discard execShellCmd(osOpenCmd & " " & quoteShell(prepare url))
  38. else:
  39. var u = quoteShell(prepare url)
  40. if execShellCmd(osOpenCmd & " " & u) == 0: return
  41. for b in getEnv("BROWSER").split(PathSep):
  42. try:
  43. # we use `startProcess` here because we don't want to block!
  44. discard startProcess(command = b, args = [url], options = {poUsePath})
  45. return
  46. except OSError:
  47. discard
  48. proc openDefaultBrowser*(url: string) =
  49. ## Opens `url` with the user's default browser. This does not block.
  50. ## The URL must not be empty string, to open on a blank page see `openDefaultBrowser()`.
  51. ##
  52. ## Under Windows, `ShellExecute` is used. Under Mac OS X the `open`
  53. ## command is used. Under Unix, it is checked if `xdg-open` exists and
  54. ## used if it does. Otherwise the environment variable `BROWSER` is
  55. ## used to determine the default browser to use.
  56. ##
  57. ## This proc doesn't raise an exception on error, beware.
  58. ##
  59. ## .. code-block:: nim
  60. ## block: openDefaultBrowser("https://nim-lang.org")
  61. doAssert url.len > 0, "URL must not be empty string"
  62. openDefaultBrowserImpl(url)
  63. proc openDefaultBrowser*() {.since: (1, 1).} =
  64. ## Opens the user's default browser without any `url` (blank page). This does not block.
  65. ## Implements IETF RFC-6694 Section 3, "about:blank" must be reserved for a blank page.
  66. ##
  67. ## Under Windows, `ShellExecute` is used. Under Mac OS X the `open`
  68. ## command is used. Under Unix, it is checked if `xdg-open` exists and
  69. ## used if it does. Otherwise the environment variable `BROWSER` is
  70. ## used to determine the default browser to use.
  71. ##
  72. ## This proc doesn't raise an exception on error, beware.
  73. ##
  74. ## **See also:**
  75. ##
  76. ## * https://tools.ietf.org/html/rfc6694#section-3
  77. ##
  78. ## .. code-block:: nim
  79. ## block: openDefaultBrowser()
  80. openDefaultBrowserImpl("http:about:blank") # See IETF RFC-6694 Section 3.