browsers.nim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 # used by the deprecated `openDefaultBrowser()`
  14. import std/strutils
  15. when defined(nimPreviewSlimSystem):
  16. import std/assertions
  17. when defined(windows):
  18. import std/winlean
  19. when defined(nimPreviewSlimSystem):
  20. import std/widestrs
  21. from std/os import absolutePath
  22. else:
  23. import std/os
  24. when not defined(osx):
  25. import std/osproc
  26. const osOpenCmd* =
  27. when defined(macos) or defined(macosx) or defined(windows): "open" else: "xdg-open" ## \
  28. ## Alias for the operating system specific *"open"* command,
  29. ## `"open"` on OSX, MacOS and Windows, `"xdg-open"` on Linux, BSD, etc.
  30. proc prepare(s: string): string =
  31. if s.contains("://"):
  32. result = s
  33. else:
  34. result = "file://" & absolutePath(s)
  35. proc openDefaultBrowserRaw(url: string) =
  36. ## note the url argument should be alreadly prepared, i.e. the url is passed "AS IS"
  37. when defined(windows):
  38. var o = newWideCString(osOpenCmd)
  39. var u = newWideCString(url)
  40. discard shellExecuteW(0'i32, o, u, nil, nil, SW_SHOWNORMAL)
  41. elif defined(macosx):
  42. discard execShellCmd(osOpenCmd & " " & quoteShell(url))
  43. else:
  44. var u = quoteShell(url)
  45. if execShellCmd(osOpenCmd & " " & u) == 0: return
  46. for b in getEnv("BROWSER").split(PathSep):
  47. try:
  48. # we use `startProcess` here because we don't want to block!
  49. discard startProcess(command = b, args = [url], options = {poUsePath})
  50. return
  51. except OSError:
  52. discard
  53. proc openDefaultBrowser*(url: string) =
  54. ## Opens `url` with the user's default browser. This does not block.
  55. ## The URL must not be empty string, to open on a blank page see `openDefaultBrowser()`.
  56. ##
  57. ## Under Windows, `ShellExecute` is used. Under Mac OS X the `open`
  58. ## command is used. Under Unix, it is checked if `xdg-open` exists and
  59. ## used if it does. Otherwise the environment variable `BROWSER` is
  60. ## used to determine the default browser to use.
  61. ##
  62. ## This proc doesn't raise an exception on error, beware.
  63. ##
  64. ## ```nim
  65. ## block: openDefaultBrowser("https://nim-lang.org")
  66. ## ```
  67. doAssert url.len > 0, "URL must not be empty string"
  68. openDefaultBrowserRaw(url)
  69. proc openDefaultBrowser*() {.since: (1, 1), deprecated:
  70. "not implemented, please open with a specific url instead".} =
  71. ## Intends to open the user's default browser without any `url` (blank page).
  72. ## This does not block.
  73. ## Intends to implement IETF RFC-6694 Section 3,
  74. ## ("about:blank" is reserved for a blank page).
  75. ##
  76. ## Beware that this intended behavior is **not** implemented and
  77. ## considered not worthy to implement here.
  78. ##
  79. ## The following describes the behavior of current implementation:
  80. ##
  81. ## - Under Windows, this will only cause a pop-up dialog \
  82. ## asking the assocated application with `about` \
  83. ## (as Windows simply treats `about:` as a protocol like `http`).
  84. ## - Under Mac OS X the `open "about:blank"` command is used.
  85. ## - Under Unix, it is checked if `xdg-open` exists and used \
  86. ## if it does and open the application assocated with `text/html` mime \
  87. ## (not `x-scheme-handler/http`, so maybe html-viewer \
  88. ## other than your default browser is opened). \
  89. ## Otherwise the environment variable `BROWSER` is used \
  90. ## to determine the default browser to use.
  91. ##
  92. ## This proc doesn't raise an exception on error, beware.
  93. ##
  94. ## ```nim
  95. ## block: openDefaultBrowser()
  96. ## ```
  97. ##
  98. ## **See also:**
  99. ##
  100. ## * https://tools.ietf.org/html/rfc6694#section-3
  101. openDefaultBrowserRaw("about:blank") # See IETF RFC-6694 Section 3.