Debian Codegraph @

ansi/iterm2/file.go

raw
1 // Package iterm2 provides iTerm2-specific functionality.
2 package iterm2
3
4 import (
5 "strconv"
6 "strings"
7 )
8
9 // Auto is a constant that represents the "auto" value.
10 const Auto = "auto"
11
12 // Cells returns a string that represents the number of cells. This is simply a
13 // wrapper around strconv.Itoa.
14 func Cells(n int) string {
15 return strconv.Itoa(n)
16 }
17
18 // Pixels returns a string that represents the number of pixels.
19 func Pixels(n int) string {
20 return strconv.Itoa(n) + "px"
21 }
22
23 // Percent returns a string that represents the percentage.
24 func Percent(n int) string {
25 return strconv.Itoa(n) + "%"
26 }
27
28 // file represents the optional arguments for the iTerm2 Inline Image Protocol.
29 //
30 // See https://iterm2.com/documentation-images.html
31 type file struct {
32 // Name is the name of the file. Defaults to "Unnamed file" if empty.
33 Name string
34 // Size is the file size in bytes. Used for progress indication. This is
35 // optional.
36 Size int64
37 // Width is the width of the image. This can be specified by a number
38 // followed by by a unit or "auto". The unit can be none, "px" or "%". None
39 // means the number is in cells. Defaults to "auto" if empty.
40 // For convenience, the [Pixels], [Cells] and [Percent] functions and
41 // [Auto] can be used.
42 Width string
43 // Height is the height of the image. This can be specified by a number
44 // followed by by a unit or "auto". The unit can be none, "px" or "%". None
45 // means the number is in cells. Defaults to "auto" if empty.
46 // For convenience, the [Pixels], [Cells] and [Percent] functions and
47 // [Auto] can be used.
48 Height string
49 // IgnoreAspectRatio is a flag that indicates that the image should be
50 // stretched to fit the specified width and height. Defaults to false
51 // meaning the aspect ratio is preserved.
52 IgnoreAspectRatio bool
53 // Inline is a flag that indicates that the image should be displayed
54 // inline. Otherwise, it is downloaded to the Downloads folder with no
55 // visual representation in the terminal. Defaults to false.
56 Inline bool
57 // DoNotMoveCursor is a flag that indicates that the cursor should not be
58 // moved after displaying the image. This is an extension introduced by
59 // WezTerm and might not work on all terminals supporting the iTerm2
60 // protocol. Defaults to false.
61 DoNotMoveCursor bool
62 // Content is the base64 encoded data of the file.
63 Content []byte
64 }
65
66 // String implements fmt.Stringer.
67 func (f file) String() string {
68 var opts []string
69 if f.Name != "" {
70 opts = append(opts, "name="+f.Name)
71 }
72 if f.Size != 0 {
73 opts = append(opts, "size="+strconv.FormatInt(f.Size, 10))
74 }
75 if f.Width != "" {
76 opts = append(opts, "width="+f.Width)
77 }
78 if f.Height != "" {
79 opts = append(opts, "height="+f.Height)
80 }
81 if f.IgnoreAspectRatio {
82 opts = append(opts, "preserveAspectRatio=0")
83 }
84 if f.Inline {
85 opts = append(opts, "inline=1")
86 }
87 if f.DoNotMoveCursor {
88 opts = append(opts, "doNotMoveCursor=1")
89 }
90 return strings.Join(opts, ";")
91 }
92
93 // File represents the optional arguments for the iTerm2 Inline Image Protocol.
94 type File file
95
96 // String implements fmt.Stringer.
97 func (f File) String() string {
98 var s strings.Builder
99 s.WriteString("File=")
100 s.WriteString(file(f).String())
101 if len(f.Content) > 0 {
102 s.WriteString(":")
103 s.Write(f.Content)
104 }
105
106 return s.String()
107 }
108
109 // MultipartFile represents the optional arguments for the iTerm2 Inline Image Protocol.
110 type MultipartFile file
111
112 // String implements fmt.Stringer.
113 func (f MultipartFile) String() string {
114 return "MultipartFile=" + file(f).String()
115 }
116
117 // FilePart represents the optional arguments for the iTerm2 Inline Image Protocol.
118 type FilePart file
119
120 // String implements fmt.Stringer.
121 func (f FilePart) String() string {
122 return "FilePart=" + string(f.Content)
123 }
124
125 // FileEnd represents the optional arguments for the iTerm2 Inline Image Protocol.
126 type FileEnd struct{}
127
128 // String implements fmt.Stringer.
129 func (f FileEnd) String() string {
130 return "FileEnd"
131 }