github.com/jmigpin/editor@v1.6.0/core/internalcmds/newfile.go (about) 1 package internalcmds 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/jmigpin/editor/core" 9 ) 10 11 func NewFile(args *core.InternalCmdArgs) error { 12 if len(args.Part.Args) != 2 { 13 return fmt.Errorf("missing filename") 14 } 15 name := args.Part.Args[1].String() 16 17 // erow always defined (row cmd) 18 erow := args.ERow 19 20 // directory 21 dir := erow.Info.Name() 22 if !erow.Info.IsDir() { 23 dir = filepath.Dir(dir) 24 } 25 26 filename := filepath.Join(dir, name) 27 28 _, err := os.Stat(filename) 29 if !os.IsNotExist(err) { 30 return fmt.Errorf("already exists: %v", filename) 31 } 32 f, err := os.Create(filename) 33 if err != nil { 34 return err 35 } 36 f.Close() 37 38 info := args.Ed.ReadERowInfo(filename) 39 40 rowPos := erow.Row.PosBelow() 41 erow2, err := core.NewLoadedERow(info, rowPos) 42 if err != nil { 43 return err 44 } 45 erow2.Flash() 46 47 return nil 48 }