File: file.go

package info (click to toggle)
golang-github-charmbracelet-x 0.0~git20251028.0cf22f8%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,940 kB
  • sloc: sh: 124; makefile: 5
file content (131 lines) | stat: -rw-r--r-- 3,930 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Package iterm2 provides iTerm2-specific functionality.
package iterm2

import (
	"strconv"
	"strings"
)

// Auto is a constant that represents the "auto" value.
const Auto = "auto"

// Cells returns a string that represents the number of cells. This is simply a
// wrapper around strconv.Itoa.
func Cells(n int) string {
	return strconv.Itoa(n)
}

// Pixels returns a string that represents the number of pixels.
func Pixels(n int) string {
	return strconv.Itoa(n) + "px"
}

// Percent returns a string that represents the percentage.
func Percent(n int) string {
	return strconv.Itoa(n) + "%"
}

// file represents the optional arguments for the iTerm2 Inline Image Protocol.
//
// See https://iterm2.com/documentation-images.html
type file struct {
	// Name is the name of the file. Defaults to "Unnamed file" if empty.
	Name string
	// Size is the file size in bytes. Used for progress indication. This is
	// optional.
	Size int64
	// Width is the width of the image. This can be specified by a number
	// followed by by a unit or "auto". The unit can be none, "px" or "%". None
	// means the number is in cells. Defaults to "auto" if empty.
	// For convenience, the [Pixels], [Cells] and [Percent] functions and
	// [Auto] can be used.
	Width string
	// Height is the height of the image. This can be specified by a number
	// followed by by a unit or "auto". The unit can be none, "px" or "%". None
	// means the number is in cells. Defaults to "auto" if empty.
	// For convenience, the [Pixels], [Cells] and [Percent] functions and
	// [Auto] can be used.
	Height string
	// IgnoreAspectRatio is a flag that indicates that the image should be
	// stretched to fit the specified width and height. Defaults to false
	// meaning the aspect ratio is preserved.
	IgnoreAspectRatio bool
	// Inline is a flag that indicates that the image should be displayed
	// inline. Otherwise, it is downloaded to the Downloads folder with no
	// visual representation in the terminal. Defaults to false.
	Inline bool
	// DoNotMoveCursor is a flag that indicates that the cursor should not be
	// moved after displaying the image. This is an extension introduced by
	// WezTerm and might not work on all terminals supporting the iTerm2
	// protocol. Defaults to false.
	DoNotMoveCursor bool
	// Content is the base64 encoded data of the file.
	Content []byte
}

// String implements fmt.Stringer.
func (f file) String() string {
	var opts []string
	if f.Name != "" {
		opts = append(opts, "name="+f.Name)
	}
	if f.Size != 0 {
		opts = append(opts, "size="+strconv.FormatInt(f.Size, 10))
	}
	if f.Width != "" {
		opts = append(opts, "width="+f.Width)
	}
	if f.Height != "" {
		opts = append(opts, "height="+f.Height)
	}
	if f.IgnoreAspectRatio {
		opts = append(opts, "preserveAspectRatio=0")
	}
	if f.Inline {
		opts = append(opts, "inline=1")
	}
	if f.DoNotMoveCursor {
		opts = append(opts, "doNotMoveCursor=1")
	}
	return strings.Join(opts, ";")
}

// File represents the optional arguments for the iTerm2 Inline Image Protocol.
type File file

// String implements fmt.Stringer.
func (f File) String() string {
	var s strings.Builder
	s.WriteString("File=")
	s.WriteString(file(f).String())
	if len(f.Content) > 0 {
		s.WriteString(":")
		s.Write(f.Content)
	}

	return s.String()
}

// MultipartFile represents the optional arguments for the iTerm2 Inline Image Protocol.
type MultipartFile file

// String implements fmt.Stringer.
func (f MultipartFile) String() string {
	return "MultipartFile=" + file(f).String()
}

// FilePart represents the optional arguments for the iTerm2 Inline Image Protocol.
type FilePart file

// String implements fmt.Stringer.
func (f FilePart) String() string {
	return "FilePart=" + string(f.Content)
}

// FileEnd represents the optional arguments for the iTerm2 Inline Image Protocol.
type FileEnd struct{}

// String implements fmt.Stringer.
func (f FileEnd) String() string {
	return "FileEnd"
}