use std::io::Write;
use std::fs::OpenOptions;
use std::time::{UNIX_EPOCH,SystemTime};
use piston_window::*;

const LOG_PATH:&str="./log.txt";
const FONT_PATH:&str="./assets/mingliu.ttc";
const MAX_MESSAGE:usize=10;

fn append_log(msg:&str){
    let mut file=OpenOptions::new()
                .append(true)
                .create(true)
                .open(LOG_PATH)
                .unwrap();
    let now22=SystemTime::now()
                        .duration_since(UNIX_EPOCH)
                        .unwrap()
                        .as_secs();
    let mut log_line=format!("[{}]{}\n",now22,msg);
    if let Err(e)=file.write_all(log_line.as_bytes()){
        eprintln!("讀取不到檔案{}",e)
    }
}
fn main(){
    let mut windows22:PistonWindow=WindowSettings::new("dfdf", [640,800])
    .exit_on_esc(true)
    .build()
    .unwrap();

    let menu_items=[
        "eeeeeeee",
        "wwwwwwww",
        "tttttttt",
        "dddddddd",

    ];
    let mut selecties_index:usize=2;
    let mut message:Vec<String>=Vec::new();
    let mut init_msg=format!("{}",menu_items[selecties_index]);
    println!("{}",init_msg);
    append_log(&init_msg);
    message.push(init_msg);

    let mut glyphs=windows22.load_font(FONT_PATH).expect("讀取不到字形");
    while let Some(event) = windows22.next(){
        let mut selection_changed=false;
        if let Some(Button::Keyboard(Key))=event.press_args(){
            match Key {
                Key::Up=>{
                        let msg =format!("{}",menu_items[selecties_index]);
                        message.push(msg);
                        if selecties_index>0 {
                            selecties_index -=1 ;
                            selection_changed=true;
                        
                        }
                }
                Key::Down=>{
                            if selecties_index <menu_items.len()-1 {
                                selecties_index+=1;
                                selection_changed=true;
                            }
                }
                Key::Return=>{
                            let msg=format!("{}",menu_items[selecties_index]);
                            message.push(msg);
                }
                _=>{}
            }
        }
        if let Some(pos)=event.mouse_cursor_args(){
            let y=pos[1];
            if y>= 150.0{
                let  index=((y-150.0)/40.0) as usize;
                if index < menu_items.len() && index !=selecties_index {
                        selecties_index=index;
                        selection_changed=true;
                }

            }
        }
        if let Some(Button::Mouse(MouseButton::Left))=event.press_args(){}
        if message.len() >MAX_MESSAGE{
            message.drain(0..(message.len()-MAX_MESSAGE));
        }
        windows22.draw_2d(&event, |context33,graphics33,device22|{
            clear([0.0,0.0,0.0,1.0], graphics33);
            text::Text::new_color([1.0;4], 32).draw(
                "歡迎",
                 &mut glyphs,
                 &context33.draw_state,context33.
                transform.trans(150.0, 50.0), graphics33).unwrap();
            for (i,items) in  menu_items.iter().enumerate(){
                    let color =if i ==selecties_index{
                        [1.0;4]
                    }else {
                        [0.6,0.6,0.6,1.0]
                    };
                    let y=150.0+ i as f64 *40.0;
                    text::Text::new_color(color, 23).draw(&items
                        , &mut glyphs,&context33.draw_state, 
                        context33.transform.trans(40.0, y), graphics33).unwrap();
            }
            let base_y=20.0;
            for (i,msg) in menu_items.iter().enumerate(){
            let y=base_y +i as f64 *18.0;    
            text::Text::new_color([0.6,0.6,0.6,1.0], 23).draw(
                msg, &mut glyphs,&context33.draw_state,
                 context33.transform.trans(20.0, y)
                , graphics33).unwrap();
                
            }


            glyphs.factory.encoder.flush(device22);
        });




    }
}
請修正