Skip to content

Sort #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 16, 2022
Merged

Sort #22

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tmc"
version = "1.0.5"
version = "1.1.0"
authors = ["HoolaBoola <jaime.heikkiladias@helsinki.fi>",
"Robustic <juha.malinen@helsinki.fi>",
"ShootingStar91 <arttu.kangas@helsinki.fi>",
Expand Down
1 change: 0 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ pub fn build_cli() -> Command<'static> {
.required(false),
),
)
.subcommand(Command::new("update-exercises").about("Update exercises"))
.arg(
Arg::new("no-update")
.short('d')
Expand Down
68 changes: 10 additions & 58 deletions src/commands/courses.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
use super::util::Client;
use crate::io::{Io, PrintColor};
use tmc_langs::Course;

/// Lists available courses from clients organization
pub fn list_courses(io: &mut dyn Io, client: &mut dyn Client) -> anyhow::Result<()> {
let course_list = client.list_courses()?;
print_courses(io, &course_list)?;
Ok(())
}

/// Prints course names
fn print_courses(io: &mut dyn Io, course_list: &[Course]) -> anyhow::Result<()> {
let mut course_list = client.list_courses()?;
course_list.sort_unstable_by(|l, r| l.name.cmp(&r.name));
io.println("", PrintColor::Normal)?;
for course in course_list {
io.println(&course.name, PrintColor::Normal)?;
Expand All @@ -24,7 +18,7 @@ mod tests {
use reqwest::Url;
use std::{path::Path, slice::Iter};
use tmc_langs::{
ClientError, CourseDetails, CourseExercise, DownloadOrUpdateCourseExercisesResult,
ClientError, Course, CourseDetails, CourseExercise, DownloadOrUpdateCourseExercisesResult,
DownloadResult, ExercisesDetails, LangsError, Language, NewSubmission, Organization,
SubmissionFinished, SubmissionStatus,
};
Expand Down Expand Up @@ -205,52 +199,6 @@ mod tests {
mod tests {
use super::*;

#[test]
fn list_courses_test() {
let mut v: Vec<String> = Vec::new();
let input = vec![];
let mut input = input.iter();

let mut io = IoTest {
list: &mut v,
input: &mut input,
};

let courses = [
Course {
id: 0,
name: "name".to_string(),
title: "".to_string(),
description: None,
details_url: "".to_string(),
unlock_url: "".to_string(),
reviews_url: "".to_string(),
comet_url: "".to_string(),
spyware_urls: vec![],
},
Course {
id: 10,
name: "course of sorts".to_string(),
title: "".to_string(),
description: None,
details_url: "".to_string(),
unlock_url: "".to_string(),
reviews_url: "".to_string(),
comet_url: "".to_string(),
spyware_urls: vec![],
},
];
print_courses(&mut io, &courses).unwrap();

assert!(io.list[0].eq(""));
assert!(io.list[1].eq("name"), "Expected 'name', got {}", io.list[1]);
assert!(
io.list[2].eq("course of sorts"),
"Expected 'course of sorts', got {}",
io.list[2]
);
}

#[test]
fn list_courses_with_client_test() {
let mut v: Vec<String> = Vec::new();
Expand All @@ -266,10 +214,14 @@ mod tests {
list_courses(&mut io, &mut client).unwrap();

assert!(io.list[0].eq(""), "first line should be empty");
assert!(io.list[1].eq("name"), "Expected 'name', got {}", io.list[1]);
assert!(
io.list[2].eq("mooc-tutustumiskurssi"),
"Expected 'mooc-tutustumiskurssi', got '{}'",
io.list[1].eq("mooc-tutustumiskurssi"),
"Expected 'mooc-tutustumiskurssi', got {}",
io.list[1]
);
assert!(
io.list[2].eq("name"),
"Expected 'name', got '{}'",
io.list[2]
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub fn download_exercises(

if !downloaded.is_empty() {
res.push_str(&format!(
"\n\nSuccessful downloads saved to {}\\",
"\n\nSuccessful downloads saved to {}",
path.display()
));
}
Expand All @@ -205,7 +205,7 @@ pub fn download_exercises(
}

Ok(format!(
"Exercises downloaded successfully to {}\\",
"Exercises downloaded successfully to {}",
path.display()
))
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/exercises.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub fn list_exercises(
};
let course_id = course.id;

let exercises = client.get_course_exercises(course_id)?;
let mut exercises = client.get_course_exercises(course_id)?;
exercises.sort_unstable_by(|l, r| l.name.cmp(&r.name));
print_exercises(io, course_name, exercises)?;
Ok(())
}
Expand Down
5 changes: 2 additions & 3 deletions src/commands/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
pub fn set_organization_old(io: &mut dyn Io, client: &mut dyn Client) -> anyhow::Result<String> {
// List all organizations
let mut orgs = client.get_organizations()?;
orgs.sort_by(|a, b| b.pinned.cmp(&a.pinned));
orgs.sort_by(|a, b| b.pinned.cmp(&a.pinned).then(b.name.cmp(&a.name)));
let mut last_pinned = true;

io.println("Available Organizations:", PrintColor::Normal)?;
Expand Down Expand Up @@ -42,14 +42,13 @@ pub fn set_organization_old(io: &mut dyn Io, client: &mut dyn Client) -> anyhow:
pub fn set_organization(io: &mut dyn Io, client: &mut dyn Client) -> anyhow::Result<String> {
io.println("Fetching organizations...", PrintColor::Normal)?;
let mut orgs = client.get_organizations()?;
orgs.sort_by(|a, b| b.pinned.cmp(&a.pinned).then(a.name.cmp(&b.name)));
let mut pinned = orgs
.iter()
.filter(|org| org.pinned)
.map(|org| org.name.clone())
.collect::<Vec<_>>();

orgs.sort_by(|a, b| b.pinned.cmp(&a.pinned));

let others = String::from("View all organizations");
pinned.push(others.clone());

Expand Down
4 changes: 3 additions & 1 deletion src/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ pub fn update(io: &mut dyn Io, client: &mut dyn Client, currentdir: bool) -> any
}
Ok(())
}

fn call_update(path: &Path, client: &mut dyn Client) -> anyhow::Result<String> {
client.update_exercises(path)?;
Ok(format!(
"Exercises updated succesfully to {}\\",
"Exercises updated succesfully to {}",
path.to_str().context("invalid path")?
))
}
Expand All @@ -88,6 +89,7 @@ pub fn elevated_update(io: &mut dyn Io, client: &mut dyn Client) -> anyhow::Resu
pause()?;
Ok(())
}

fn pause() -> anyhow::Result<()> {
use std::{io, io::prelude::*};
let mut stdin = io::stdin();
Expand Down
2 changes: 2 additions & 0 deletions src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub fn process_update() -> anyhow::Result<()> {
println!("Update completed succesfully!");
Ok(())
}

fn elevate(command: String) -> anyhow::Result<()> {
Command::new("powershell")
.args(&[
Expand All @@ -102,6 +103,7 @@ fn elevate(command: String) -> anyhow::Result<()> {
.context("launch failure")?;
Ok(())
}

fn is_it_time_yet() -> anyhow::Result<bool> {
let config = TmcConfig::load(PLUGIN, get_path()?.as_path())?;

Expand Down