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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//! View structured data (i.e., `json::JsonValue`s) in an `unsegen` widget.
//!
//! # Example:
//! ```no_run
//! extern crate unsegen;
//!
//! use std::io::{stdin, stdout};
//! use unsegen::base::Terminal;
//! use unsegen::input::{Input, Key, ScrollBehavior};
//! use unsegen::widget::{RenderingHints, Widget};
//!
//! use unsegen_jsonviewer::json_ext::{JsonValue, Object};
//! use unsegen_jsonviewer::JsonViewer;
//!
//! fn main() {
//!     let stdout = stdout();
//!     let stdin = stdin();
//!     let stdin = stdin.lock();
//!
//!     let mut json_viewer = JsonViewer::new(&JsonValue::Null);
//!
//!     let mut term = Terminal::new(stdout.lock()).unwrap();
//!
//!     for input in Input::read_all(stdin) {
//!         let input = input.unwrap();
//!         input
//!             .chain(
//!                 ScrollBehavior::new(&mut json_viewer)
//!                     .forwards_on(Key::Down)
//!                     .forwards_on(Key::Char('j'))
//!                     .backwards_on(Key::Up)
//!                     .backwards_on(Key::Char('k'))
//!                     .to_beginning_on(Key::Home)
//!                     .to_end_on(Key::End),
//!             )
//!             .chain((Key::Char('s'), || {
//!                 let mut object = Object::new();
//!                 object.insert("foo", JsonValue::String("String!".to_owned()));
//!                 object.insert("bar", JsonValue::Boolean(true));
//!                 json_viewer.update(&JsonValue::Object(object));
//!             }))
//!             .chain((Key::Char('n'), || {
//!                 let mut object = Object::new();
//!                 object.insert("foo", JsonValue::Number((27 * 37).into()));
//!                 object.insert("bar", JsonValue::Boolean(true));
//!                 // Notice that foo is highlighted when pressing 'n' after 's'!
//!                 json_viewer.update(&JsonValue::Object(object));
//!             }));
//!         // Put more application logic here...
//!
//!         {
//!             let win = term.create_root_window();
//!             json_viewer.as_widget().draw(win, RenderingHints::default());
//!         }
//!         term.present();
//!     }
//! }
//! ```
#[cfg(test)] //Only tests use macros. Otherwise we get unused_imports warnings.
#[macro_use]
extern crate json;

#[cfg(not(test))]
extern crate json;

extern crate unsegen;

use unsegen::base::basic_types::*;
use unsegen::base::{BoolModifyMode, Color, Cursor, ExtentEstimationWindow, StyleModifier, Window};
use unsegen::widget::{Demand, Demand2D, RenderingHints, Widget};

use unsegen::input::{OperationResult, Scrollable};

/// Convenience reexport of `json` types.
pub mod json_ext {
    pub use json::{number::Number, object::Object, Array, JsonValue};
}

impl Value for &json_ext::JsonValue {
    fn visit<'s>(self) -> ValueVariant<'s, Self> {
        match self {
            json_ext::JsonValue::Null => ValueVariant::Scalar("null".to_string()),
            json_ext::JsonValue::Short(val) => ValueVariant::Scalar(val.to_string()),
            json_ext::JsonValue::String(val) => ValueVariant::Scalar(val.to_string()),
            json_ext::JsonValue::Number(val) => ValueVariant::Scalar(val.to_string()),
            json_ext::JsonValue::Boolean(val) => ValueVariant::Scalar(val.to_string()),
            json_ext::JsonValue::Object(val) => {
                ValueVariant::Map(None, Box::new(val.iter().map(|(k, v)| (k.to_owned(), v))))
            }
            json_ext::JsonValue::Array(val) => ValueVariant::Array(None, Box::new(val.iter())),
        }
    }
}

impl Value for &str {
    fn visit<'s>(self) -> ValueVariant<'s, Self> {
        ValueVariant::Scalar(self.to_owned())
    }
}

