default editor

check if $EDITOR exists, then use it.
otherwise, if /usr/bin/sensible-editor exists, use it.
otherwise, if neither of the above works, use 'vi'.

As suggested by cogburnd02
This commit is contained in:
Sina Siadat 2015-05-07 10:33:00 +04:30
parent 6762374f5d
commit 2f9fa82417
3 changed files with 22 additions and 117 deletions

View File

@ -8,7 +8,7 @@ eton is a note-taking cli tool.
## Examples
# print the complete help
# display the help message
eton -h
# quick add

View File

@ -22,6 +22,7 @@ var globalOpts Options
// const orderby = "-frequency, -mark, CASE WHEN updated_at IS NULL THEN created_at ELSE updated_at END DESC"
const orderby = "CASE WHEN updated_at IS NULL THEN created_at ELSE updated_at END DESC"
const defaultEditor = "vi"
func cmdShow(db *sql.DB, opts Options) bool {
if len(opts.IDs) == 0 && len(opts.Aliases) == 0 {
@ -60,12 +61,14 @@ func cmdCat(db *sql.DB, opts Options) bool {
}
func cmdMount(db *sql.DB, opts Options) bool {
// check if opts.MountPoint directory exists, if it exists do nothing, if it doesn't create it and continue
globalDB = db
globalOpts = opts
globalOpts.Offset = 0
globalOpts.Limit = 40
Mount(opts.MountPoint)
log.Println("Not implemented yet")
/*
globalDB = db
globalOpts = opts
globalOpts.Offset = 0
globalOpts.Limit = 40
Mount(opts.MountPoint)
*/
return true
}
@ -362,11 +365,20 @@ func cmdUnmark(db *sql.DB, opts Options) bool {
/******************************************************************************/
func openEditor(filepath string) {
var cmd *exec.Cmd
editor := os.Getenv("EDITOR")
if len(editor) == 0 {
editor = "vim"
if len(editor) > 0 {
cmd = exec.Command("/usr/bin/env", editor, filepath)
} else {
if _, err := os.Stat("/usr/bin/sensible-editor"); err == nil {
cmd = exec.Command("/usr/bin/sensible-editor", filepath)
} else {
cmd = exec.Command("/usr/bin/env", defaultEditor, filepath)
}
}
cmd := exec.Command("/usr/bin/env", editor, filepath)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Run()

View File

@ -1,107 +0,0 @@
package main
import (
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
"log"
"os"
"os/signal"
//"path/filepath"
)
type HelloFs struct {
pathfs.FileSystem
}
func (me *HelloFs) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
log.Printf("GetAttr for %v\n", name)
switch name {
case "":
return &fuse.Attr{
Mode: fuse.S_IFDIR | 0755,
}, fuse.OK
default:
attr := findAttributeByAliasOrID(globalDB, name)
size := 0
if attr.GetID() > 0 {
size = len(attr.GetValue())
}
return &fuse.Attr{
Mode: fuse.S_IFREG | 0644,
Size: uint64(size),
}, fuse.OK
}
return nil, fuse.ENOENT
}
func (me *HelloFs) OpenDir(name string, context *fuse.Context) (c []fuse.DirEntry, code fuse.Status) {
log.Printf("OpenDir for %v\n", name)
if name == "" {
//c = []fuse.DirEntry{{Name: "file.txt", Mode: fuse.S_IFREG}}
attrs := listWithFilters(globalDB, globalOpts)
c = make([]fuse.DirEntry, len(attrs), len(attrs))
for i, attr := range attrs {
var d fuse.DirEntry
d.Name = attr.GetIdentifier()
//log.Println(d.Name)
d.Mode = fuse.S_IFREG | 0644
c[i] = fuse.DirEntry(d)
//c = append(c, )
}
return c, fuse.OK
}
return nil, fuse.ENOENT
}
func (me *HelloFs) Open(name string, flags uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) {
log.Printf("Open for %v\n", name)
if flags&fuse.O_ANYWRITE != 0 {
return nil, fuse.EPERM
}
attr := findAttributeByAliasOrID(globalDB, name)
if attr.GetID() <= 0 {
return nil, fuse.ENOENT
}
bytes := []byte(attr.GetValue())
return nodefs.NewDataFile(bytes), fuse.OK
}
func Mount(mountpoint string) {
log.Println("NOTE: This is just an experiment")
log.Println("mounting on", mountpoint)
if _, err := os.Stat(mountpoint); err == nil {
log.Println("directory exists:", mountpoint)
log.Println("move directory and try again")
return
} else {
os.Mkdir(mountpoint, os.ModeDir)
}
nfs := pathfs.NewPathNodeFs(&HelloFs{FileSystem: pathfs.NewDefaultFileSystem()}, nil)
server, _, err := nodefs.MountRoot(mountpoint, nfs.Root(), nil)
check(err)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer func() {
server.Unmount()
//mountpointAbsolutepath, _ := filepath.Abs(mountpoint)
os.Remove(mountpoint)
log.Println("Removed mount point", mountpoint)
}()
go func() {
for _ = range c {
// CTRL-c
// Do nothing, this will continue executing the rest of the code
server.Unmount()
}
}()
server.Serve()
}