all repos

viye @ f73a949

my shot at reimplementing xiki
7 files changed, 34 insertions(+), 30 deletions(-)
make FormatOutput and FormatBulletList generic functions
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-08-28 19:02:34 +0300
Authored at: 2026-08-28 18:54:24 +0300
Change ID: rurrknxstonslnwwuqttvtzuotkxsztr
Parent: fbda676
M internal/plumbing/gobin/gobin.go
···
        46
        46
         	cmd := exec.Command("go", append([]string{subcmd}, args...)...)

      
        47
        47
         	cmd.Dir = dir

      
        48
        48
         	out, _ := cmd.CombinedOutput()

      
        49
        
        -	return viye.FormatOutput(string(out)), nil

      
        
        49
        +	return viye.FormatOutput(out), nil

      
        50
        50
         }

      
M internal/plumbing/http/http.go
···
        58
        58
         	if len(inp) == 0 {

      
        59
        59
         		return nil, "", nil

      
        60
        60
         	}

      
        61
        
        -	headers = make(map[string]string)

      
        62
        61
         

      
        
        62
        +	headers = make(map[string]string)

      
        63
        63
         	for _, line := range inp {

      
        64
        64
         		key, val, ok := strings.Cut(line, ": ")

      
        65
        65
         		if ok {

      
M internal/plumbing/json/json.go
···
        32
        32
         		}

      
        33
        33
         	}

      
        34
        34
         

      
        35
        
        -	mashaled, err := json.Marshal(m)

      
        
        35
        +	marshaled, err := json.Marshal(m)

      
        36
        36
         	if err != nil {

      
        37
        37
         		return "", fmt.Errorf("demo: %w", err)

      
        38
        38
         	}

      
        39
        
        -	return fmt.Sprintf("| %s\n", string(mashaled)), nil

      
        
        39
        +	return viye.FormatOutput(marshaled), nil

      
        40
        40
         }

      
M internal/plumbing/makefile/makefile.go
···
        49
        49
         		if err != nil {

      
        50
        50
         			return "", err

      
        51
        51
         		}

      
        52
        
        -		return viye.FormatOutput(string(out)), nil

      
        
        52
        +		return viye.FormatOutput(out), nil

      
        53
        53
         

      
        54
        54
         	default:

      
        55
        55
         		return "", errors.New("invalid command, make usage: make [task]")

      
M internal/plumbing/shell/shell.go
···
        34
        34
         				return "", fmt.Errorf("timeout: command exceeded 5s")

      
        35
        35
         			}

      
        36
        36
         		}

      
        37
        
        -		return viye.FormatOutput(string(out)), nil

      
        
        37
        +		return viye.FormatOutput(out), nil

      
        38
        38
         

      
        39
        39
         	case isBGCmd(c):

      
        40
        40
         		cmd := exec.Command("sh", "-c", strings.Join(c.Args, " "))

      ···
        43
        43
         		if err := cmd.Start(); err != nil {

      
        44
        44
         			return "", err

      
        45
        45
         		}

      
        46
        
        -		return fmt.Sprintf("| pid: %d", cmd.Process.Pid), nil

      
        
        46
        +		out := fmt.Sprintf("pid: %d", cmd.Process.Pid)

      
        
        47
        +		return viye.FormatOutput(out), nil

      
        47
        48
         

      
        48
        49
         	case isLs(c):

      
        49
        50
         		entries, err := os.ReadDir(c.Dir)

      ···
        58
        59
         			}

      
        59
        60
         			res = append(res, name)

      
        60
        61
         		}

      
        61
        
        -		return viye.FormatOutputList(res), nil

      
        
        62
        +		return viye.FormatOutput(res), nil

      
        62
        63
         

      
        63
        64
         	case isKill(c):

      
        64
        65
         		pid, err := strconv.Atoi(c.Args[0])

      
M internal/plumbing/weather/weather.go
···
        34
        34
         		return "", err

      
        35
        35
         	}

      
        36
        36
         

      
        37
        
        -	return viye.FormatOutputList(render(city, fc)), nil

      
        
        37
        +	return viye.FormatOutput(render(city, fc)), nil

      
        38
        38
         }

      
        39
        39
         

      
        40
        40
         func getCity(c *viye.Context) (string, error) {

      
M internal/viye/format.go
···
        2
        2
         

      
        3
        3
         import "strings"

      
        4
        4
         

      
        5
        
        -func FormatOutputList(lines []string) string {

      
        6
        
        -	for i, line := range lines {

      
        7
        
        -		if line == "" {

      
        8
        
        -			lines[i] = "|"

      
        9
        
        -		} else {

      
        10
        
        -			lines[i] = "| " + line

      
        11
        
        -		}

      
        
        5
        +func FormatOutput[T string | []string | []byte](s T) string {

      
        
        6
        +	if d := decorate(asString(s), "| ", "|"); d != "" {

      
        
        7
        +		return d + "\n"

      
        12
        8
         	}

      
        13
        
        -	return strings.Join(lines, "\n") + "\n"

      
        
        9
        +	return ""

      
        14
        10
         }

      
        15
        11
         

      
        16
        
        -func FormatOutput(s string) string {

      
        17
        
        -	s = strings.TrimSuffix(s, "\n")

      
        18
        
        -	if s == "" {

      
        19
        
        -		return ""

      
        20
        
        -	}

      
        21
        
        -	return FormatOutputList(strings.Split(s, "\n"))

      
        
        12
        +func FormatBulletList[T string | []string | []byte](s T) string {

      
        
        13
        +	return decorate(asString(s), "- ", "")

      
        22
        14
         }

      
        23
        15
         

      
        24
        
        -func FormatBulletListString(s string) string {

      
        25
        
        -	return FormatBulletList(strings.Split(s, "\n"))

      
        
        16
        +func asString[T string | []string | []byte](s T) string {

      
        
        17
        +	switch v := any(s).(type) {

      
        
        18
        +	case string:

      
        
        19
        +		return strings.TrimSuffix(v, "\n")

      
        
        20
        +	case []string:

      
        
        21
        +		return strings.Join(v, "\n")

      
        
        22
        +	case []byte:

      
        
        23
        +		return strings.TrimSuffix(string(v), "\n")

      
        
        24
        +	}

      
        
        25
        +	panic("unreachable")

      
        26
        26
         }

      
        27
        27
         

      
        28
        
        -func FormatBulletList(s []string) string {

      
        29
        
        -	for i, line := range s {

      
        30
        
        -		if line != "" {

      
        31
        
        -			s[i] = "- " + line

      
        
        28
        +func decorate(s, prefix, blank string) string {

      
        
        29
        +	ls := strings.Split(s, "\n")

      
        
        30
        +	for i, line := range ls {

      
        
        31
        +		if line == "" {

      
        
        32
        +			ls[i] = blank

      
        
        33
        +		} else {

      
        
        34
        +			ls[i] = prefix + line

      
        32
        35
         		}

      
        33
        36
         	}

      
        34
        
        -	return strings.Join(s, "\n")

      
        
        37
        +	return strings.Join(ls, "\n")

      
        35
        38
         }