1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
use std::collections::HashMap;
use neovim_lib::Value;

#[derive(Clone, PartialEq)]
pub enum NvimMode {
    Normal,
    Insert,
    Other,
}

pub struct Mode {
    mode: NvimMode,
    idx: usize,
    info: Option<Vec<ModeInfo>>,
}

impl Mode {
    pub fn new() -> Self {
        Mode {
            mode: NvimMode::Normal,
            idx: 0,
            info: None,
        }
    }

    pub fn is(&self, mode: &NvimMode) -> bool {
        self.mode == *mode
    }

    pub fn mode_info(&self) -> Option<&ModeInfo> {
        self.info.as_ref().and_then(|i| i.get(self.idx))
    }

    pub fn update(&mut self, mode: &str, idx: usize) {
        match mode {
            "normal" => self.mode = NvimMode::Normal,
            "insert" => self.mode = NvimMode::Insert,
            _ => self.mode = NvimMode::Other,
        }

        self.idx = idx;
    }

    pub fn set_info(&mut self, cursor_style_enabled: bool, info: Vec<ModeInfo>) {
        self.info = if cursor_style_enabled {
            Some(info)
        } else {
            None
        };
    }
}


#[derive(Debug, PartialEq, Clone)]
pub enum CursorShape {
    Block,
    Horizontal,
    Vertical,
    Unknown,
}

impl CursorShape {
    fn new(shape_code: &Value) -> Result<CursorShape, String> {
        let str_code = shape_code
            .as_str()
            .ok_or_else(|| "Can't convert cursor shape to string".to_owned())?;

        Ok(match str_code {
            "block" => CursorShape::Block,
            "horizontal" => CursorShape::Horizontal,
            "vertical" => CursorShape::Vertical,
            _ => {
                error!("Unknown cursor_shape {}", str_code);
                CursorShape::Unknown
            }
        })
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct ModeInfo {
    cursor_shape: Option<CursorShape>,
    cell_percentage: Option<u64>,
    pub blinkwait: Option<u32>,
}

impl ModeInfo {
    pub fn new(mode_info_map: &HashMap<String, Value>) -> Result<Self, String> {
        let cursor_shape = if let Some(shape) = mode_info_map.get("cursor_shape") {
            Some(CursorShape::new(shape)?)
        } else {
            None
        };

        Ok(ModeInfo {
            cursor_shape,
            cell_percentage: mode_info_map.get("cell_percentage").and_then(|cp| cp.as_u64()),
            blinkwait: mode_info_map.get("blinkwait").and_then(|cp| cp.as_u64()).map(|v| v as u32),
        })
    }

    pub fn cursor_shape(&self) -> Option<&CursorShape> {
        self.cursor_shape.as_ref()
    }

    pub fn cell_percentage(&self) -> u64 {
        self.cell_percentage.unwrap_or(0)
    }
}