Skip to content

Commit 7856d4d

Browse files
committed
Selections
1 parent c6e38b2 commit 7856d4d

5 files changed

Lines changed: 101 additions & 7 deletions

File tree

pdm.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "bb7"
3-
version = "0.3.3"
3+
version = "0.4.0"
44
description = "A TDD coding bot"
55
authors = [{ name = "Drunkwcodes", email = "drunkwcodes@gmail.com" }]
66
dependencies = [
@@ -13,6 +13,7 @@ dependencies = [
1313
"prompt-toolkit>=3.0.47",
1414
"platformdirs>=4.3.1",
1515
"pytest-cov>=5.0.0",
16+
"tomlkit>=0.13.2",
1617
]
1718
requires-python = ">=3.11"
1819
readme = "README.md"

src/bb7/chat.py

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
import ollama
44
import pygame
5+
import tomlkit
56
from gtts import gTTS
67
from platformdirs import user_data_dir
78
from prompt_toolkit import PromptSession
89
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
910
from prompt_toolkit.history import FileHistory
11+
from prompt_toolkit.shortcuts import radiolist_dialog
1012
from rich.console import Console
1113
from rich.prompt import Prompt
1214
from rich.text import Text
15+
from tomlkit import document, parse
1316

1417
from .utils import random_mp3_fname
1518

@@ -39,6 +42,73 @@ def tts(text: str, lang="zh-tw", slow=False, file_name: str | None = None):
3942
os.remove(file_path)
4043

4144

45+
def set_lang(conf_file: str, lang: str):
46+
with open(conf_file, "r") as f:
47+
doc = parse(f.read())
48+
49+
doc["voice_language"] = lang
50+
51+
with open(conf_file, "w") as f:
52+
f.write(tomlkit.dumps(doc))
53+
54+
55+
def get_lang(conf_file: str):
56+
with open(conf_file, "r") as f:
57+
doc = parse(f.read())
58+
59+
return doc["voice_language"]
60+
61+
62+
def select_language(conf_file: str):
63+
# 定義語言選項
64+
languages = [
65+
("中文 (Chinese)", "Chinese"),
66+
("英文 (English)", "English"),
67+
("日文 (Japanese)", "Japanese"),
68+
("韓文 (Korean)", "Korean"),
69+
("法文 (French)", "French"),
70+
("西班牙文 (Spanish)", "Spanish"),
71+
("其他 (Other)", "Other"),
72+
]
73+
74+
# 使用 radiolist_dialog 顯示選單
75+
result = radiolist_dialog(
76+
title="語言選單",
77+
text="Please select a language for /voice command:",
78+
values=languages,
79+
).run()
80+
81+
# 如果選擇「其他」,讓使用者輸入自訂語言名稱
82+
if result == "其他 (Other)":
83+
session = PromptSession()
84+
custom_language = session.prompt("請輸入自訂語言名稱:")
85+
print(f"You selected: {custom_language}")
86+
set_lang(conf_file=conf_file, lang=custom_language)
87+
88+
else:
89+
print(f"You selected: {result}")
90+
if result == "中文 (Chinese)":
91+
set_lang(conf_file=conf_file, lang="zh-tw")
92+
93+
elif result == "英文 (English)":
94+
set_lang(conf_file=conf_file, lang="en")
95+
96+
elif result == "日文 (Japanese)":
97+
set_lang(conf_file=conf_file, lang="ja")
98+
99+
elif result == "韓文 (Korean)":
100+
set_lang(conf_file=conf_file, lang="ko")
101+
102+
elif result == "法文 (French)":
103+
set_lang(conf_file=conf_file, lang="fr")
104+
105+
elif result == "西班牙文 (Spanish)":
106+
set_lang(conf_file=conf_file, lang="es")
107+
108+
else:
109+
raise ValueError(f"Invalid language selection: {result}")
110+
111+
42112
def chat_terminal():
43113
"""
44114
Runs a chat terminal where the user can interact with a simulated chatbot.
@@ -60,13 +130,23 @@ def chat_terminal():
60130
history = []
61131
bb7_dir = user_data_dir(appname="bb7", appauthor="drunkwcodes")
62132
history_file = bb7_dir + "/chat_history.txt"
133+
134+
conf_file = bb7_dir + "/bb7_config.toml"
135+
136+
if os.path.exists(conf_file) is False:
137+
doc = document()
138+
doc.add("voice_language", "ja")
139+
with open(conf_file, "w") as f:
140+
tomlkit.dump(doc, f)
141+
63142
os.makedirs(bb7_dir, exist_ok=True)
64143
session = PromptSession(history=FileHistory(history_file))
65144

66145
while True:
67146
try:
68147
# 使用 Prompt 讓用戶輸入訊息
69148
# user_input = Prompt.ask("[bold blue]>>[/bold blue]")
149+
lang = get_lang(conf_file)
70150
user_input = session.prompt(">> ", auto_suggest=AutoSuggestFromHistory())
71151

72152
# 檢查是否是退出命令
@@ -84,10 +164,12 @@ def chat_terminal():
84164
inputs = user_input.split(" ")
85165
if len(inputs) > 1:
86166
lang = inputs[1]
87-
else:
88-
lang = "ja"
89167
tts(bot_reply, lang=lang)
90168
continue
169+
170+
elif "/select" in user_input.lower() or "/s" in user_input.lower():
171+
select_language(conf_file=conf_file)
172+
continue
91173
else:
92174
console.print("[bold red]Invalid command[/bold red]")
93175
continue

src/bb7/cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def main(test):
1414
print("bb7 is a TDD coding bot. It can recognize the Python project structure,")
1515
print("find the tests folder, and run tests. It can also chat with a chatbot.")
1616

17-
1817
if test:
1918
proot = None
2019
if examine_folders() is False:
@@ -41,9 +40,9 @@ def main(test):
4140
# loop 2.
4241
result = run_tests()
4342

44-
4543
else:
4644
chat_terminal()
4745

46+
4847
if __name__ == "__main__":
4948
main()

src/bb7/examine.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import os
66
from pathlib import Path
7+
78
import pytest
89

9-
from .utils import find_project_root, cd
10+
from .utils import cd, find_project_root
1011

1112

1213
def examine_folders():

0 commit comments

Comments
 (0)