akiyosi.goneovim/cmd/goneovim/main.go

79 lines
1.5 KiB
Go
Raw Normal View History

2019-11-02 10:54:41 +09:00
package main
import (
"fmt"
"os"
"runtime"
// "runtime/pprof"
2021-05-20 17:37:09 +09:00
// "github.com/felixge/fgprof"
2019-11-02 10:54:41 +09:00
"github.com/akiyosi/goneovim/editor"
"github.com/jessevdk/go-flags"
"github.com/mattn/go-isatty"
"github.com/therecipe/qt/core"
2019-11-02 10:54:41 +09:00
)
func main() {
// parse args
options, args := parseArgs()
2021-08-02 23:08:12 +09:00
if options.Version {
fmt.Println(editor.Version)
os.Exit(0)
2021-08-02 23:08:12 +09:00
}
2021-01-17 11:24:13 +09:00
nofork := options.Nofork
// In Windows, nofork always true
if runtime.GOOS == "windows" {
nofork = true
}
2022-12-28 23:22:33 +09:00
// In MacOS X do not fork when the process is not launched from a tty
if runtime.GOOS == "darwin" {
if !isatty.IsTerminal(os.Stdin.Fd()) {
nofork = true
}
}
2022-12-28 23:22:33 +09:00
// If ssh option specified
if options.Ssh != "" {
nofork = true
}
2022-12-28 23:22:33 +09:00
2021-01-17 11:24:13 +09:00
// start editor
if nofork {
editor.InitEditor(options, args)
} else {
p := core.NewQProcess(nil)
var pid int64
goneovim := core.NewQCoreApplication(len(os.Args), os.Args)
if !p.StartDetached2(
goneovim.ApplicationFilePath(),
append([]string{"--nofork"}, goneovim.Arguments()[1:]...),
"",
pid,
) {
fmt.Println("Unable to fork into background")
os.Exit(1)
}
}
}
// parsArgs parse args
func parseArgs() (editor.Options, []string) {
var options editor.Options
parser := flags.NewParser(&options, flags.HelpFlag|flags.PassDoubleDash)
args, err := parser.ParseArgs(os.Args[1:])
if flagsErr, ok := err.(*flags.Error); ok {
switch flagsErr.Type {
case flags.ErrDuplicatedFlag:
case flags.ErrHelp:
fmt.Println(err)
os.Exit(1)
}
}
2020-12-17 01:31:07 +09:00
return options, args
2019-11-02 10:54:41 +09:00
}