From 7942ef51fecfd9fabe12151bffd5675bec196add Mon Sep 17 00:00:00 2001 From: Petter Rasmussen Date: Sat, 27 Jul 2024 13:09:48 +0200 Subject: [PATCH] Show language version --- glot_core/src/language/ats.rs | 2 +- glot_core/src/language/bash.rs | 2 +- glot_core/src/language/c.rs | 2 +- glot_core/src/language/cobol.rs | 2 +- glot_core/src/language/cpp.rs | 2 +- glot_core/src/language/crystal.rs | 2 +- glot_core/src/language/d.rs | 2 +- glot_core/src/language/erlang.rs | 2 +- glot_core/src/language/guile.rs | 2 +- glot_core/src/page/snippet_page.rs | 64 ++++++++++++++++++++++++++++-- glot_web/src/app.ts | 11 +++++ 11 files changed, 81 insertions(+), 12 deletions(-) diff --git a/glot_core/src/language/ats.rs b/glot_core/src/language/ats.rs index 34bee8a..e7b9965 100644 --- a/glot_core/src/language/ats.rs +++ b/glot_core/src/language/ats.rs @@ -25,7 +25,7 @@ pub fn config() -> Config { }, run_config: RunConfig { container_image: "glot/ats:latest".to_string(), - version_command: "patscc --version".to_string(), + version_command: "patscc -vats".to_string(), }, } } diff --git a/glot_core/src/language/bash.rs b/glot_core/src/language/bash.rs index 57a3957..6c3b86e 100644 --- a/glot_core/src/language/bash.rs +++ b/glot_core/src/language/bash.rs @@ -24,7 +24,7 @@ pub fn config() -> Config { }, run_config: RunConfig { container_image: "glot/bash:latest".to_string(), - version_command: "bash --version".to_string(), + version_command: "bash --version | head -n 1".to_string(), }, } } diff --git a/glot_core/src/language/c.rs b/glot_core/src/language/c.rs index 2e284cb..5e23e50 100644 --- a/glot_core/src/language/c.rs +++ b/glot_core/src/language/c.rs @@ -30,7 +30,7 @@ pub fn config() -> Config { }, run_config: RunConfig { container_image: "glot/clang:latest".to_string(), - version_command: "clang --version".to_string(), + version_command: "clang --version | head -n 1".to_string(), }, } } diff --git a/glot_core/src/language/cobol.rs b/glot_core/src/language/cobol.rs index e81f3a2..59cb19a 100644 --- a/glot_core/src/language/cobol.rs +++ b/glot_core/src/language/cobol.rs @@ -31,7 +31,7 @@ pub fn config() -> Config { }, run_config: RunConfig { container_image: "glot/cobol:latest".to_string(), - version_command: "cobc --version".to_string(), + version_command: "cobc --version | head -n 1".to_string(), }, } } diff --git a/glot_core/src/language/cpp.rs b/glot_core/src/language/cpp.rs index 9d64a82..82e2aed 100644 --- a/glot_core/src/language/cpp.rs +++ b/glot_core/src/language/cpp.rs @@ -31,7 +31,7 @@ pub fn config() -> Config { }, run_config: RunConfig { container_image: "glot/clang:latest".to_string(), - version_command: "clang --version".to_string(), + version_command: "clang --version | head -n 1".to_string(), }, } } diff --git a/glot_core/src/language/crystal.rs b/glot_core/src/language/crystal.rs index c54c15c..ef8940b 100644 --- a/glot_core/src/language/crystal.rs +++ b/glot_core/src/language/crystal.rs @@ -24,7 +24,7 @@ pub fn config() -> Config { }, run_config: RunConfig { container_image: "glot/crystal:latest".to_string(), - version_command: "crystal --version".to_string(), + version_command: "crystal --version | head -n 1".to_string(), }, } } diff --git a/glot_core/src/language/d.rs b/glot_core/src/language/d.rs index 523c749..9e92852 100644 --- a/glot_core/src/language/d.rs +++ b/glot_core/src/language/d.rs @@ -30,7 +30,7 @@ pub fn config() -> Config { }, run_config: RunConfig { container_image: "glot/dlang:latest".to_string(), - version_command: "dmd --version".to_string(), + version_command: "dmd --version | head -n 1".to_string(), }, } } diff --git a/glot_core/src/language/erlang.rs b/glot_core/src/language/erlang.rs index ddb07ee..1a56cd2 100644 --- a/glot_core/src/language/erlang.rs +++ b/glot_core/src/language/erlang.rs @@ -28,7 +28,7 @@ pub fn config() -> Config { }, run_config: RunConfig { container_image: "glot/erlang:latest".to_string(), - version_command: "erl -version".to_string(), + version_command: "erl -version 2>&1".to_string(), }, } } diff --git a/glot_core/src/language/guile.rs b/glot_core/src/language/guile.rs index 4fe45b4..15e4b9f 100644 --- a/glot_core/src/language/guile.rs +++ b/glot_core/src/language/guile.rs @@ -24,7 +24,7 @@ pub fn config() -> Config { }, run_config: RunConfig { container_image: "glot/guile:latest".to_string(), - version_command: "guile --version".to_string(), + version_command: "guile --version | head -n 1".to_string(), }, } } diff --git a/glot_core/src/page/snippet_page.rs b/glot_core/src/page/snippet_page.rs index 189abd4..741b446 100644 --- a/glot_core/src/page/snippet_page.rs +++ b/glot_core/src/page/snippet_page.rs @@ -57,6 +57,7 @@ pub struct Model { pub current_route: Route, pub current_url: Url, pub run_result: RemoteData, + pub language_version_result: RemoteData, pub snippet: Option, } @@ -216,6 +217,7 @@ impl SnippetPage { current_url: self.current_url.clone(), current_route: route.clone(), run_result: RemoteData::NotAsked, + language_version_result: RemoteData::Loading, snippet: None, }) } @@ -264,6 +266,7 @@ impl SnippetPage { current_url: self.current_url.clone(), current_route: route.clone(), run_result: RemoteData::NotAsked, + language_version_result: RemoteData::Loading, snippet: Some(snippet_clone), }) } @@ -275,9 +278,13 @@ impl Page for SnippetPage { } fn init(&self) -> Result<(Model, Effects), String> { - let effects = vec![load_settings_effect()]; let model = self.get_model()?; + let effects = vec![ + load_settings_effect(), + get_language_version_effect(&model.language), + ]; + Ok((model, effects)) } @@ -543,6 +550,7 @@ impl Page for SnippetPage { language: model.language.id.clone(), files: model.files.to_vec(), stdin: model.stdin.clone(), + command: None, }, }; @@ -681,6 +689,23 @@ impl Page for SnippetPage { Ok(vec![]) } + "GotLanguageVersionResponse" => { + let outcome = RunOutcome::from_value(msg.data) + .map_err(|err| format!("Failed to decode run response from js: {}", err))?; + + match outcome { + RunOutcome::Success(run_result) => { + model.language_version_result = RemoteData::Success(run_result); + } + + RunOutcome::Failure(err) => { + model.language_version_result = RemoteData::Failure(err); + } + } + + Ok(vec![]) + } + _ => { let log_effect = browser::console::log(&format!("Got unknown message from JS: {}", msg.type_)); @@ -886,7 +911,7 @@ impl EditorTheme { #[serde(rename_all = "camelCase")] pub enum AppEffect { Run(RunRequest), - CreateSnippet(Snippet), + GetLanguageVersion(RunRequest), } #[derive(Clone, serde::Serialize, serde::Deserialize)] @@ -935,6 +960,7 @@ pub struct RunRequestPayload { pub language: language::Language, pub files: Vec, pub stdin: String, + pub command: Option, } fn view_head(model: &Model) -> maud::Markup { @@ -1085,13 +1111,31 @@ fn view_content(model: &Model) -> Markup { } } +fn extract_language_version(model: &Model) -> Option { + if let RemoteData::Success(run_result) = &model.language_version_result { + if run_result.stdout.is_empty() { + None + } else { + Some(run_result.stdout.clone()) + } + } else { + None + } +} + fn view_output_panel(model: &Model) -> Markup { + let ready_info = if let Some(version) = extract_language_version(model) { + format!("READY.\n\n{}", version) + } else { + "READY.".to_string() + }; + html! { div class="overflow-auto h-full border-b border-x border-gray-400 shadow-lg" { dl { @match &model.run_result { RemoteData::NotAsked => { - (view_info("READY.")) + (view_info(&ready_info)) } RemoteData::Loading => { @@ -1590,6 +1634,20 @@ fn save_settings_effect(model: &Model) -> Effect { ) } +fn get_language_version_effect(language: &language::Config) -> Effect { + let config = RunRequest { + image: language.run_config.container_image.clone(), + payload: RunRequestPayload { + language: language.id.clone(), + files: vec![], + stdin: "".to_string(), + command: Some(language.run_config.version_command.clone()), + }, + }; + + effect::app_effect(AppEffect::GetLanguageVersion(config)) +} + fn focus_editor_effect() -> Effect { dom::dispatch_element_event(Id::Editor, "focus") } diff --git a/glot_web/src/app.ts b/glot_web/src/app.ts index dff37bf..fed522d 100644 --- a/glot_web/src/app.ts +++ b/glot_web/src/app.ts @@ -31,6 +31,17 @@ AceEditorElement.register(); } break; + case "getLanguageVersion": + try { + const runResponse = await run(msg.config); + poly.sendMessage("GotLanguageVersionResponse", runResponse); + } catch (err: any) { + poly.sendMessage("LanguageVersionResponse", { + message: err.message, + }); + } + break; + default: console.warn(`Unhandled app effect: ${msg.type}`); }