package xrandr import ( "regexp" "git.milar.in/milarin/bufr" ) var modePattern = regexp.MustCompile(`^\s*(\d+?)x(\d+?)\s+(.*?)$`) var ratePattern = regexp.MustCompile(`(\d+\.\d+)(\*)?(\+)?\s*`) type DisplayMode struct { Resolution Resolution `json:"resolution"` Rate float64 `json:"rate"` } func parseDisplayModes(r *bufr.Reader) (active, recommended *DisplayMode, allModes []DisplayMode, err error) { allModes = make([]DisplayMode, 0) var line string for line, err = r.StringUntil(isNewLine); err == nil; line, err = r.StringUntil(isNewLine) { matches := modePattern.FindStringSubmatch(line) // line is not a DisplayMode if matches == nil { err = nil break } res := NewResolutionFromString(matches[1], matches[2]) rates := ratePattern.FindAllStringSubmatch(matches[3], -1) for _, rate := range rates { mode := DisplayMode{ Resolution: *res, Rate: atof(rate[1]), } if rate[2] == "*" { active = &mode } if rate[3] == "+" { recommended = &mode } allModes = append(allModes, mode) } } // last read line was not a DisplayMode. Unread line if err == nil { r.UnreadString(line) } return active, recommended, allModes, err }