Skip to content

Commit e9322fa

Browse files
authored
Automatic Browser Open (#27)
* feat: open browser window wait for sometime allowing server to run Signed-off-by: Lakshya Singh <[email protected]> * feat: add flag to disable browser opening check for disable auto open flag before launching go routine Signed-off-by: Lakshya Singh <[email protected]> * refactor: log error instead of returning golint needs to check error if any Signed-off-by: Lakshya Singh <[email protected]>
1 parent 6b7fd38 commit e9322fa

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

cli.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type CLI struct {
2424
type CLIOptions struct {
2525
Port int `short:"p" long:"port" description:"TCP port number of this server" default:"3000"`
2626
Theme string `long:"theme" description:"Slide theme or original css file name. default themes: beige, black, blood, league, moon, night, serif, simple, sky, solarized, and white" default:"black.css"`
27+
DisableAutoOpen bool `long:"disable-auto-open" description:"Disable automatic opening of the browser"`
2728
Transition string `long:"transition" description:"Transition effect for slides: default, cube, page, concave, zoom, linear, fade, none" default:"default"`
2829
Separator string `long:"separator" description:"Horizontal slide separator characters" default:"^---"`
2930
VerticalSeparator string `long:"vertical-separator" description:"Vertical slide separator characters" default:"^___"`
@@ -86,6 +87,7 @@ func (cli *CLI) serve(args []string) {
8687
Theme: addExtention(opts.Theme, "css"),
8788
Transition: opts.Transition,
8889
OriginalTheme: originalTheme,
90+
DisableAutoOpen: opts.DisableAutoOpen,
8991
Separator: opts.Separator,
9092
VerticalSeparator: opts.VerticalSeparator,
9193
}

server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type ServerParam struct {
3333
Path string
3434
Theme string
3535
OriginalTheme bool
36+
DisableAutoOpen bool
3637
Transition string
3738
Separator string
3839
VerticalSeparator string
@@ -76,6 +77,9 @@ func (server *Server) Serve(param ServerParam) {
7677
fmt.Printf("master slides can be found at http://*:%d/master/\n", port)
7778
}
7879

80+
if !param.DisableAutoOpen {
81+
go openBrowser(port)
82+
}
7983
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
8084
log.Fatal("ListenAndServe:", err)
8185
}

util.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,32 @@ package revealgo
22

33
import (
44
"fmt"
5+
"os/exec"
6+
"runtime"
57
"strings"
8+
"time"
69
)
710

11+
func openBrowser(port int) {
12+
<-time.After(100 * time.Millisecond)
13+
url := fmt.Sprintf("http://localhost:%d/", port)
14+
var args []string
15+
var cmd string
16+
switch runtime.GOOS {
17+
case "windows":
18+
cmd = "cmd"
19+
args = []string{"/c", "start"}
20+
case "darwin":
21+
cmd = "open"
22+
default: // "linux", "freebsd", "openbsd", "netbsd"
23+
cmd = "xdg-open"
24+
}
25+
args = append(args, url)
26+
if err := exec.Command(cmd, args...).Start(); err != nil {
27+
fmt.Printf("error when trying to open browser: %s", err.Error())
28+
}
29+
}
30+
831
func addExtention(path string, ext string) string {
932
if strings.HasSuffix(path, fmt.Sprintf(".%s", ext)) {
1033
return path

0 commit comments

Comments
 (0)