func cmdList(opts map[string]interface{}, conf config.Config) {

	// load the cheatsheets
	cheatsheets, err := sheets.Load(conf.Cheatpaths)
	if err != nil {
		fmt.Fprintln(os.Stderr, fmt.Sprintf("failed to list cheatsheets: %v", err))
		os.Exit(1)
	}

	// filter cheatsheets by tag if --tag was provided
	if opts["--tag"] != nil {
		cheatsheets = sheets.Filter(
			cheatsheets,
			strings.Split(opts["--tag"].(string), ","),
		)
	}

	// instead of "consolidating" all of the cheatsheets (ie, overwriting global
	// sheets with local sheets), here we simply want to create a slice
	// containing all sheets.
	flattened := []sheet.Sheet{}
	for _, pathsheets := range cheatsheets {
		for _, s := range pathsheets {
			flattened = append(flattened, s)
		}
	}

	// sort the "flattened" sheets alphabetically
	sort.Slice(flattened, func(i, j int) bool {
		return flattened[i].Title < flattened[j].Title
	})

	// filter if <cheatsheet> was specified
	// NB: our docopt specification is misleading here. When used in conjunction
	// with `-l`, `<cheatsheet>` is really a pattern against which to filter
	// sheet titles.
	if opts["<cheatsheet>"] != nil {

		// initialize a slice of filtered sheets
		filtered := []sheet.Sheet{}

		// initialize our filter pattern
		pattern := "(?i)" + opts["<cheatsheet>"].(string)

		// compile the regex
		reg, err := regexp.Compile(pattern)
		if err != nil {
			fmt.Fprintln(
				os.Stderr,
				fmt.Sprintf("failed to compile regexp: %s, %v", pattern, err),
			)
			os.Exit(1)
		}

		// iterate over each cheatsheet, and pass-through those which match the
		// filter pattern
		for _, s := range flattened {
			if reg.MatchString(s.Title) {
				filtered = append(filtered, s)
			}
		}

		flattened = filtered
	}

	// return exit code 2 if no cheatsheets are available
	if len(flattened) == 0 {
		os.Exit(2)
	}

	// initialize a tabwriter to produce cleanly columnized output
	var out bytes.Buffer
	w := tabwriter.NewWriter(&out, 0, 0, 1, ' ', 0)

	// write a header row
	fmt.Fprintln(w, "title:\tfile:\ttags:")

	// generate sorted, columnized output
	for _, sheet := range flattened {
		fmt.Fprintln(w, fmt.Sprintf(
			"%s\t%s\t%s",
			sheet.Title,
			sheet.Path,
			strings.Join(sheet.Tags, ","),
		))
	}

	// write columnized output to stdout
	w.Flush()
	display.Write(out.String(), conf)
}
// cmdList lists all available cheatsheets.
cmdList lists all available cheatsheets.
// cmdList lists all available cheatsheets .
func cmdList(opts map[string]interface{}, conf config.Config) {
	cheatsheets, err := sheets.Load(conf.Cheatpaths)
	if err != nil {
		fmt.Fprintln(os.Stderr, fmt.Sprintf("failed to list cheatsheets: %v", err))
		os.Exit(1)
	}
	if opts["--tag"] != nil {
		cheatsheets = sheets.Filter(
			cheatsheets,
			strings.Split(opts["--tag"].(string), ","),
		)
	}
	flattened := []sheet.Sheet{}
	for _, pathsheets := range cheatsheets {
		for _, s := range pathsheets {
			flattened = append(flattened, s)
		}
	}
	sort.Slice(flattened, func(i, j int) bool {
		return flattened[i].Title < flattened[j].Title
	})
	if opts["<cheatsheet>"] != nil {
		filtered := []sheet.Sheet{}
		pattern := "(?i)" + opts["<cheatsheet>"].(string)
		reg, err := regexp.Compile(pattern)
		if err != nil {
			fmt.Fprintln(
				os.Stderr,
				fmt.Sprintf("failed to compile regexp: %s, %v", pattern, err),
			)
			os.Exit(1)
		}
		for _, s := range flattened {
			if reg.MatchString(s.Title) {
				filtered = append(filtered, s)
			}
		}
		flattened = filtered
	}
	if len(flattened) == 0 {
		os.Exit(2)
	}
	var out bytes.Buffer
	w := tabwriter.NewWriter(&out, 0, 0, 1, ' ', 0)
	fmt.Fprintln(w, "title:\tfile:\ttags:")
	for _, sheet := range flattened {
		fmt.Fprintln(w, fmt.Sprintf(
			"%s\t%s\t%s",
			sheet.Title,
			sheet.Path,
			strings.Join(sheet.Tags, ","),
		))
	}
	w.Flush()
	display.Write(out.String(), conf)
}
func cmdList ( opts map [ string ] interface { } , conf config . Config ) { cheatsheets , err := sheets . Load ( conf . Cheatpaths ) if err != nil { fmt . Fprintln ( os . Stderr , fmt . Sprintf ( " " , err ) ) os . Exit ( 1 ) } if opts [ " " ] != nil { cheatsheets = sheets . Filter ( cheatsheets , strings . Split ( opts [ " " ] . ( string ) , " " ) , ) } flattened := [ ] sheet . Sheet { } for _ , pathsheets := range cheatsheets { for _ , s := range pathsheets { flattened = append ( flattened , s ) } } sort . Slice ( flattened , func ( i , j int ) bool { return flattened [ i ] . Title < flattened [ j ] . Title } ) if opts [ " " ] != nil { filtered := [ ] sheet . Sheet { } pattern := " " + opts [ " " ] . ( string ) reg , err := regexp . Compile ( pattern ) if err != nil { fmt . Fprintln ( os . Stderr , fmt . Sprintf ( " " , pattern , err ) , ) os . Exit ( 1 ) } for _ , s := range flattened { if reg . MatchString ( s . Title ) { filtered = append ( filtered , s ) } } flattened = filtered } if len ( flattened ) == 0 { os . Exit ( 2 ) } var out bytes . Buffer w := tabwriter . NewWriter ( & out , 0 , 0 , 1 , ' ' , 0 ) fmt . Fprintln ( w , " \t \t " ) for _ , sheet := range flattened { fmt . Fprintln ( w , fmt . Sprintf ( " \t \t " , sheet . Title , sheet . Path , strings . Join ( sheet . Tags , " " ) , ) ) } w . Flush ( ) display . Write ( out . String ( ) , conf ) }
cmdList lists all available cheatsheets.
cmdList lists all available cheatsheets .
// load the cheatsheets // filter cheatsheets by tag if --tag was provided // instead of "consolidating" all of the cheatsheets (ie, overwriting global // sheets with local sheets), here we simply want to create a slice // containing all sheets. // sort the "flattened" sheets alphabetically // filter if <cheatsheet> was specified // NB: our docopt specification is misleading here. When used in conjunction // with `-l`, `<cheatsheet>` is really a pattern against which to filter // sheet titles. // initialize a slice of filtered sheets // initialize our filter pattern // compile the regex // iterate over each cheatsheet, and pass-through those which match the // filter pattern // return exit code 2 if no cheatsheets are available // initialize a tabwriter to produce cleanly columnized output // write a header row // generate sorted, columnized output // write columnized output to stdout