github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/progress/export_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package progress 21 22 import ( 23 "io" 24 ) 25 26 var ( 27 ClrEOL = clrEOL 28 CursorInvisible = cursorInvisible 29 CursorVisible = cursorVisible 30 EnterReverseMode = enterReverseMode 31 ExitAttributeMode = exitAttributeMode 32 ) 33 34 func MockEmptyEscapes() func() { 35 clrEOL = "" 36 cursorInvisible = "" 37 cursorVisible = "" 38 enterReverseMode = "" 39 exitAttributeMode = "" 40 41 return func() { 42 clrEOL = ClrEOL 43 cursorInvisible = CursorInvisible 44 cursorVisible = CursorVisible 45 enterReverseMode = EnterReverseMode 46 exitAttributeMode = ExitAttributeMode 47 } 48 } 49 50 func MockSimpleEscapes() func() { 51 // set them to the tcap name (in all caps) 52 clrEOL = "<CE>" 53 cursorInvisible = "<VI>" 54 cursorVisible = "<VS>" 55 enterReverseMode = "<MR>" 56 exitAttributeMode = "<ME>" 57 58 return func() { 59 clrEOL = ClrEOL 60 cursorInvisible = CursorInvisible 61 cursorVisible = CursorVisible 62 enterReverseMode = EnterReverseMode 63 exitAttributeMode = ExitAttributeMode 64 } 65 } 66 67 func (p *ANSIMeter) Percent() string { 68 return p.percent() 69 } 70 71 func (p *ANSIMeter) SetWritten(written float64) { 72 p.written = written 73 } 74 75 func (p *ANSIMeter) GetWritten() float64 { 76 return p.written 77 } 78 79 func (p *ANSIMeter) GetTotal() float64 { 80 return p.total 81 } 82 83 func MockTermWidth(f func() int) func() { 84 origTermWidth := termWidth 85 termWidth = f 86 return func() { 87 termWidth = origTermWidth 88 } 89 } 90 91 func MockStdout(w io.Writer) func() { 92 origStdout := stdout 93 stdout = w 94 return func() { 95 stdout = origStdout 96 } 97 } 98 99 func MockFormatDuration(m func(f float64) string) (restore func()) { 100 old := formatDuration 101 formatDuration = m 102 return func() { 103 formatDuration = old 104 } 105 } 106 107 var ( 108 Norm = norm 109 Spinner = spinner 110 )