browsers.nim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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(nimPreviewSlimSystem):
  16. import std/assertions
  17. when defined(windows):
  18. import winlean
  19. when defined(nimPreviewSlimSystem):
  20. import std/widestrs
  21. from os import absolutePath
  22. else:
  23. import os
  24. when not defined(osx):
  25. import 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 openDefaultBrowserImplPrep(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 openDefaultBrowserImpl(url: string) =
  54. openDefaultBrowserImplPrep(prepare url)
  55. proc openDefaultBrowser*(url: string) =
  56. ## Opens `url` with the user's default browser. This does not block.
  57. ## The URL must not be empty string, to open on a blank page see `openDefaultBrowser()`.
  58. ##
  59. ## Under Windows, `ShellExecute` is used. Under Mac OS X the `open`
  60. ## command is used. Under Unix, it is checked if `xdg-open` exists and
  61. ## used if it does. Otherwise the environment variable `BROWSER` is
  62. ## used to determine the default browser to use.
  63. ##
  64. ## This proc doesn't raise an exception on error, beware.
  65. ##
  66. ## ```nim
  67. ## block: openDefaultBrowser("https://nim-lang.org")
  68. ## ```
  69. doAssert url.len > 0, "URL must not be empty string"
  70. openDefaultBrowserImpl(url)
  71. proc openDefaultBrowser*() {.since: (1, 1).} =
  72. ## Opens the user's default browser without any `url` (blank page). This does not block.
  73. ## Implements IETF RFC-6694 Section 3, "about:blank" must be reserved for a blank page.
  74. ##
  75. ## Under Windows, `ShellExecute` is used. Under Mac OS X the `open`
  76. ## command is used. Under Unix, it is checked if `xdg-open` exists and
  77. ## used if it does. Otherwise the environment variable `BROWSER` is
  78. ## used to determine the default browser to use.
  79. ##
  80. ## This proc doesn't raise an exception on error, beware.
  81. ##
  82. ## ```nim
  83. ## block: openDefaultBrowser()
  84. ## ```
  85. ##
  86. ## **See also:**
  87. ##
  88. ## * https://tools.ietf.org/html/rfc6694#section-3
  89. openDefaultBrowserImplPrep("about:blank") # See IETF RFC-6694 Section 3.