mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Basic selectable todo functionality working
Can: - move between todo items - toggle checked/unchecked state Cannot: - persiste changes to file - add items - delete items
This commit is contained in:
@@ -3,16 +3,26 @@ package wtf
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
)
|
||||
|
||||
func ConfigDir() (string, error) {
|
||||
configDir, err := ExpandHomeDir("~/.wtf/")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return configDir, nil
|
||||
}
|
||||
|
||||
// CreateConfigDir creates the .wtf directory in the user's home dir
|
||||
func CreateConfigDir() bool {
|
||||
homeDir, _ := ExpandHomeDir("~/.wtf/")
|
||||
configDir, _ := ConfigDir()
|
||||
|
||||
if _, err := os.Stat(homeDir); os.IsNotExist(err) {
|
||||
err := os.Mkdir(homeDir, os.ModePerm)
|
||||
if _, err := os.Stat(configDir); os.IsNotExist(err) {
|
||||
err := os.Mkdir(configDir, os.ModePerm)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -21,6 +31,34 @@ func CreateConfigDir() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// CreateFile creates the named file in the config directory, if it does not already exist.
|
||||
// If the file exists it does not recreate it.
|
||||
// If successful, eturns the absolute path to the file
|
||||
// If unsuccessful, returns an error
|
||||
func CreateFile(fileName string) (string, error) {
|
||||
configDir, err := ConfigDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
filePath := fmt.Sprintf("%s/%s", configDir, fileName)
|
||||
|
||||
// Check if the file already exists; if it does not, create it
|
||||
_, err = os.Stat(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
_, err = os.Create(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
return filePath, nil
|
||||
}
|
||||
|
||||
// LoadConfigFile loads the config.yml file to configure the app
|
||||
func LoadConfigFile(filePath string) *config.Config {
|
||||
absPath, _ := ExpandHomeDir(filePath)
|
||||
@@ -34,3 +72,20 @@ func LoadConfigFile(filePath string) *config.Config {
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
|
||||
func ReadFile(fileName string) (string, error) {
|
||||
configDir, err := ConfigDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
filePath := fmt.Sprintf("%s/%s", configDir, fileName)
|
||||
|
||||
bytes, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(bytes), nil
|
||||
}
|
||||
|
||||
10
wtf/utils.go
10
wtf/utils.go
@@ -82,6 +82,16 @@ func Now() time.Time {
|
||||
return time.Now().Local()
|
||||
}
|
||||
|
||||
func ReadYamlFile(filePath string) ([]byte, error) {
|
||||
file, err := ioutil.ReadFile(filePath)
|
||||
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func RightAlignFormat(view *tview.TextView) string {
|
||||
_, _, w, _ := view.GetInnerRect()
|
||||
return fmt.Sprintf("%%%ds", w-1)
|
||||
|
||||
Reference in New Issue
Block a user