|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/charmbracelet/bubbles/textinput" |
| 8 | + tea "github.com/charmbracelet/bubbletea" |
| 9 | + "github.com/cocoide/commitify/util" |
| 10 | + "github.com/fatih/color" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + configKey = [...]string{"api-key", "language", "format"} |
| 16 | + configOption = [][]string{ |
| 17 | + {}, |
| 18 | + {"Japanese", "English"}, |
| 19 | + {"Format 1", "Format 2"}, |
| 20 | + } |
| 21 | +) |
| 22 | + |
| 23 | +type configModel struct { |
| 24 | + configKeyIndex int |
| 25 | + configOptionIndex int |
| 26 | + configKeySelected bool |
| 27 | + err error |
| 28 | + textInput textinput.Model |
| 29 | +} |
| 30 | + |
| 31 | +func initConfigModel() configModel { |
| 32 | + ti := textinput.New() |
| 33 | + ti.Focus() |
| 34 | + |
| 35 | + return configModel{ |
| 36 | + textInput: ti, |
| 37 | + err: nil, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (cm configModel) Init() tea.Cmd { |
| 42 | + return textinput.Blink |
| 43 | +} |
| 44 | + |
| 45 | +func (cm configModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 46 | + switch cm.configKeySelected { |
| 47 | + // 設定項目を選択する |
| 48 | + case false: |
| 49 | + switch msg := msg.(type) { |
| 50 | + case tea.KeyMsg: |
| 51 | + switch msg.Type { |
| 52 | + case tea.KeyUp: |
| 53 | + if cm.configKeyIndex > 0 { |
| 54 | + cm.configKeyIndex-- |
| 55 | + } |
| 56 | + case tea.KeyDown: |
| 57 | + if cm.configKeyIndex < len(configKey)-1 { |
| 58 | + cm.configKeyIndex++ |
| 59 | + } |
| 60 | + case tea.KeyEnter: |
| 61 | + cm.configKeySelected = true |
| 62 | + return cm, nil |
| 63 | + case tea.KeyCtrlC, tea.KeyEsc: |
| 64 | + return cm, tea.Quit |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // 設定項目に値をセットする |
| 69 | + case true: |
| 70 | + switch len(configOption[cm.configKeyIndex]) { |
| 71 | + // 選択肢のない項目は入力を受け付ける |
| 72 | + case 0: |
| 73 | + var cmd tea.Cmd |
| 74 | + switch msg := msg.(type) { |
| 75 | + case tea.KeyMsg: |
| 76 | + switch msg.Type { |
| 77 | + case tea.KeyEnter: |
| 78 | + saveConfig(cm) |
| 79 | + return cm, tea.Quit |
| 80 | + case tea.KeyCtrlC, tea.KeyEsc: |
| 81 | + return cm, tea.Quit |
| 82 | + } |
| 83 | + case error: |
| 84 | + cm.err = msg |
| 85 | + return cm, nil |
| 86 | + } |
| 87 | + |
| 88 | + cm.textInput, cmd = cm.textInput.Update(msg) |
| 89 | + return cm, cmd |
| 90 | + |
| 91 | + // 選択肢がある場合はセレクターで表示する |
| 92 | + default: |
| 93 | + switch msg := msg.(type) { |
| 94 | + case tea.KeyMsg: |
| 95 | + switch msg.Type { |
| 96 | + case tea.KeyUp: |
| 97 | + if cm.configOptionIndex > 0 { |
| 98 | + cm.configOptionIndex-- |
| 99 | + } |
| 100 | + case tea.KeyDown: |
| 101 | + if cm.configOptionIndex < len(configOption[cm.configKeyIndex])-1 { |
| 102 | + cm.configOptionIndex++ |
| 103 | + } |
| 104 | + case tea.KeyEnter: |
| 105 | + saveConfig(cm) |
| 106 | + return cm, tea.Quit |
| 107 | + case tea.KeyCtrlC, tea.KeyEsc: |
| 108 | + return cm, tea.Quit |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return cm, nil |
| 115 | +} |
| 116 | + |
| 117 | +func (cm configModel) View() string { |
| 118 | + var b strings.Builder |
| 119 | + |
| 120 | + switch cm.configKeySelected { |
| 121 | + // 設定項目を選んでいない時 |
| 122 | + case false: |
| 123 | + white := color.New(color.FgWhite).SprintFunc() |
| 124 | + b.WriteString(white("設定項目を選んでください:\n")) |
| 125 | + b.WriteString(white(" ↑↓の矢印キーで項目を移動、Enterで選択\n")) |
| 126 | + |
| 127 | + for i, choice := range configKey { |
| 128 | + cyan := color.New(color.FgCyan).SprintFunc() |
| 129 | + hiCyan := color.New(color.FgHiCyan).SprintFunc() |
| 130 | + if i == cm.configKeyIndex { |
| 131 | + b.WriteString(fmt.Sprintf(hiCyan("➡️ %s\n"), choice)) |
| 132 | + } else { |
| 133 | + b.WriteString(fmt.Sprintf(cyan(" %s\n"), choice)) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + // 設定項目に値をセットする |
| 138 | + case true: |
| 139 | + // 選択肢のない項目はテキストエリアを表示 |
| 140 | + switch len(configOption[cm.configKeyIndex]) { |
| 141 | + case 0: |
| 142 | + white := color.New(color.FgWhite).SprintFunc() |
| 143 | + b.WriteString(white(fmt.Sprintf( |
| 144 | + "ここに%sを入力: %s\n", |
| 145 | + configKey[cm.configKeyIndex], |
| 146 | + cm.textInput.View(), |
| 147 | + ))) |
| 148 | + b.WriteString(white(" Enterキーで確定")) |
| 149 | + |
| 150 | + default: |
| 151 | + white := color.New(color.FgWhite).SprintFunc() |
| 152 | + b.WriteString(white("設定内容を選んでください:\n")) |
| 153 | + b.WriteString(white(" ↑↓の矢印キーで項目を移動、Enterで選択\n")) |
| 154 | + |
| 155 | + for i, option := range configOption[cm.configKeyIndex] { |
| 156 | + cyan := color.New(color.FgCyan).SprintFunc() |
| 157 | + hiCyan := color.New(color.FgHiCyan).SprintFunc() |
| 158 | + if i == cm.configOptionIndex { |
| 159 | + b.WriteString(fmt.Sprintf(hiCyan("➡️ %s\n"), option)) |
| 160 | + } else { |
| 161 | + b.WriteString(fmt.Sprintf(cyan(" %s\n"), option)) |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return b.String() |
| 168 | +} |
| 169 | + |
| 170 | +var configCmd = &cobra.Command{ |
| 171 | + Use: "config", |
| 172 | + Short: "設定を変更します", |
| 173 | + Long: `設定を変更します。設定項目はコマンドを実行すると表示されます。`, |
| 174 | + Run: func(cmd *cobra.Command, args []string) { |
| 175 | + p := tea.NewProgram(initConfigModel()) |
| 176 | + p.Run() |
| 177 | + }, |
| 178 | +} |
| 179 | + |
| 180 | +func init() { |
| 181 | + rootCmd.AddCommand(configCmd) |
| 182 | +} |
| 183 | + |
| 184 | +func saveConfig(cm configModel) { |
| 185 | + currentConfig, err := util.ReadConfig() |
| 186 | + if err != nil { |
| 187 | + fmt.Println(err) |
| 188 | + } |
| 189 | + |
| 190 | + switch cm.configKeyIndex { |
| 191 | + case 0: |
| 192 | + currentConfig.ChatGptApiKey = cm.textInput.Value() |
| 193 | + case 1: |
| 194 | + currentConfig.UseLanguage = configOption[cm.configKeyIndex][cm.configOptionIndex] |
| 195 | + case 2: |
| 196 | + currentConfig.CommitFormat = configOption[cm.configKeyIndex][cm.configOptionIndex] |
| 197 | + } |
| 198 | + |
| 199 | + err = util.WriteConfig(currentConfig) |
| 200 | + if err != nil { |
| 201 | + fmt.Println(err) |
| 202 | + } |
| 203 | +} |
0 commit comments