Skip to content

Commit

Permalink
Show language version
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Jul 27, 2024
1 parent 11805ef commit 7942ef5
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 12 deletions.
2 changes: 1 addition & 1 deletion glot_core/src/language/ats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion glot_core/src/language/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion glot_core/src/language/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion glot_core/src/language/cobol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion glot_core/src/language/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion glot_core/src/language/crystal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion glot_core/src/language/d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion glot_core/src/language/erlang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion glot_core/src/language/guile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
}
Expand Down
64 changes: 61 additions & 3 deletions glot_core/src/page/snippet_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct Model {
pub current_route: Route,
pub current_url: Url,
pub run_result: RemoteData<FailedRunResult, RunResult>,
pub language_version_result: RemoteData<FailedRunResult, RunResult>,
pub snippet: Option<Snippet>,
}

Expand Down Expand Up @@ -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,
})
}
Expand Down Expand Up @@ -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),
})
}
Expand All @@ -275,9 +278,13 @@ impl Page<Model, Msg, AppEffect, Markup> for SnippetPage {
}

fn init(&self) -> Result<(Model, Effects<Msg, AppEffect>), 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))
}

Expand Down Expand Up @@ -543,6 +550,7 @@ impl Page<Model, Msg, AppEffect, Markup> for SnippetPage {
language: model.language.id.clone(),
files: model.files.to_vec(),
stdin: model.stdin.clone(),
command: None,
},
};

Expand Down Expand Up @@ -681,6 +689,23 @@ impl Page<Model, Msg, AppEffect, Markup> 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_));
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -935,6 +960,7 @@ pub struct RunRequestPayload {
pub language: language::Language,
pub files: Vec<File>,
pub stdin: String,
pub command: Option<String>,
}

fn view_head(model: &Model) -> maud::Markup {
Expand Down Expand Up @@ -1085,13 +1111,31 @@ fn view_content(model: &Model) -> Markup {
}
}

fn extract_language_version(model: &Model) -> Option<String> {
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 => {
Expand Down Expand Up @@ -1590,6 +1634,20 @@ fn save_settings_effect(model: &Model) -> Effect<Msg, AppEffect> {
)
}

fn get_language_version_effect(language: &language::Config) -> Effect<Msg, AppEffect> {
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<Msg, AppEffect> {
dom::dispatch_element_event(Id::Editor, "focus")
}
Expand Down
11 changes: 11 additions & 0 deletions glot_web/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down

0 comments on commit 7942ef5

Please sign in to comment.