|
| 1 | +// Copyright 2025 Naren Yellavula |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + |
| 22 | + "gopkg.in/yaml.v3" |
| 23 | +) |
| 24 | + |
| 25 | +type HistoryConfig struct { |
| 26 | + EnableFuzzing bool `yaml:"enable_fuzzing"` |
| 27 | +} |
| 28 | + |
| 29 | +type Config struct { |
| 30 | + History HistoryConfig `yaml:"history"` |
| 31 | +} |
| 32 | + |
| 33 | +var defaultConfig = Config{ |
| 34 | + History: HistoryConfig{ |
| 35 | + EnableFuzzing: true, |
| 36 | + }, |
| 37 | +} |
| 38 | + |
| 39 | +func LoadConfig() (*Config, error) { |
| 40 | + homeDir, err := os.UserHomeDir() |
| 41 | + if err != nil { |
| 42 | + return &defaultConfig, nil |
| 43 | + } |
| 44 | + |
| 45 | + configPath := filepath.Join(homeDir, ".recaller.yaml") |
| 46 | + |
| 47 | + if _, err := os.Stat(configPath); os.IsNotExist(err) { |
| 48 | + return &defaultConfig, nil |
| 49 | + } |
| 50 | + |
| 51 | + data, err := os.ReadFile(configPath) |
| 52 | + if err != nil { |
| 53 | + return &defaultConfig, nil |
| 54 | + } |
| 55 | + |
| 56 | + var config Config |
| 57 | + err = yaml.Unmarshal(data, &config) |
| 58 | + if err != nil { |
| 59 | + return &defaultConfig, nil |
| 60 | + } |
| 61 | + |
| 62 | + return &config, nil |
| 63 | +} |
| 64 | + |
| 65 | +func getConfigPath() (string, error) { |
| 66 | + homeDir, err := os.UserHomeDir() |
| 67 | + if err != nil { |
| 68 | + return "", err |
| 69 | + } |
| 70 | + return filepath.Join(homeDir, ".recaller.yaml"), nil |
| 71 | +} |
| 72 | + |
| 73 | +func createDefaultConfigFile() error { |
| 74 | + configPath, err := getConfigPath() |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("failed to get config path: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + data, err := yaml.Marshal(&defaultConfig) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("failed to marshal default config: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + err = os.WriteFile(configPath, data, 0644) |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("failed to write config file: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func displaySettings() { |
| 93 | + configPath, err := getConfigPath() |
| 94 | + if err != nil { |
| 95 | + fmt.Printf("❌ Failed to get config path: %v\n", err) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + config, err := LoadConfig() |
| 100 | + if err != nil { |
| 101 | + fmt.Printf("❌ Failed to load configuration: %v\n", err) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + configExists := true |
| 106 | + if _, err := os.Stat(configPath); os.IsNotExist(err) { |
| 107 | + configExists = false |
| 108 | + fmt.Printf("📝 Configuration file not found. Creating default configuration...\n\n") |
| 109 | + |
| 110 | + if err := createDefaultConfigFile(); err != nil { |
| 111 | + fmt.Printf("❌ Failed to create default config file: %v\n", err) |
| 112 | + return |
| 113 | + } |
| 114 | + fmt.Printf("✅ Created default configuration at: %s\n\n", configPath) |
| 115 | + } |
| 116 | + |
| 117 | + fmt.Printf("🔧 Recaller Configuration Settings\n") |
| 118 | + fmt.Printf("═══════════════════════════════════\n\n") |
| 119 | + |
| 120 | + if configExists { |
| 121 | + fmt.Printf("📍 Config file: %s\n", configPath) |
| 122 | + } else { |
| 123 | + fmt.Printf("📍 Config file: %s (newly created)\n", configPath) |
| 124 | + } |
| 125 | + |
| 126 | + fmt.Printf("📊 Current settings:\n\n") |
| 127 | + |
| 128 | + fmt.Printf("🔍 %sHistory Search:%s\n", Green, Reset) |
| 129 | + |
| 130 | + fuzzyValue := "true" |
| 131 | + fuzzyDesc := "Fuzzy search (substring matching anywhere)" |
| 132 | + if !config.History.EnableFuzzing { |
| 133 | + fuzzyValue = "false" |
| 134 | + fuzzyDesc = "Prefix-based search (commands starting with query)" |
| 135 | + } |
| 136 | + |
| 137 | + fmt.Printf(" • %senable_fuzzing%s: %s\n", Green, Reset, fuzzyValue) |
| 138 | + fmt.Printf(" %s\n\n", fuzzyDesc) |
| 139 | + |
| 140 | + if !config.History.EnableFuzzing { |
| 141 | + fmt.Printf("💡 Fuzzy search is disabled. To enable it, edit %s:\n", configPath) |
| 142 | + fmt.Printf(" history:\n enable_fuzzing: true\n\n") |
| 143 | + } else { |
| 144 | + fmt.Printf("💡 To use prefix-only search, edit %s:\n", configPath) |
| 145 | + fmt.Printf(" history:\n enable_fuzzing: false\n\n") |
| 146 | + } |
| 147 | + |
| 148 | + fmt.Printf("📚 For more information, see: https://github.com/cybrota/recaller#search-modes\n") |
| 149 | +} |
0 commit comments