| 1 |
package kitty |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding" |
| 5 |
"fmt" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
var ( |
| 11 |
_ encoding.TextMarshaler = Options{} |
| 12 |
_ encoding.TextUnmarshaler = &Options{} |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
type Options struct { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
Action byte |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
Quite byte |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
ID int |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
PlacementID int |
| 39 |
|
| 40 |
|
| 41 |
Number int |
| 42 |
|
| 43 |
|
| 44 |
Format int |
| 45 |
|
| 46 |
|
| 47 |
ImageWidth int |
| 48 |
|
| 49 |
|
| 50 |
ImageHeight int |
| 51 |
|
| 52 |
|
| 53 |
Compression byte |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Transmission byte |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
File string |
| 63 |
|
| 64 |
|
| 65 |
Size int |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
Offset int |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
Chunk bool |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
X int |
| 81 |
|
| 82 |
|
| 83 |
Y int |
| 84 |
|
| 85 |
|
| 86 |
Z int |
| 87 |
|
| 88 |
|
| 89 |
Width int |
| 90 |
|
| 91 |
|
| 92 |
Height int |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
OffsetX int |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
OffsetY int |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
Columns int |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
Rows int |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
VirtualPlacement bool |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
DoNotMoveCursor bool |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
ParentID int |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
ParentPlacementID int |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
Delete byte |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
DeleteResources bool |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func (o *Options) Options() (opts []string) { |
| 145 |
opts = []string{} |
| 146 |
if o.Format == 0 { |
| 147 |
o.Format = RGBA |
| 148 |
} |
| 149 |
|
| 150 |
if o.Action == 0 { |
| 151 |
o.Action = Transmit |
| 152 |
} |
| 153 |
|
| 154 |
if o.Delete == 0 { |
| 155 |
o.Delete = DeleteAll |
| 156 |
} |
| 157 |
|
| 158 |
if o.Transmission == 0 { |
| 159 |
if len(o.File) > 0 { |
| 160 |
o.Transmission = File |
| 161 |
} else { |
| 162 |
o.Transmission = Direct |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if o.Format != RGBA { |
| 167 |
opts = append(opts, fmt.Sprintf("f=%d", o.Format)) |
| 168 |
} |
| 169 |
|
| 170 |
if o.Quite > 0 { |
| 171 |
opts = append(opts, fmt.Sprintf("q=%d", o.Quite)) |
| 172 |
} |
| 173 |
|
| 174 |
if o.ID > 0 { |
| 175 |
opts = append(opts, fmt.Sprintf("i=%d", o.ID)) |
| 176 |
} |
| 177 |
|
| 178 |
if o.PlacementID > 0 { |
| 179 |
opts = append(opts, fmt.Sprintf("p=%d", o.PlacementID)) |
| 180 |
} |
| 181 |
|
| 182 |
if o.Number > 0 { |
| 183 |
opts = append(opts, fmt.Sprintf("I=%d", o.Number)) |
| 184 |
} |
| 185 |
|
| 186 |
if o.ImageWidth > 0 { |
| 187 |
opts = append(opts, fmt.Sprintf("s=%d", o.ImageWidth)) |
| 188 |
} |
| 189 |
|
| 190 |
if o.ImageHeight > 0 { |
| 191 |
opts = append(opts, fmt.Sprintf("v=%d", o.ImageHeight)) |
| 192 |
} |
| 193 |
|
| 194 |
if o.Transmission != Direct { |
| 195 |
opts = append(opts, fmt.Sprintf("t=%c", o.Transmission)) |
| 196 |
} |
| 197 |
|
| 198 |
if o.Size > 0 { |
| 199 |
opts = append(opts, fmt.Sprintf("S=%d", o.Size)) |
| 200 |
} |
| 201 |
|
| 202 |
if o.Offset > 0 { |
| 203 |
opts = append(opts, fmt.Sprintf("O=%d", o.Offset)) |
| 204 |
} |
| 205 |
|
| 206 |
if o.Compression == Zlib { |
| 207 |
opts = append(opts, fmt.Sprintf("o=%c", o.Compression)) |
| 208 |
} |
| 209 |
|
| 210 |
if o.VirtualPlacement { |
| 211 |
opts = append(opts, "U=1") |
| 212 |
} |
| 213 |
|
| 214 |
if o.DoNotMoveCursor { |
| 215 |
opts = append(opts, "C=1") |
| 216 |
} |
| 217 |
|
| 218 |
if o.ParentID > 0 { |
| 219 |
opts = append(opts, fmt.Sprintf("P=%d", o.ParentID)) |
| 220 |
} |
| 221 |
|
| 222 |
if o.ParentPlacementID > 0 { |
| 223 |
opts = append(opts, fmt.Sprintf("Q=%d", o.ParentPlacementID)) |
| 224 |
} |
| 225 |
|
| 226 |
if o.X > 0 { |
| 227 |
opts = append(opts, fmt.Sprintf("x=%d", o.X)) |
| 228 |
} |
| 229 |
|
| 230 |
if o.Y > 0 { |
| 231 |
opts = append(opts, fmt.Sprintf("y=%d", o.Y)) |
| 232 |
} |
| 233 |
|
| 234 |
if o.Z > 0 { |
| 235 |
opts = append(opts, fmt.Sprintf("z=%d", o.Z)) |
| 236 |
} |
| 237 |
|
| 238 |
if o.Width > 0 { |
| 239 |
opts = append(opts, fmt.Sprintf("w=%d", o.Width)) |
| 240 |
} |
| 241 |
|
| 242 |
if o.Height > 0 { |
| 243 |
opts = append(opts, fmt.Sprintf("h=%d", o.Height)) |
| 244 |
} |
| 245 |
|
| 246 |
if o.OffsetX > 0 { |
| 247 |
opts = append(opts, fmt.Sprintf("X=%d", o.OffsetX)) |
| 248 |
} |
| 249 |
|
| 250 |
if o.OffsetY > 0 { |
| 251 |
opts = append(opts, fmt.Sprintf("Y=%d", o.OffsetY)) |
| 252 |
} |
| 253 |
|
| 254 |
if o.Columns > 0 { |
| 255 |
opts = append(opts, fmt.Sprintf("c=%d", o.Columns)) |
| 256 |
} |
| 257 |
|
| 258 |
if o.Rows > 0 { |
| 259 |
opts = append(opts, fmt.Sprintf("r=%d", o.Rows)) |
| 260 |
} |
| 261 |
|
| 262 |
if o.Delete != DeleteAll || o.DeleteResources { |
| 263 |
da := o.Delete |
| 264 |
if o.DeleteResources { |
| 265 |
da = da - ' ' |
| 266 |
} |
| 267 |
|
| 268 |
opts = append(opts, fmt.Sprintf("d=%c", da)) |
| 269 |
} |
| 270 |
|
| 271 |
if o.Action != Transmit { |
| 272 |
opts = append(opts, fmt.Sprintf("a=%c", o.Action)) |
| 273 |
} |
| 274 |
|
| 275 |
return opts |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
func (o Options) String() string { |
| 280 |
return strings.Join(o.Options(), ",") |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
func (o Options) MarshalText() ([]byte, error) { |
| 285 |
return []byte(o.String()), nil |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
func (o *Options) UnmarshalText(text []byte) error { |
| 290 |
opts := strings.Split(string(text), ",") |
| 291 |
for _, opt := range opts { |
| 292 |
ps := strings.SplitN(opt, "=", 2) |
| 293 |
if len(ps) != 2 || len(ps[1]) == 0 { |
| 294 |
continue |
| 295 |
} |
| 296 |
|
| 297 |
switch ps[0] { |
| 298 |
case "a": |
| 299 |
o.Action = ps[1][0] |
| 300 |
case "o": |
| 301 |
o.Compression = ps[1][0] |
| 302 |
case "t": |
| 303 |
o.Transmission = ps[1][0] |
| 304 |
case "d": |
| 305 |
d := ps[1][0] |
| 306 |
if d >= 'A' && d <= 'Z' { |
| 307 |
o.DeleteResources = true |
| 308 |
d = d + ' ' |
| 309 |
} |
| 310 |
o.Delete = d |
| 311 |
case "i", "q", "p", "I", "f", "s", "v", "S", "O", "m", "x", "y", "z", "w", "h", "X", "Y", "c", "r", "U", "P", "Q": |
| 312 |
v, err := strconv.Atoi(ps[1]) |
| 313 |
if err != nil { |
| 314 |
continue |
| 315 |
} |
| 316 |
|
| 317 |
switch ps[0] { |
| 318 |
case "i": |
| 319 |
o.ID = v |
| 320 |
case "q": |
| 321 |
o.Quite = byte(v) |
| 322 |
case "p": |
| 323 |
o.PlacementID = v |
| 324 |
case "I": |
| 325 |
o.Number = v |
| 326 |
case "f": |
| 327 |
o.Format = v |
| 328 |
case "s": |
| 329 |
o.ImageWidth = v |
| 330 |
case "v": |
| 331 |
o.ImageHeight = v |
| 332 |
case "S": |
| 333 |
o.Size = v |
| 334 |
case "O": |
| 335 |
o.Offset = v |
| 336 |
case "m": |
| 337 |
o.Chunk = v == 0 || v == 1 |
| 338 |
case "x": |
| 339 |
o.X = v |
| 340 |
case "y": |
| 341 |
o.Y = v |
| 342 |
case "z": |
| 343 |
o.Z = v |
| 344 |
case "w": |
| 345 |
o.Width = v |
| 346 |
case "h": |
| 347 |
o.Height = v |
| 348 |
case "X": |
| 349 |
o.OffsetX = v |
| 350 |
case "Y": |
| 351 |
o.OffsetY = v |
| 352 |
case "c": |
| 353 |
o.Columns = v |
| 354 |
case "r": |
| 355 |
o.Rows = v |
| 356 |
case "U": |
| 357 |
o.VirtualPlacement = v == 1 |
| 358 |
case "P": |
| 359 |
o.ParentID = v |
| 360 |
case "Q": |
| 361 |
o.ParentPlacementID = v |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
return nil |
| 367 |
} |