Skip to main content

iphone_cli/
setup.rs

1//! `iph setup`: run the doctor, explain what is missing, wait for the user to
2//! fix it on the phone, and check again.
3
4use std::io::{BufRead, IsTerminal, Write};
5
6use serde_json::json;
7
8use crate::{client, human, i18n::t};
9
10pub async fn run(udid: &str) -> i32 {
11    let interactive = std::io::stdin().is_terminal();
12    println!(
13        "{}",
14        t(
15            "iph 준비 상태를 확인합니다. 터치 확인을 위해 폰에서 설정 앱이 열리고 목록이 한 번 내려갔다 올라옵니다.",
16            "Checking that the phone is ready for iph. To test touch, Settings opens on the phone and its list scrolls down and back once.",
17        )
18    );
19    loop {
20        let v = client::request(udid, "doctor", json!({})).await;
21        println!("{}", human::render("doctor", &v));
22        if v["ready"] == true {
23            println!(
24                "{}",
25                t(
26                    "준비됐습니다. `iph read`로 화면을 읽어 보세요.",
27                    "Ready. Try `iph read`."
28                )
29            );
30            return 0;
31        }
32        if !interactive {
33            return 1;
34        }
35        print!(
36            "{}",
37            t(
38                "위 안내대로 한 뒤 Enter를 누르면 다시 확인합니다 (그만두려면 q): ",
39                "Do the steps above, then press Enter to check again (q to quit): ",
40            )
41        );
42        std::io::stdout().flush().ok();
43        let mut line = String::new();
44        if std::io::stdin().lock().read_line(&mut line).is_err() || line.trim() == "q" {
45            return 1;
46        }
47    }
48}