Browse Source

Fix launching editor in Windows

Fixes the start command.
Saves the temporary file with .txt extension, so that Windows can correctly handle it.
tags/v0.3
Matt Baer 8 years ago
parent
commit
4a808d8c2d
3 changed files with 71 additions and 2 deletions
  1. +67
    -0
      utils/tempfile.go
  2. +2
    -1
      writeas/posts.go
  3. +2
    -1
      writeas/posts_win.go

+ 67
- 0
utils/tempfile.go View File

@@ -0,0 +1,67 @@
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fileutils

import (
"os"
"path/filepath"
"strconv"
"sync"
"time"
)

// Random number state.
// We generate random temporary file names so that there's a good
// chance the file doesn't exist yet - keeps the number of tries in
// TempFile to a minimum.
var rand uint32
var randmu sync.Mutex

func reseed() uint32 {
return uint32(time.Now().UnixNano() + int64(os.Getpid()))
}

func nextSuffix() string {
randmu.Lock()
r := rand
if r == 0 {
r = reseed()
}
r = r*1664525 + 1013904223 // constants from Numerical Recipes
rand = r
randmu.Unlock()
return strconv.Itoa(int(1e9 + r%1e9))[1:]
}

// TempFile creates a new temporary file in the directory dir
// with a name beginning with prefix and ending with ext, opens the
// file for reading and writing, and returns the resulting *os.File.
// If dir is the empty string, TempFile uses the default directory
// for temporary files (see os.TempDir).
// Multiple programs calling TempFile simultaneously
// will not choose the same file. The caller can use f.Name()
// to find the pathname of the file. It is the caller's responsibility
// to remove the file when no longer needed.
func TempFile(dir, prefix, ext string) (f *os.File, err error) {
if dir == "" {
dir = os.TempDir()
}

nconflict := 0
for i := 0; i < 10000; i++ {
name := filepath.Join(dir, prefix+nextSuffix()+"."+ext)
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if os.IsExist(err) {
if nconflict++; nconflict > 10 {
randmu.Lock()
rand = reseed()
randmu.Unlock()
}
continue
}
break
}
return
}

+ 2
- 1
writeas/posts.go View File

@@ -99,7 +99,7 @@ func getPosts() *[]Post {
}

func composeNewPost() *[]byte {
f, err := ioutil.TempFile(os.TempDir(), "WApost")
f, err := fileutils.TempFile(os.TempDir(), "WApost", "txt")
if err != nil {
if DEBUG {
panic(err)
@@ -109,6 +109,7 @@ func composeNewPost() *[]byte {
}
}
defer os.Remove(f.Name())
f.Close()

cmd := editPostCmd(f.Name())
if cmd == nil {


+ 2
- 1
writeas/posts_win.go View File

@@ -17,5 +17,6 @@ func parentDataDir() string {
}

func editPostCmd(fname string) *exec.Cmd {
return exec.Command(fname)
// NOTE this won't work if fname contains spaces.
return exec.Command("cmd", "/C start /WAIT "+fname)
}

Loading…
Cancel
Save