Skip to content

Commit

Permalink
feat: jjui --config opens config file in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
idursun committed Mar 7, 2025
1 parent b7a30ac commit bf1c9b1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
13 changes: 10 additions & 3 deletions cmd/jjui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/idursun/jjui/internal/config"
"github.com/idursun/jjui/internal/ui/context"
"os"

Expand All @@ -12,9 +13,15 @@ import (
var Version = "unknown"

func main() {
if len(os.Args) > 1 && os.Args[1] == "--version" {
fmt.Printf("jjui version %s\n", Version)
os.Exit(0)
if len(os.Args) > 1 {
switch os.Args[1] {
case "--version":
fmt.Printf("jjui version %s\n", Version)
os.Exit(0)
case "--config":
exitCode := config.Edit()
os.Exit(exitCode)
}
}
location := os.Getenv("PWD")
if len(os.Args) > 1 {
Expand Down
69 changes: 61 additions & 8 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,90 @@ package config

import (
"github.com/BurntSushi/toml"
"log"
"os"
"os/exec"
"path"
"path/filepath"
)

type Config struct {
Keys KeyMappings[keys] `toml:"keys"`
}

func findConfig() (string, error) {
func getConfigFilePath() string {
configDir, err := os.UserConfigDir()
if err != nil {
return "", err
return ""
}
configPath := filepath.Join(configDir, "jjui", "config.toml")
return filepath.Join(configDir, "jjui", "config.toml")
}

_, err = os.Stat(configPath)
if err != nil {
return "", err
func getDefaultEditor() string {
editor := os.Getenv("EDITOR")
if editor == "" {
editor = os.Getenv("VISUAL")
}

// Fallback to common editors if not set
if editor == "" {
candidates := []string{"nano", "vim", "vi", "notepad.exe"} // Windows fallback
for _, candidate := range candidates {
if p, err := exec.LookPath(candidate); err == nil {
editor = p
break
}
}
}
return configPath, nil

return editor
}

func Load() Config {
defaultConfig := Config{Keys: DefaultKeyMappings}
configFile, err := findConfig()
configFile := getConfigFilePath()
_, err := os.Stat(configFile)
if err != nil {
return defaultConfig
}
_, err = toml.DecodeFile(configFile, &defaultConfig)
if err != nil {
Current = &defaultConfig
return defaultConfig
}
return defaultConfig
}

func Edit() int {
configFile := getConfigFilePath()
_, err := os.Stat(configFile)
if os.IsNotExist(err) {
configPath := path.Dir(configFile)
if _, err := os.Stat(configPath); os.IsNotExist(err) {
err = os.MkdirAll(configPath, 0755)
if err != nil {
log.Fatal(err)
return -1
}
}
if _, err := os.Stat(configFile); os.IsNotExist(err) {
_, err := os.Create(configFile)
if err != nil {
log.Fatal(err)
return -1
}
}
}

editor := getDefaultEditor()
if editor == "" {
log.Fatal("No editor found. Please set $EDITOR or $VISUAL")
}

cmd := exec.Command(editor, configFile)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
return cmd.ProcessState.ExitCode()
}

0 comments on commit bf1c9b1

Please sign in to comment.