practicode 0.1.9 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,119 @@
1
+ use super::*;
2
+
3
+ pub fn localized(map: &HashMap<String, String>, lang: &str) -> String {
4
+ let lang = normalize_ui_language(lang);
5
+ map.get(lang.as_str())
6
+ .or_else(|| map.get("en"))
7
+ .or_else(|| map.get("ko"))
8
+ .or_else(|| map.values().next())
9
+ .cloned()
10
+ .unwrap_or_default()
11
+ }
12
+ pub fn render_problem(problem: &Problem, ui_language: &str) -> String {
13
+ let lang = normalize_ui_language(ui_language);
14
+ let examples = problem
15
+ .examples
16
+ .iter()
17
+ .enumerate()
18
+ .map(|(index, case)| {
19
+ format!(
20
+ "### {} {}\n\n{}\n\n{}\n\n{}\n\n{}",
21
+ ui_text(&lang, "example"),
22
+ index + 1,
23
+ ui_text(&lang, "input"),
24
+ fenced_text(&case.input),
25
+ ui_text(&lang, "output"),
26
+ fenced_text(&case.output)
27
+ )
28
+ })
29
+ .collect::<Vec<_>>()
30
+ .join("\n\n");
31
+ let number = problem
32
+ .id
33
+ .split_once('-')
34
+ .map(|(number, _)| number)
35
+ .unwrap_or(&problem.id);
36
+ format!(
37
+ "# {number}. {}\n\n{}: {}\n{}: {}\n\n{}\n\n## {}\n\n{}\n\n## {}\n\n{}\n\n## {}\n\n{}",
38
+ localized(&problem.title, &lang),
39
+ ui_text(&lang, "difficulty"),
40
+ problem.difficulty,
41
+ ui_text(&lang, "topics"),
42
+ problem.topics.join(", "),
43
+ localized(&problem.statement, &lang),
44
+ ui_text(&lang, "input"),
45
+ localized(&problem.input, &lang),
46
+ ui_text(&lang, "output"),
47
+ localized(&problem.output, &lang),
48
+ ui_text(&lang, "examples"),
49
+ examples
50
+ )
51
+ }
52
+
53
+ pub fn render_problem_tui(problem: &Problem, ui_language: &str) -> String {
54
+ let lang = normalize_ui_language(ui_language);
55
+ let number = problem
56
+ .id
57
+ .split_once('-')
58
+ .map(|(number, _)| number)
59
+ .unwrap_or(&problem.id);
60
+ let mut lines = vec![
61
+ format!("{number}. {}", localized(&problem.title, &lang)),
62
+ format!(
63
+ "{}: {} {}: {}",
64
+ ui_text(&lang, "difficulty"),
65
+ problem.difficulty,
66
+ ui_text(&lang, "topics"),
67
+ problem.topics.join(", ")
68
+ ),
69
+ String::new(),
70
+ localized(&problem.statement, &lang),
71
+ ];
72
+ push_tui_section(
73
+ &mut lines,
74
+ ui_text(&lang, "input"),
75
+ &localized(&problem.input, &lang),
76
+ );
77
+ push_tui_section(
78
+ &mut lines,
79
+ ui_text(&lang, "output"),
80
+ &localized(&problem.output, &lang),
81
+ );
82
+ lines.push(String::new());
83
+ lines.push(ui_text(&lang, "examples").to_string());
84
+ for (index, case) in problem.examples.iter().enumerate() {
85
+ lines.push(format!(" {} {}", ui_text(&lang, "example"), index + 1));
86
+ lines.push(format!(" {}:", ui_text(&lang, "input")));
87
+ push_case_text(&mut lines, &case.input);
88
+ lines.push(format!(" {}:", ui_text(&lang, "output")));
89
+ push_case_text(&mut lines, &case.output);
90
+ }
91
+ lines.join("\n").trim_end().to_string()
92
+ }
93
+
94
+ fn push_tui_section(lines: &mut Vec<String>, title: &str, body: &str) {
95
+ lines.push(String::new());
96
+ lines.push(title.to_string());
97
+ for line in body.trim_end().lines() {
98
+ lines.push(format!(" {line}"));
99
+ }
100
+ }
101
+
102
+ fn push_case_text(lines: &mut Vec<String>, body: &str) {
103
+ let body = body.trim_end();
104
+ if body.is_empty() {
105
+ lines.push(" <empty>".to_string());
106
+ } else {
107
+ for line in body.lines() {
108
+ lines.push(format!(" {line}"));
109
+ }
110
+ }
111
+ }
112
+
113
+ pub fn fenced_text(value: &str) -> String {
114
+ let mut body = value.to_string();
115
+ if !body.ends_with('\n') {
116
+ body.push('\n');
117
+ }
118
+ format!("```text\n{body}```")
119
+ }
@@ -0,0 +1,81 @@
1
+ use super::*;
2
+
3
+ pub fn load_state(root: &Path, bank: &[Problem]) -> Result<AppState> {
4
+ let path = root.join(STATE_PATH);
5
+ if !path.exists() {
6
+ return Ok(AppState {
7
+ current_problem: bank[0].id.clone(),
8
+ settings: Settings::default(),
9
+ solved: Vec::new(),
10
+ history: vec![HistoryItem {
11
+ id: bank[0].id.clone(),
12
+ status: "assigned".to_string(),
13
+ }],
14
+ suggested_next_difficulty: "easy".to_string(),
15
+ });
16
+ }
17
+
18
+ let text = fs::read_to_string(&path).with_context(|| format!("read {}", path.display()))?;
19
+ let mut state: AppState =
20
+ serde_json::from_str(&text).with_context(|| format!("parse {}", path.display()))?;
21
+ if !bank
22
+ .iter()
23
+ .any(|problem| problem.id == state.current_problem)
24
+ {
25
+ state.current_problem = bank[0].id.clone();
26
+ }
27
+ normalize_settings(&mut state.settings);
28
+ if state.history.is_empty() {
29
+ state.history.push(HistoryItem {
30
+ id: state.current_problem.clone(),
31
+ status: "assigned".to_string(),
32
+ });
33
+ }
34
+ Ok(state)
35
+ }
36
+
37
+ pub fn save_state(root: &Path, state: &AppState) -> Result<()> {
38
+ #[derive(Serialize)]
39
+ struct StateFile<'a> {
40
+ current_problem: &'a str,
41
+ next_number: usize,
42
+ suggested_next_difficulty: &'a str,
43
+ settings: &'a Settings,
44
+ solved: &'a [String],
45
+ history: &'a [HistoryItem],
46
+ }
47
+
48
+ let path = root.join(STATE_PATH);
49
+ if let Some(parent) = path.parent() {
50
+ fs::create_dir_all(parent)?;
51
+ }
52
+ let file = StateFile {
53
+ current_problem: &state.current_problem,
54
+ next_number: state.history.len() + 1,
55
+ suggested_next_difficulty: &state.suggested_next_difficulty,
56
+ settings: &state.settings,
57
+ solved: &state.solved,
58
+ history: &state.history,
59
+ };
60
+ fs::write(path, serde_json::to_string_pretty(&file)? + "\n")?;
61
+ Ok(())
62
+ }
63
+
64
+ pub fn normalize_settings(settings: &mut Settings) {
65
+ settings.language = normalize_language(&settings.language);
66
+ settings.ui_language = normalize_ui_language(&settings.ui_language);
67
+ if !THEMES.contains(&settings.theme.as_str()) {
68
+ settings.theme = "dark".to_string();
69
+ }
70
+ settings.difficulty = normalize_difficulty(&settings.difficulty);
71
+ settings.topics = normalize_topic_list(&settings.topics);
72
+ settings.avoid_topics = normalize_topic_list(&settings.avoid_topics);
73
+ settings.generate_languages = normalize_language_list(&settings.generate_languages);
74
+ settings.generate_ui_languages = normalize_ui_language_list(&settings.generate_ui_languages);
75
+ settings.next_source = normalize_next_source(&settings.next_source);
76
+ settings.ai_provider = normalize_ai_provider(&settings.ai_provider);
77
+ if settings.ai_model.trim().is_empty() {
78
+ settings.ai_model = default_ai_model();
79
+ }
80
+ settings.ai_effort = normalize_ai_effort(&settings.ai_provider, &settings.ai_effort);
81
+ }