pub enum ValueVariant<'s, V: Value + 's> {
    Scalar(String),
    Array(Option<String>, Box<dyn Iterator<Item = V> + 's>),
    Map(Option<String>, Box<dyn Iterator<Item = (String, V)> + 's>),
}

pub trait Value: Sized + Clone {
    fn visit<'s>(self) -> ValueVariant<'s, Self>;
}

mod displayvalue;
mod path;

use self::displayvalue::*;
use self::path::*;

/// A widget for viewing `json` data.
///
/// Set an initial value during construction (via `new`) and replace it either using `update` or `reset`.
///
/// The widgets provides multiple interaction points, for example to fold or unfold structures, or
/// to grow or shrink the number of elements shown of an array. At any time, one of these knobs is
/// active. Use `select_next` or `select_previous` or the `Scrollable` implementation to change it.
/// Call `toggle_active_element` to interact with the currently active element.
/// Interacting with an element might hide it, but the widget takes care to select another element
/// in that case.
pub struct JsonViewer {
    value: DisplayValue,
    active_element: Path,
}

impl JsonViewer {
    /// Create a new `JsonViewer` widget that will display the specified value.
    ///
    /// It follows that it is impossible to not have content. However, it *is* possible to show an
    /// empty String, so there is that.
    pub fn new(value: impl Value) -> Self {
        let mut res = JsonViewer {
            value: DisplayValue::new(value),
            active_element: Path::Scalar, //Will be fixed ...
        };
        res.fix_active_element_path(); //... here!
        res
    }

    /// Set a new value to display and do not highlight any changes (in contrast to `update`).
    pub fn reset(&mut self, value: impl Value) {
        self.value = DisplayValue::new(value);
        self.fix_active_element_path();
    }

    /// Set a new value to display and highlight changes from the previous value (which will be
    /// shown until the next `update` or `reset`.
    pub fn update(&mut self, value: impl Value) {
        self.value = self.value.update(value);
        self.fix_active_element_path();
    }

    /// Select the next interaction point of the widget (generally "down" from the current one).
    pub fn select_next(&mut self) -> Result<(), ()> {
        if let Some(new_path) = self.active_element.clone().find_next_path(&self.value) {
            self.active_element = new_path;
            Ok(())
        } else {
            Err(())
        }
    }

    /// Select the previous interaction point of the widget (generally "up" from the current one).
    pub fn select_previous(&mut self) -> Result<(), ()> {
        if let Some(new_path) = self.active_element.clone().find_previous_path(&self.value) {
            self.active_element = new_path;
            Ok(())
        } else {
            Err(())
        }
    }

    fn fix_active_element_path(&mut self) {
        let mut tmp = Path::Scalar;
        ::std::mem::swap(&mut self.active_element, &mut tmp);
        self.active_element = tmp.fix_path_for_value(&self.value)
    }

    /// Interact with the currently active interaction point and, for example, fold/unfold
    /// structures.
    pub fn toggle_active_element(&mut self) -> Result<(), ()> {
        let res = self.active_element.find_and_act_on_element(&mut self.value);
        self.fix_active_element_path();
        res
    }

    pub fn as_widget<'a>(&'a self) -> JsonViewerWidget<'a> {
        JsonViewerWidget {
            inner: self,
            indentation: Width::new(2).unwrap(),
            active_focused_style: StyleModifier::new()
                .invert(BoolModifyMode::Toggle)
                .bold(true),
            inactive_focused_style: StyleModifier::new().bold(true),
            item_changed_style: StyleModifier::new().bg_color(Color::Red),
        }
    }
}

pub struct JsonViewerWidget<'a> {
    inner: &'a JsonViewer,
    indentation: Width,
    active_focused_style: StyleModifier,
    inactive_focused_style: StyleModifier,
    item_changed_style: StyleModifier,
}

impl<'a> JsonViewerWidget<'a> {
    pub fn indentation(mut self, w: Width) -> Self {
        self.indentation = w;
        self
    }
    pub fn active_focused(mut self, style: StyleModifier) -> Self {
        self.active_focused_style = style;
        self
    }
    pub fn inactive_focused(mut self, style: StyleModifier) -> Self {
        self.inactive_focused_style = style;
        self
    }
    pub fn item_changed(mut self, style: StyleModifier) -> Self {
        self.item_changed_style = style;
        self
    }
}

impl<'a> Widget for JsonViewerWidget<'a> {
    fn space_demand(&self) -> Demand2D {
        let mut window = ExtentEstimationWindow::unbounded();
        //TODO: We may want to consider passing hints to space_demand as well for an accurate estimate
        {
            let mut cursor = Cursor::<ExtentEstimationWindow>::new(&mut window);
            let info = RenderingInfo {
                hints: RenderingHints::default(),
                active_focused_style: self.active_focused_style,
                inactive_focused_style: self.inactive_focused_style,
                item_changed_style: self.item_changed_style,
            };
            self.inner.value.draw(
                &mut cursor,
                Some(&self.inner.active_element),
                &info,
                self.indentation,
            );
        }
        Demand2D {
            width: Demand::at_least(window.extent_x()),
            height: Demand::exact(window.extent_y()),
        }
    }
    fn draw(&self, mut window: Window, hints: RenderingHints) {
        let mut cursor = Cursor::new(&mut window);
        let info = RenderingInfo {
            hints,
            active_focused_style: self.active_focused_style,
            inactive_focused_style: self.inactive_focused_style,
            item_changed_style: self.item_changed_style,
        };
        self.inner.value.draw(
            &mut cursor,
            Some(&self.inner.active_element),
            &info,
            self.indentation,
        );
    }
}

impl Scrollable for JsonViewer {
    fn scroll_forwards(&mut self) -> OperationResult {
        self.select_next()
    }
    fn scroll_backwards(&mut self) -> OperationResult {
        self.select_previous()
    }
}